Commit cb052135e3c65051f7185065a459ac55dd154a84

Authored by Leandro Santos
1 parent 32788f40
Exists in refactor_with_role

avoid double distinct clause in queries

Showing 2 changed files with 7 additions and 7 deletions   Show diff stats
app/models/person.rb
@@ -19,23 +19,23 @@ class Person < Profile @@ -19,23 +19,23 @@ class Person < Profile
19 scope :members_of, -> resources { 19 scope :members_of, -> resources {
20 resources = Array(resources) 20 resources = Array(resources)
21 conditions = resources.map {|resource| "role_assignments.resource_type = '#{resource.class.base_class.name}' AND role_assignments.resource_id = #{resource.id || -1}"}.join(' OR ') 21 conditions = resources.map {|resource| "role_assignments.resource_type = '#{resource.class.base_class.name}' AND role_assignments.resource_id = #{resource.id || -1}"}.join(' OR ')
22 - select('DISTINCT profiles.*').joins(:role_assignments).where([conditions]) 22 + distinct.select('profiles.*').joins(:role_assignments).where([conditions])
23 } 23 }
24 24
25 scope :not_members_of, -> resources { 25 scope :not_members_of, -> resources {
26 resources = Array(resources) 26 resources = Array(resources)
27 conditions = resources.map {|resource| "role_assignments.resource_type = '#{resource.class.base_class.name}' AND role_assignments.resource_id = #{resource.id || -1}"}.join(' OR ') 27 conditions = resources.map {|resource| "role_assignments.resource_type = '#{resource.class.base_class.name}' AND role_assignments.resource_id = #{resource.id || -1}"}.join(' OR ')
28 - select('DISTINCT profiles.*').where('"profiles"."id" NOT IN (SELECT DISTINCT profiles.id FROM "profiles" INNER JOIN "role_assignments" ON "role_assignments"."accessor_id" = "profiles"."id" AND "role_assignments"."accessor_type" = (Profile) WHERE "profiles"."type" IN (Person) AND (%s))' % conditions) 28 + distinct.select('profiles.*').where('"profiles"."id" NOT IN (SELECT DISTINCT profiles.id FROM "profiles" INNER JOIN "role_assignments" ON "role_assignments"."accessor_id" = "profiles"."id" AND "role_assignments"."accessor_type" = (Profile) WHERE "profiles"."type" IN (Person) AND (%s))' % conditions)
29 } 29 }
30 30
31 scope :by_role, -> roles { 31 scope :by_role, -> roles {
32 roles = Array(roles) 32 roles = Array(roles)
33 - select('DISTINCT profiles.*').joins(:role_assignments).where('role_assignments.role_id IN (?)', roles) 33 + distinct.select('profiles.*').joins(:role_assignments).where('role_assignments.role_id IN (?)', roles)
34 } 34 }
35 35
36 scope :not_friends_of, -> resources { 36 scope :not_friends_of, -> resources {
37 resources = Array(resources) 37 resources = Array(resources)
38 - select('DISTINCT profiles.*').where('"profiles"."id" NOT IN (SELECT DISTINCT profiles.id FROM "profiles" INNER JOIN "friendships" ON "friendships"."person_id" = "profiles"."id" WHERE "friendships"."friend_id" IN (%s))' % resources.map(&:id)) 38 + distinct.select('profiles.*').where('"profiles"."id" NOT IN (SELECT DISTINCT profiles.id FROM "profiles" INNER JOIN "friendships" ON "friendships"."person_id" = "profiles"."id" WHERE "friendships"."friend_id" IN (%s))' % resources.map(&:id))
39 } 39 }
40 40
41 scope :visible_for_person, lambda { |person| 41 scope :visible_for_person, lambda { |person|
@@ -117,10 +117,10 @@ class Person < Profile @@ -117,10 +117,10 @@ class Person < Profile
117 scope :more_popular, -> { order 'friends_count DESC' } 117 scope :more_popular, -> { order 'friends_count DESC' }
118 118
119 scope :abusers, -> { 119 scope :abusers, -> {
120 - joins(:abuse_complaints).where('tasks.status = 3').select('DISTINCT profiles.*') 120 + joins(:abuse_complaints).where('tasks.status = 3').distinct.select('profiles.*')
121 } 121 }
122 scope :non_abusers, -> { 122 scope :non_abusers, -> {
123 - select("DISTINCT profiles.*"). 123 + distinct.select("profiles.*").
124 joins("LEFT JOIN tasks ON profiles.id = tasks.requestor_id AND tasks.type='AbuseComplaint'"). 124 joins("LEFT JOIN tasks ON profiles.id = tasks.requestor_id AND tasks.type='AbuseComplaint'").
125 where("tasks.status != 3 OR tasks.id is NULL") 125 where("tasks.status != 3 OR tasks.id is NULL")
126 } 126 }
app/models/profile.rb
@@ -89,7 +89,7 @@ class Profile < ActiveRecord::Base @@ -89,7 +89,7 @@ class Profile < ActiveRecord::Base
89 include Noosfero::Plugin::HotSpot 89 include Noosfero::Plugin::HotSpot
90 90
91 scope :memberships_of, -> person { 91 scope :memberships_of, -> person {
92 - select('DISTINCT profiles.*'). 92 + distinct.select('profiles.*').
93 joins(:role_assignments). 93 joins(:role_assignments).
94 where('role_assignments.accessor_type = ? AND role_assignments.accessor_id = ?', person.class.base_class.name, person.id) 94 where('role_assignments.accessor_type = ? AND role_assignments.accessor_id = ?', person.class.base_class.name, person.id)
95 } 95 }