Adds User.organization_attributes
This commit is contained in:
@@ -2,6 +2,8 @@ class Organization < ActiveRecord::Base
|
|||||||
|
|
||||||
belongs_to :user
|
belongs_to :user
|
||||||
|
|
||||||
|
validates :name, presence: true
|
||||||
|
|
||||||
delegate :email, :phone_number, to: :user
|
delegate :email, :phone_number, to: :user
|
||||||
|
|
||||||
def verify
|
def verify
|
||||||
@@ -22,5 +24,4 @@ class Organization < ActiveRecord::Base
|
|||||||
(verified_at.blank? || verified_at < rejected_at)
|
(verified_at.blank? || verified_at < rejected_at)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -4,15 +4,18 @@ class User < ActiveRecord::Base
|
|||||||
|
|
||||||
acts_as_voter
|
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 :administrator
|
||||||
has_one :moderator
|
has_one :moderator
|
||||||
has_one :organization
|
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 :administrators, -> { joins(:administrators) }
|
||||||
scope :moderators, -> { joins(:moderator) }
|
scope :moderators, -> { joins(:moderator) }
|
||||||
scope :organizations, -> { joins(:organization) }
|
scope :organizations, -> { joins(:organization) }
|
||||||
@@ -50,15 +53,10 @@ class User < ActiveRecord::Base
|
|||||||
|
|
||||||
private
|
private
|
||||||
def use_first_name?
|
def use_first_name?
|
||||||
!is_organization && !use_nickname?
|
!organization? && !use_nickname?
|
||||||
end
|
end
|
||||||
|
|
||||||
def use_last_name?
|
def use_last_name?
|
||||||
use_first_name?
|
use_first_name?
|
||||||
end
|
end
|
||||||
|
|
||||||
def create_associated_organization
|
|
||||||
create_organization(name: organization_name) if is_organization
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -129,27 +129,21 @@ describe User do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "is_organization" do
|
describe "organization_attributes" do
|
||||||
before(:each) { subject.is_organization = true }
|
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
|
it "triggers the creation of an associated organization" do
|
||||||
subject.first_name = nil
|
expect(subject.organization).to be
|
||||||
subject.last_name = nil
|
expect(subject.organization.name).to eq('org')
|
||||||
subject.organization_name = nil
|
|
||||||
expect(subject).to_not be_valid
|
|
||||||
|
|
||||||
subject.organization_name = 'org'
|
|
||||||
expect(subject).to be_valid
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it "triggers the creation of an associated organization using organization_name" do
|
it "deactivates the validation of first_name and last_name, and activates the validation of organization" do
|
||||||
expect(subject.organization).to_not be
|
subject.first_name = nil
|
||||||
|
subject.last_name = nil
|
||||||
|
expect(subject).to be_valid
|
||||||
|
|
||||||
subject.is_organization = true
|
subject.organization.name= nil
|
||||||
subject.organization_name = 'org'
|
expect(subject).to_not be_valid
|
||||||
subject.save
|
|
||||||
|
|
||||||
expect(subject.organization).to be
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user