grouped_scope 0.6.1 → 3.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +5 -0
- data/CHANGELOG +13 -0
- data/Gemfile +4 -6
- data/README.md +173 -0
- data/lib/grouped_scope.rb +7 -7
- data/lib/grouped_scope/arish/associations/association_scope.rb +90 -0
- data/lib/grouped_scope/arish/associations/builder/grouped_association.rb +50 -0
- data/lib/grouped_scope/arish/associations/builder/grouped_collection_association.rb +32 -0
- data/lib/grouped_scope/arish/associations/collection_association.rb +25 -0
- data/lib/grouped_scope/arish/base.rb +24 -0
- data/lib/grouped_scope/arish/reflection.rb +18 -0
- data/lib/grouped_scope/arish/relation/predicate_builer.rb +27 -0
- data/lib/grouped_scope/errors.rb +2 -3
- data/lib/grouped_scope/self_grouping.rb +59 -23
- data/lib/grouped_scope/version.rb +2 -4
- data/test/cases/has_many_test.rb +155 -0
- data/test/cases/has_many_through_test.rb +51 -0
- data/test/cases/reflection_test.rb +62 -0
- data/test/cases/self_grouping_test.rb +201 -0
- data/test/helper.rb +48 -35
- metadata +27 -30
- data/README.rdoc +0 -98
- data/lib/grouped_scope/association_reflection.rb +0 -54
- data/lib/grouped_scope/class_methods.rb +0 -32
- data/lib/grouped_scope/core_ext.rb +0 -29
- data/lib/grouped_scope/grouping.rb +0 -9
- data/lib/grouped_scope/has_many_association.rb +0 -28
- data/lib/grouped_scope/has_many_through_association.rb +0 -28
- data/lib/grouped_scope/instance_methods.rb +0 -10
- data/test/grouped_scope/association_reflection_test.rb +0 -73
- data/test/grouped_scope/class_methods_test.rb +0 -51
- data/test/grouped_scope/has_many_association_test.rb +0 -156
- data/test/grouped_scope/has_many_through_association_test.rb +0 -51
- data/test/grouped_scope/self_grouping_test.rb +0 -146
@@ -1,51 +0,0 @@
|
|
1
|
-
require 'helper'
|
2
|
-
|
3
|
-
class GroupedScope::HasManyThroughAssociationTest < GroupedScope::TestCase
|
4
|
-
|
5
|
-
setup do
|
6
|
-
setup_environment
|
7
|
-
@e1 = FactoryGirl.create(:employee, :group_id => 1)
|
8
|
-
@e1.departments << Department.hr << Department.finance
|
9
|
-
@e2 = FactoryGirl.create(:employee, :group_id => 1)
|
10
|
-
@e2.departments << Department.it
|
11
|
-
@all_group_departments = [Department.hr, Department.it, Department.finance]
|
12
|
-
end
|
13
|
-
|
14
|
-
context 'For default association' do
|
15
|
-
|
16
|
-
should 'scope to owner' do
|
17
|
-
assert_sql(/employee_id = #{@e1.id}/) do
|
18
|
-
@e1.departments(true)
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
should 'scope count to owner' do
|
23
|
-
assert_sql(/employee_id = #{@e1.id}/) do
|
24
|
-
@e1.departments(true).count
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
end
|
29
|
-
|
30
|
-
context 'For grouped association' do
|
31
|
-
|
32
|
-
should 'scope to group' do
|
33
|
-
assert_sql(/employee_id IN \(#{@e1.id},#{@e2.id}\)/) do
|
34
|
-
@e2.group.departments(true)
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
should 'scope count to group' do
|
39
|
-
assert_sql(/employee_id IN \(#{@e1.id},#{@e2.id}\)/) do
|
40
|
-
@e1.group.departments(true).count
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
should 'have a group count equal to sum of seperate owner counts' do
|
45
|
-
assert_equal @e1.departments(true).count + @e2.departments(true).count, @e2.group.departments(true).count
|
46
|
-
end
|
47
|
-
|
48
|
-
end
|
49
|
-
|
50
|
-
|
51
|
-
end
|
@@ -1,146 +0,0 @@
|
|
1
|
-
require 'helper'
|
2
|
-
|
3
|
-
class GroupedScope::SelfGrouppingTest < GroupedScope::TestCase
|
4
|
-
|
5
|
-
setup do
|
6
|
-
setup_environment
|
7
|
-
end
|
8
|
-
|
9
|
-
context 'General behavior' do
|
10
|
-
|
11
|
-
setup do
|
12
|
-
@employee = FactoryGirl.create(:employee)
|
13
|
-
end
|
14
|
-
|
15
|
-
should 'return an array' do
|
16
|
-
assert_instance_of Array, @employee.group
|
17
|
-
end
|
18
|
-
|
19
|
-
should 'return #ids array' do
|
20
|
-
assert_equal [@employee.id], @employee.group.ids
|
21
|
-
end
|
22
|
-
|
23
|
-
should 'return #quoted_ids string for use in sql statments' do
|
24
|
-
assert_equal "#{@employee.id}", @employee.group.quoted_ids
|
25
|
-
end
|
26
|
-
|
27
|
-
should 'respond true to grouped associations' do
|
28
|
-
assert @employee.group.respond_to?(:reports)
|
29
|
-
end
|
30
|
-
|
31
|
-
should 'raise a GroupedScope::NoGroupIdError exception for objects with no group_id schema' do
|
32
|
-
FooBar.column_names.wont_include 'group_id'
|
33
|
-
lambda{ GroupedScope::SelfGroupping.new(FooBar.new) }.must_raise(GroupedScope::NoGroupIdError)
|
34
|
-
end
|
35
|
-
|
36
|
-
should 'return correct attribute_condition for GroupedScope::SelfGroupping object' do
|
37
|
-
assert_sql(/"?group_id"? IN \(#{@employee.id}\)/) do
|
38
|
-
Employee.find :all, :conditions => {:group_id => @employee.group}
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
context 'for Array delegates' do
|
43
|
-
|
44
|
-
should 'respond to first/last' do
|
45
|
-
[:first,:last].each do |method|
|
46
|
-
assert @employee.group.respond_to?(method), "Should respond to #{method.inspect}"
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
should 'respond to each' do
|
51
|
-
assert @employee.group.respond_to?(:each)
|
52
|
-
@employee.group.each do |employee|
|
53
|
-
assert_instance_of Employee, employee
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
end
|
58
|
-
|
59
|
-
end
|
60
|
-
|
61
|
-
context 'Calling #group' do
|
62
|
-
|
63
|
-
should 'return an array' do
|
64
|
-
assert_instance_of Array, FactoryGirl.create(:employee).group
|
65
|
-
end
|
66
|
-
|
67
|
-
context 'with a NIL group_id' do
|
68
|
-
|
69
|
-
setup do
|
70
|
-
@employee = FactoryGirl.create(:employee)
|
71
|
-
end
|
72
|
-
|
73
|
-
should 'return a collection of one' do
|
74
|
-
assert_equal 1, @employee.group.size
|
75
|
-
end
|
76
|
-
|
77
|
-
should 'include self in group' do
|
78
|
-
assert @employee.group.include?(@employee)
|
79
|
-
end
|
80
|
-
|
81
|
-
end
|
82
|
-
|
83
|
-
context 'with a set group_id' do
|
84
|
-
|
85
|
-
setup do
|
86
|
-
@employee = FactoryGirl.create(:employee, :group_id => 1)
|
87
|
-
end
|
88
|
-
|
89
|
-
should 'return a collection of one' do
|
90
|
-
assert_equal 1, @employee.group.size
|
91
|
-
end
|
92
|
-
|
93
|
-
should 'include self in group' do
|
94
|
-
assert @employee.group.include?(@employee)
|
95
|
-
end
|
96
|
-
|
97
|
-
end
|
98
|
-
|
99
|
-
context 'with different groups available' do
|
100
|
-
|
101
|
-
setup do
|
102
|
-
@e1 = FactoryGirl.create(:employee_with_reports, :group_id => 1)
|
103
|
-
@e2 = FactoryGirl.create(:employee, :group_id => 1)
|
104
|
-
@e3 = FactoryGirl.create(:employee_with_reports, :group_id => 2)
|
105
|
-
@e4 = FactoryGirl.create(:employee, :group_id => 2)
|
106
|
-
end
|
107
|
-
|
108
|
-
should 'return a collection of group members' do
|
109
|
-
assert_equal 2, @e1.group.size
|
110
|
-
end
|
111
|
-
|
112
|
-
should 'include all group members' do
|
113
|
-
assert_same_elements [@e1,@e2], @e1.group
|
114
|
-
end
|
115
|
-
|
116
|
-
should 'allow member to find grouped associations of other member' do
|
117
|
-
assert_same_elements @e1.reports, @e2.group.reports
|
118
|
-
end
|
119
|
-
|
120
|
-
should 'allow proxy owner to define all grouped which ignores group_id schema' do
|
121
|
-
@e2.stubs(:all_grouped?).returns(true)
|
122
|
-
assert_same_elements [@e1,@e2,@e3,@e4], @e2.group
|
123
|
-
assert_same_elements @e1.reports + @e3.reports, @e2.group.reports
|
124
|
-
end
|
125
|
-
|
126
|
-
end
|
127
|
-
|
128
|
-
context 'with different groups in legacy schema' do
|
129
|
-
|
130
|
-
setup do
|
131
|
-
@e1 = FactoryGirl.create(:legacy_employee_with_reports, :group_id => 1)
|
132
|
-
@e2 = FactoryGirl.create(:legacy_employee, :group_id => 1)
|
133
|
-
@e3 = FactoryGirl.create(:legacy_employee_with_reports, :group_id => 2)
|
134
|
-
@e4 = FactoryGirl.create(:legacy_employee, :group_id => 2)
|
135
|
-
end
|
136
|
-
|
137
|
-
should 'honor legacy reports association options like class_name and foreign_key' do
|
138
|
-
@e2.group.reports.all? { |r| r.is_a?(LegacyReport) }
|
139
|
-
end
|
140
|
-
|
141
|
-
end
|
142
|
-
|
143
|
-
end
|
144
|
-
|
145
|
-
|
146
|
-
end
|