Skip to content

Commit

Permalink
Adds User.is_organization and User.organization_name
Browse files Browse the repository at this point in the history
  • Loading branch information
kikito committed Aug 14, 2015
1 parent 4e5d53f commit 60a1cc2
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
18 changes: 14 additions & 4 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ 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 :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
Expand All @@ -16,6 +17,11 @@ class User < ActiveRecord::Base
scope :moderators, -> { joins(:moderator) }
scope :organizations, -> { joins(:organization) }

attr_accessor :organization_name
attr_accessor :is_organization

after_save :create_associated_organization

def name
return nickname if use_nickname?
return organization.name if organization?
Expand Down Expand Up @@ -44,11 +50,15 @@ def organization?

private
def use_first_name?
!use_nickname? && !organization?
!is_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
30 changes: 24 additions & 6 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,16 +123,34 @@
expect(subject.organization?).to be true
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
expect(subject.name).to eq(subject.organization.name)
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

0 comments on commit 60a1cc2

Please sign in to comment.