diff --git a/app/models/organization.rb b/app/models/organization.rb index 8bcb377b6..29bf01c2c 100644 --- a/app/models/organization.rb +++ b/app/models/organization.rb @@ -2,6 +2,8 @@ class Organization < ActiveRecord::Base belongs_to :user + validates :name, presence: true + delegate :email, :phone_number, to: :user def verify @@ -22,5 +24,4 @@ class Organization < ActiveRecord::Base (verified_at.blank? || verified_at < rejected_at) end - end diff --git a/app/models/user.rb b/app/models/user.rb index 3b14879ca..ef1afc5ee 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -4,15 +4,18 @@ class User < ActiveRecord::Base acts_as_voter - validates :first_name, presence: true, if: :use_first_name? - validates :last_name, presence: true, if: :use_last_name? - validates :nickname, presence: true, if: :use_nickname? - validates :organization_name, presence: true, if: :is_organization - has_one :administrator has_one :moderator has_one :organization + validates :first_name, presence: true, if: :use_first_name? + validates :last_name, presence: true, if: :use_last_name? + validates :nickname, presence: true, if: :use_nickname? + + validates_associated :organization, message: false + + accepts_nested_attributes_for :organization + scope :administrators, -> { joins(:administrators) } scope :moderators, -> { joins(:moderator) } scope :organizations, -> { joins(:organization) } @@ -50,15 +53,10 @@ class User < ActiveRecord::Base private def use_first_name? - !is_organization && !use_nickname? + !organization? && !use_nickname? end def use_last_name? use_first_name? end - - def create_associated_organization - create_organization(name: organization_name) if is_organization - end - end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index ce9fb5989..7d79898c0 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -129,27 +129,21 @@ describe User do end end - describe "is_organization" do - before(:each) { subject.is_organization = true } + describe "organization_attributes" do + before(:each) { subject.organization_attributes = {name: 'org'} } - it "deactivates the validation of first_name and last_name, and activates the validation of organization_name" do - subject.first_name = nil - subject.last_name = nil - subject.organization_name = nil - expect(subject).to_not be_valid - - subject.organization_name = 'org' - expect(subject).to be_valid + it "triggers the creation of an associated organization" do + expect(subject.organization).to be + expect(subject.organization.name).to eq('org') end - it "triggers the creation of an associated organization using organization_name" do - expect(subject.organization).to_not be + it "deactivates the validation of first_name and last_name, and activates the validation of organization" do + subject.first_name = nil + subject.last_name = nil + expect(subject).to be_valid - subject.is_organization = true - subject.organization_name = 'org' - subject.save - - expect(subject.organization).to be + subject.organization.name= nil + expect(subject).to_not be_valid end end