Updates User model, moving org stuff to Organisation model

This commit is contained in:
kikito
2015-08-13 20:04:57 +02:00
parent bdf462c16b
commit 976c88177d
2 changed files with 25 additions and 67 deletions

View File

@@ -8,11 +8,17 @@ class User < ActiveRecord::Base
validates :last_name, presence: true, if: :use_last_name?
validates :nickname, presence: true, if: :use_nickname?
scope :organizations, -> { where("users.organization_name IS NOT NULL AND users.organization_name <> ''") }
has_one :administrator
has_one :moderator
has_one :organization
scope :administrators, -> { joins(:administrators) }
scope :moderators, -> { joins(:moderator) }
scope :organizations, -> { joins(:organization) }
def name
return nickname if use_nickname?
return organization_name if organization?
return organization.name if organization?
"#{first_name} #{last_name}"
end
@@ -25,25 +31,15 @@ class User < ActiveRecord::Base
end
def administrator?
@is_administrator ||= Administrator.where(user_id: id).exists?
administrator.present?
end
def moderator?
@is_moderator ||= Moderator.where(user_id: id).exists?
moderator.present?
end
def organization?
organization_name.present?
end
def verified_organization?
organization_verified_at.present? &&
(organization_rejected_at.blank? || organization_rejected_at < organization_verified_at)
end
def rejected_organization?
organization_rejected_at.present? &&
(organization_verified_at.blank? || organization_verified_at < organization_rejected_at)
organization.present?
end
private

View File

@@ -112,64 +112,26 @@ describe User do
end
describe "organization?" do
it "is false when organization_name is blank" do
it "is false when the user is not an organization" do
expect(subject.organization?).to be false
end
it "is true when organization_name exists" do
subject.organization_name = "Anonymous"
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
subject.organization_name = "The A Team"
expect(subject).to be_valid
end
describe 'when it is an organization' do
before(:each) { create(:organization, user: subject) }
it "calculates the name using the organization name" do
subject.organization_name = "The A Team"
expect(subject.name).to eq("The A Team")
end
end
it "is true when the user is an organization" do
expect(subject.organization?).to be true
end
describe "verified_organization?" do
it "is false when organization_verified_at? is blank" do
expect(subject.verified_organization?).to be false
end
it "is true when organization_verified_at? exists" do
subject.organization_verified_at = Time.now
expect(subject.verified_organization?).to be true
end
it "is false when the organization was verified and then rejected" do
subject.organization_verified_at = Time.now
subject.organization_rejected_at = Time.now + 1
expect(subject.verified_organization?).to be false
end
it "is true when the organization was rejected and then verified" do
subject.organization_rejected_at = Time.now
subject.organization_verified_at = Time.now + 1
expect(subject.verified_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
describe "rejected_organization?" do
it "is false when organization_rejected_at? is blank" do
expect(subject.rejected_organization?).to be false
end
it "is true when organization_rejected_at? exists" do
subject.organization_rejected_at = Time.now
expect(subject.rejected_organization?).to be true
end
it "is true when the organization was verified and then rejected" do
subject.organization_verified_at = Time.now
subject.organization_rejected_at = Time.now + 1
expect(subject.rejected_organization?).to be true
end
it "is false when the organization was rejected and then verified" do
subject.organization_rejected_at = Time.now
subject.organization_verified_at = Time.now + 1
expect(subject.rejected_organization?).to be false
it "calculates the name using the organization name" do
expect(subject.name).to eq(subject.organization.name)
end
end
end