Adds User.organization_attributes

This commit is contained in:
kikito
2015-08-16 23:35:56 +02:00
parent 60a1cc2267
commit 9343952d52
3 changed files with 22 additions and 29 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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