Adds User.is_organization and User.organization_name
This commit is contained in:
@@ -7,6 +7,7 @@ class User < ActiveRecord::Base
|
|||||||
validates :first_name, presence: true, if: :use_first_name?
|
validates :first_name, presence: true, if: :use_first_name?
|
||||||
validates :last_name, presence: true, if: :use_last_name?
|
validates :last_name, presence: true, if: :use_last_name?
|
||||||
validates :nickname, presence: true, if: :use_nickname?
|
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
|
||||||
@@ -16,6 +17,11 @@ class User < ActiveRecord::Base
|
|||||||
scope :moderators, -> { joins(:moderator) }
|
scope :moderators, -> { joins(:moderator) }
|
||||||
scope :organizations, -> { joins(:organization) }
|
scope :organizations, -> { joins(:organization) }
|
||||||
|
|
||||||
|
attr_accessor :organization_name
|
||||||
|
attr_accessor :is_organization
|
||||||
|
|
||||||
|
after_save :create_associated_organization
|
||||||
|
|
||||||
def name
|
def name
|
||||||
return nickname if use_nickname?
|
return nickname if use_nickname?
|
||||||
return organization.name if organization?
|
return organization.name if organization?
|
||||||
@@ -44,11 +50,15 @@ class User < ActiveRecord::Base
|
|||||||
|
|
||||||
private
|
private
|
||||||
def use_first_name?
|
def use_first_name?
|
||||||
!use_nickname? && !organization?
|
!is_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
|
||||||
|
|||||||
@@ -123,16 +123,34 @@ describe User do
|
|||||||
expect(subject.organization?).to be true
|
expect(subject.organization?).to be true
|
||||||
end
|
end
|
||||||
|
|
||||||
it "deactivates the validation of first_name and last_name" do
|
|
||||||
subject.first_name = nil
|
|
||||||
subject.last_name = nil
|
|
||||||
expect(subject).to be_valid
|
|
||||||
end
|
|
||||||
|
|
||||||
it "calculates the name using the organization name" do
|
it "calculates the name using the organization name" do
|
||||||
expect(subject.name).to eq(subject.organization.name)
|
expect(subject.name).to eq(subject.organization.name)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "is_organization" do
|
||||||
|
before(:each) { subject.is_organization = true }
|
||||||
|
|
||||||
|
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
|
||||||
|
end
|
||||||
|
|
||||||
|
it "triggers the creation of an associated organization using organization_name" do
|
||||||
|
expect(subject.organization).to_not be
|
||||||
|
|
||||||
|
subject.is_organization = true
|
||||||
|
subject.organization_name = 'org'
|
||||||
|
subject.save
|
||||||
|
|
||||||
|
expect(subject.organization).to be
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user