Commit b904d5530f1893028541fe0202eb30f083816918

Authored by Leandro Santos
2 parents a3fb6283 957db004

Merge branch 'refatoring_visible_for_person' into 'master'

refactoring visible for person query

transform visible_for_person in named scope and make some changes on generated query

See merge request !995
Showing 1 changed file with 13 additions and 18 deletions   Show diff stats
app/models/organization.rb
... ... @@ -20,33 +20,28 @@ class Organization < Profile
20 20 # visible.
21 21 # 4) The user is not a member of the organization but the organization is
22 22 # visible, public and enabled.
23   - def self.listed_for_person(person)
  23 + scope :listed_for_person, lambda { |person|
  24 +
24 25 joins('LEFT JOIN "role_assignments" ON ("role_assignments"."resource_id" = "profiles"."id"
25 26 AND "role_assignments"."resource_type" = \'Profile\') OR (
26 27 "role_assignments"."resource_id" = "profiles"."environment_id" AND
27 28 "role_assignments"."resource_type" = \'Environment\' )')
28 29 .joins('LEFT JOIN "roles" ON "role_assignments"."role_id" = "roles"."id"')
29 30 .where(
30   - ['( (roles.key = ? OR roles.key = ?) AND role_assignments.accessor_type = ? AND role_assignments.accessor_id = ? )
31   - OR
32   - ( ( ( role_assignments.accessor_type = ? AND role_assignments.accessor_id = ? ) OR
33   - ( profiles.enabled = ? ) ) AND
34   - ( profiles.visible = ? ) )',
35   - 'profile_admin', 'environment_administrator', Profile.name, person.id,
36   - Profile.name, person.id, true, true]
37   - ).uniq
38   - end
39   -
40   - def self.visible_for_person(person)
41   - listed_for_person(person).where(
42   - ['( (roles.key = ? OR roles.key = ?) AND role_assignments.accessor_type = ? AND role_assignments.accessor_id = ? )
43   - OR
  31 + ['( (roles.key = ? OR roles.key = ?) AND role_assignments.accessor_type = ? AND role_assignments.accessor_id = ? ) OR (
  32 + ( ( role_assignments.accessor_type = ? AND role_assignments.accessor_id = ? ) OR ( profiles.enabled = ?)) AND (profiles.visible = ?) )',
  33 + 'profile_admin', 'environment_administrator', Profile.name, person.id, Profile.name, person.id, true, true]
  34 + ).uniq
  35 + }
  36 +
  37 + scope :visible_for_person, lambda { |person|
  38 + listed_for_person(person).where( ['
44 39 ( ( role_assignments.accessor_type = ? AND role_assignments.accessor_id = ? ) OR
45   - ( profiles.enabled = ? AND profiles.public_profile = ? ) )',
46   - 'profile_admin', 'environment_administrator', Profile.name, person.id,
  40 + ( profiles.enabled = ? AND profiles.public_profile = ? ) )',
47 41 Profile.name, person.id, true, true]
48 42 )
49   - end
  43 + }
  44 +
50 45  
51 46 settings_items :closed, :type => :boolean, :default => false
52 47 def closed?
... ...