Refactoring to use scope (#15625)
* Refactoring to use scope Prior to this commit, four methods had similar purpose but different implementations. This commit normalizes that and extracts a few `ActiveRecord::Base` scopes. Related to #15624 * Fixing typo and tests
This commit is contained in:
parent
e1bddb5bf8
commit
4efcf8c3d3
2 changed files with 7 additions and 4 deletions
|
|
@ -14,6 +14,9 @@ class OrganizationMembership < ApplicationRecord
|
|||
after_create :update_user_organization_info_updated_at
|
||||
after_destroy :update_user_organization_info_updated_at
|
||||
|
||||
scope :admin, -> { where(type_of_user: "admin") }
|
||||
scope :member, -> { where(type_of_user: %w[admin member]) }
|
||||
|
||||
# @note In the case where we delete the user, we don't need to worry
|
||||
# about updating the user. Hence the the `user has_many
|
||||
# :organization_memberships dependent: :delete_all`
|
||||
|
|
|
|||
|
|
@ -402,21 +402,21 @@ class User < ApplicationRecord
|
|||
end
|
||||
|
||||
def admin_organizations
|
||||
org_ids = organization_memberships.where(type_of_user: "admin").pluck(:organization_id)
|
||||
org_ids = organization_memberships.admin.pluck(:organization_id)
|
||||
organizations.where(id: org_ids)
|
||||
end
|
||||
|
||||
def member_organizations
|
||||
org_ids = organization_memberships.where(type_of_user: %w[admin member]).pluck(:organization_id)
|
||||
org_ids = organization_memberships.member.pluck(:organization_id)
|
||||
organizations.where(id: org_ids)
|
||||
end
|
||||
|
||||
def org_member?(organization)
|
||||
OrganizationMembership.exists?(user: self, organization: organization, type_of_user: %w[admin member])
|
||||
organization_memberships.member.exists?(organization: organization)
|
||||
end
|
||||
|
||||
def org_admin?(organization)
|
||||
OrganizationMembership.exists?(user: self, organization: organization, type_of_user: "admin")
|
||||
organization_memberships.admin.exists?(organization: organization)
|
||||
end
|
||||
|
||||
def block; end
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue