Commit 32788f40f920f1745e541a7cf7c4b04826701119
1 parent
1eeb24c6
Exists in
refactor_with_role
efactor with_role person scope to works properly as rails4 scope
Showing
4 changed files
with
108 additions
and
10 deletions
Show diff stats
app/models/person.rb
... | ... | @@ -129,6 +129,11 @@ class Person < Profile |
129 | 129 | scope :activated, -> { joins(:user).where('users.activation_code IS NULL AND users.activated_at IS NOT NULL') } |
130 | 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 | 137 | after_destroy do |person| |
133 | 138 | Friendship.where(friend_id: person.id).each{ |friendship| friendship.destroy } |
134 | 139 | end | ... | ... |
plugins/people_block/lib/ext/person.rb
plugins/people_block/test/unit/members_block_test.rb
... | ... | @@ -272,6 +272,21 @@ class MembersBlockTest < ActionView::TestCase |
272 | 272 | assert_includes block.roles, Profile::Roles.moderator(owner.environment.id) |
273 | 273 | end |
274 | 274 | |
275 | + should 'count number of profiles by role' do | |
276 | + owner = fast_create(Community) | |
277 | + profile1 = fast_create(Person, {:public_profile => true}) | |
278 | + profile2 = fast_create(Person, {:public_profile => true}) | |
279 | + | |
280 | + owner.add_member profile2 | |
281 | + owner.add_moderator profile1 | |
282 | + | |
283 | + block = MembersBlock.new | |
284 | + block.visible_role = Profile::Roles.moderator(owner.environment.id).key | |
285 | + block.expects(:owner).returns(owner).at_least_once | |
286 | + | |
287 | + assert_equal 1, block.profile_count | |
288 | + end | |
289 | + | |
275 | 290 | protected |
276 | 291 | include NoosferoTestHelper |
277 | 292 | ... | ... |
test/unit/person_test.rb
... | ... | @@ -1838,4 +1838,92 @@ class PersonTest < ActiveSupport::TestCase |
1838 | 1838 | assert_equivalent [c1,c3], p1.comments |
1839 | 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 | 1929 | end | ... | ... |