Commit c48222e1df5f3d8b0e156f993e923d5250bbb7d4
Committed by
Rodrigo Souto
1 parent
3d0b536a
Exists in
master
and in
12 other branches
Refactor with_role person scope to works properly with rails4
Signed-off-by: Leandro Nunes dos Santos <leandronunes@gmail.com> Signed-off-by: Lucas Kanashiro <kanashiro.duarte@gmail.com> Signed-off-by: Gustavo Jaruga Cruz <darksshades@gmail.com>
Showing
4 changed files
with
115 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 | } |
@@ -129,6 +129,11 @@ class Person < Profile | @@ -129,6 +129,11 @@ class Person < Profile | ||
129 | scope :activated, -> { joins(:user).where('users.activation_code IS NULL AND users.activated_at IS NOT NULL') } | 129 | scope :activated, -> { joins(:user).where('users.activation_code IS NULL AND users.activated_at IS NOT NULL') } |
130 | scope :deactivated, -> { joins(:user).where('NOT (users.activation_code IS NULL AND users.activated_at IS NOT NULL)') } | 130 | scope :deactivated, -> { joins(:user).where('NOT (users.activation_code IS NULL AND users.activated_at IS NOT NULL)') } |
131 | 131 | ||
132 | + scope :with_role, -> role_id { | ||
133 | + distinct.joins(:role_assignments). | ||
134 | + where("role_assignments.role_id = #{role_id}") | ||
135 | + } | ||
136 | + | ||
132 | after_destroy do |person| | 137 | after_destroy do |person| |
133 | Friendship.where(friend_id: person.id).each{ |friendship| friendship.destroy } | 138 | Friendship.where(friend_id: person.id).each{ |friendship| friendship.destroy } |
134 | end | 139 | end |
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 | } |
plugins/people_block/test/unit/members_block_test.rb
@@ -280,6 +280,21 @@ class MembersBlockTest < ActionView::TestCase | @@ -280,6 +280,21 @@ class MembersBlockTest < ActionView::TestCase | ||
280 | assert_includes block.roles, Profile::Roles.moderator(owner.environment.id) | 280 | assert_includes block.roles, Profile::Roles.moderator(owner.environment.id) |
281 | end | 281 | end |
282 | 282 | ||
283 | + should 'count number of profiles by role' do | ||
284 | + owner = fast_create(Community) | ||
285 | + profile1 = fast_create(Person, {:public_profile => true}) | ||
286 | + profile2 = fast_create(Person, {:public_profile => true}) | ||
287 | + | ||
288 | + owner.add_member profile2 | ||
289 | + owner.add_moderator profile1 | ||
290 | + | ||
291 | + block = MembersBlock.new | ||
292 | + block.visible_role = Profile::Roles.moderator(owner.environment.id).key | ||
293 | + block.expects(:owner).returns(owner).at_least_once | ||
294 | + | ||
295 | + assert_equivalent [profile1], block.profile_list | ||
296 | + end | ||
297 | + | ||
283 | protected | 298 | protected |
284 | include NoosferoTestHelper | 299 | include NoosferoTestHelper |
285 | 300 |
test/unit/person_test.rb
@@ -1838,4 +1838,92 @@ class PersonTest < ActiveSupport::TestCase | @@ -1838,4 +1838,92 @@ class PersonTest < ActiveSupport::TestCase | ||
1838 | assert_equivalent [c1,c3], p1.comments | 1838 | assert_equivalent [c1,c3], p1.comments |
1839 | end | 1839 | end |
1840 | 1840 | ||
1841 | + should 'get people of one community by moderator role' do | ||
1842 | + community = fast_create(Community) | ||
1843 | + p1 = fast_create(Person) | ||
1844 | + p2 = fast_create(Person) | ||
1845 | + | ||
1846 | + community.add_member p1 | ||
1847 | + community.add_moderator p2 | ||
1848 | + | ||
1849 | + assert_equivalent [p2], Person.with_role(Profile::Roles.moderator(community.environment.id).id) | ||
1850 | + end | ||
1851 | + | ||
1852 | + should 'get people of one community by admin role' do | ||
1853 | + community = fast_create(Community) | ||
1854 | + p1 = fast_create(Person) | ||
1855 | + p2 = fast_create(Person) | ||
1856 | + | ||
1857 | + community.add_admin p1 | ||
1858 | + community.add_member p2 | ||
1859 | + | ||
1860 | + assert_equivalent [p1], Person.with_role(Profile::Roles.admin(community.environment.id).id) | ||
1861 | + end | ||
1862 | + | ||
1863 | + should 'get people with admin role of any community' do | ||
1864 | + c1 = fast_create(Community) | ||
1865 | + p1 = fast_create(Person) | ||
1866 | + p2 = fast_create(Person) | ||
1867 | + c1.add_admin p1 | ||
1868 | + c1.add_member p2 | ||
1869 | + | ||
1870 | + c2 = fast_create(Community) | ||
1871 | + p3 = fast_create(Person) | ||
1872 | + p4 = fast_create(Person) | ||
1873 | + | ||
1874 | + c2.add_admin p4 | ||
1875 | + c2.add_member p3 | ||
1876 | + | ||
1877 | + assert_equivalent [p1, p4], Person.with_role(Profile::Roles.admin(c1.environment.id).id) | ||
1878 | + end | ||
1879 | + | ||
1880 | + should 'get distinct people with moderator role of any community' do | ||
1881 | + c1 = fast_create(Community) | ||
1882 | + p1 = fast_create(Person) | ||
1883 | + p2 = fast_create(Person) | ||
1884 | + c1.add_member p1 | ||
1885 | + c1.add_moderator p2 | ||
1886 | + | ||
1887 | + c2 = fast_create(Community) | ||
1888 | + p3 = fast_create(Person) | ||
1889 | + p4 = fast_create(Person) | ||
1890 | + | ||
1891 | + c2.add_member p4 | ||
1892 | + c2.add_moderator p3 | ||
1893 | + c2.add_moderator p2 | ||
1894 | + | ||
1895 | + assert_equivalent [p2, p3], Person.with_role(Profile::Roles.moderator(c1.environment.id).id) | ||
1896 | + end | ||
1897 | + | ||
1898 | + should 'count members of a community collected by moderator' do | ||
1899 | + c1 = fast_create(Community) | ||
1900 | + p1 = fast_create(Person) | ||
1901 | + p2 = fast_create(Person) | ||
1902 | + p3 = fast_create(Person) | ||
1903 | + c1.add_member p1 | ||
1904 | + c1.add_moderator p2 | ||
1905 | + c1.add_member p3 | ||
1906 | + | ||
1907 | + assert_equal 1, c1.members.with_role(Profile::Roles.moderator(c1.environment.id).id).count | ||
1908 | + end | ||
1909 | + | ||
1910 | + should 'count people of any community collected by moderator' do | ||
1911 | + c1 = fast_create(Community) | ||
1912 | + p1 = fast_create(Person) | ||
1913 | + p2 = fast_create(Person) | ||
1914 | + c1.add_member p1 | ||
1915 | + c1.add_moderator p2 | ||
1916 | + | ||
1917 | + c2 = fast_create(Community) | ||
1918 | + p3 = fast_create(Person) | ||
1919 | + p4 = fast_create(Person) | ||
1920 | + | ||
1921 | + c2.add_member p4 | ||
1922 | + c2.add_moderator p3 | ||
1923 | + c2.add_moderator p2 | ||
1924 | + | ||
1925 | + assert_equal 2, Person.with_role(Profile::Roles.moderator(c1.environment.id).id).count | ||
1926 | + end | ||
1927 | + | ||
1928 | + | ||
1841 | end | 1929 | end |