Updates User model, moving org stuff to Organisation model
This commit is contained in:
@@ -8,11 +8,17 @@ class User < ActiveRecord::Base
|
|||||||
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?
|
||||||
|
|
||||||
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
|
def name
|
||||||
return nickname if use_nickname?
|
return nickname if use_nickname?
|
||||||
return organization_name if organization?
|
return organization.name if organization?
|
||||||
"#{first_name} #{last_name}"
|
"#{first_name} #{last_name}"
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -25,25 +31,15 @@ class User < ActiveRecord::Base
|
|||||||
end
|
end
|
||||||
|
|
||||||
def administrator?
|
def administrator?
|
||||||
@is_administrator ||= Administrator.where(user_id: id).exists?
|
administrator.present?
|
||||||
end
|
end
|
||||||
|
|
||||||
def moderator?
|
def moderator?
|
||||||
@is_moderator ||= Moderator.where(user_id: id).exists?
|
moderator.present?
|
||||||
end
|
end
|
||||||
|
|
||||||
def organization?
|
def organization?
|
||||||
organization_name.present?
|
organization.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)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|||||||
@@ -112,64 +112,26 @@ describe User do
|
|||||||
end
|
end
|
||||||
|
|
||||||
describe "organization?" do
|
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
|
expect(subject.organization?).to be false
|
||||||
end
|
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
|
describe 'when it is an organization' do
|
||||||
subject.first_name = nil
|
before(:each) { create(:organization, user: subject) }
|
||||||
subject.last_name = nil
|
|
||||||
subject.organization_name = "The A Team"
|
|
||||||
expect(subject).to be_valid
|
|
||||||
end
|
|
||||||
|
|
||||||
it "calculates the name using the organization name" do
|
it "is true when the user is an organization" do
|
||||||
subject.organization_name = "The A Team"
|
expect(subject.organization?).to be true
|
||||||
expect(subject.name).to eq("The A Team")
|
end
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
describe "verified_organization?" do
|
it "deactivates the validation of first_name and last_name" do
|
||||||
it "is false when organization_verified_at? is blank" do
|
subject.first_name = nil
|
||||||
expect(subject.verified_organization?).to be false
|
subject.last_name = nil
|
||||||
end
|
expect(subject).to be_valid
|
||||||
it "is true when organization_verified_at? exists" do
|
end
|
||||||
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
|
|
||||||
|
|
||||||
describe "rejected_organization?" do
|
it "calculates the name using the organization name" do
|
||||||
it "is false when organization_rejected_at? is blank" do
|
expect(subject.name).to eq(subject.organization.name)
|
||||||
expect(subject.rejected_organization?).to be false
|
end
|
||||||
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
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user