|
87 | 87 | end
|
88 | 88 |
|
89 | 89 | describe '#association_relation' do
|
90 |
| - if rails_4_x? |
91 |
| - context 'when the reflection object has a #scope method' do |
92 |
| - context 'when the scope is a block' do |
93 |
| - it 'executes the block in the context of an empty scope' do |
94 |
| - define_model(:country, mood: :string) |
95 |
| - person_model = define_model(:person, country_id: :integer) do |
96 |
| - belongs_to :country, -> { where(mood: 'nice') } |
97 |
| - end |
98 |
| - delegate_reflection = person_model.reflect_on_association(:country) |
99 |
| - reflection = described_class.new(delegate_reflection) |
100 |
| - |
101 |
| - actual_sql = reflection.association_relation.to_sql |
102 |
| - expected_sql = Country.where(mood: 'nice').to_sql |
103 |
| - expect(actual_sql).to eq expected_sql |
| 90 | + context 'when the reflection object has a #scope method' do |
| 91 | + context 'when the scope is a block' do |
| 92 | + it 'executes the block in the context of an empty scope' do |
| 93 | + define_model(:country, mood: :string) |
| 94 | + person_model = define_model(:person, country_id: :integer) do |
| 95 | + belongs_to :country, -> { where(mood: 'nice') } |
104 | 96 | end
|
105 |
| - end |
| 97 | + delegate_reflection = person_model.reflect_on_association(:country) |
| 98 | + reflection = described_class.new(delegate_reflection) |
106 | 99 |
|
107 |
| - context 'when the scope is nil' do |
108 |
| - it 'returns an empty scope' do |
109 |
| - define_model(:country) |
110 |
| - person_model = define_model(:person, country_id: :integer) do |
111 |
| - belongs_to :country |
112 |
| - end |
113 |
| - delegate_reflection = person_model.reflect_on_association(:country) |
114 |
| - reflection = described_class.new(delegate_reflection) |
115 |
| - |
116 |
| - actual_sql = reflection.association_relation.to_sql |
117 |
| - expected_sql = Country.all.to_sql |
118 |
| - expect(actual_sql).to eq expected_sql |
119 |
| - end |
| 100 | + actual_sql = reflection.association_relation.to_sql |
| 101 | + expected_sql = Country.where(mood: 'nice').to_sql |
| 102 | + expect(actual_sql).to eq expected_sql |
120 | 103 | end
|
121 | 104 | end
|
122 |
| - end |
123 |
| - |
124 |
| - if rails_3_x? |
125 |
| - context 'when the reflection object does not have a #scope method' do |
126 |
| - context 'when the reflection options contain :conditions' do |
127 |
| - it 'creates an ActiveRecord::Relation from the conditions' do |
128 |
| - define_model(:country, mood: :string) |
129 |
| - person_model = define_model(:person, country_id: :integer) do |
130 |
| - belongs_to :country, conditions: { mood: 'nice' } |
131 |
| - end |
132 |
| - delegate_reflection = person_model.reflect_on_association(:country) |
133 |
| - reflection = described_class.new(delegate_reflection) |
134 |
| - |
135 |
| - actual_sql = reflection.association_relation.to_sql |
136 |
| - expected_sql = Country.where(mood: 'nice').to_sql |
137 |
| - expect(actual_sql).to eq expected_sql |
138 |
| - end |
139 |
| - end |
140 | 105 |
|
141 |
| - context 'when the reflection options contain :order' do |
142 |
| - it 'creates an ActiveRecord::Relation from the order' do |
143 |
| - define_model(:person, country_id: :integer, age: :integer) |
144 |
| - country_model = define_model(:country) do |
145 |
| - has_many :people, order: 'age' |
146 |
| - end |
147 |
| - delegate_reflection = country_model.reflect_on_association(:people) |
148 |
| - reflection = described_class.new(delegate_reflection) |
149 |
| - |
150 |
| - actual_sql = reflection.association_relation.to_sql |
151 |
| - expected_sql = Person.order('age').to_sql |
152 |
| - expect(actual_sql).to eq expected_sql |
| 106 | + context 'when the scope is nil' do |
| 107 | + it 'returns an empty scope' do |
| 108 | + define_model(:country) |
| 109 | + person_model = define_model(:person, country_id: :integer) do |
| 110 | + belongs_to :country |
153 | 111 | end
|
154 |
| - end |
155 |
| - |
156 |
| - context 'when the reflection options contain :include' do |
157 |
| - it 'creates an ActiveRecord::Relation from the include' do |
158 |
| - define_model(:city, country_id: :integer) |
159 |
| - define_model(:country) do |
160 |
| - has_many :cities |
161 |
| - end |
162 |
| - person_model = define_model(:person, country_id: :integer) do |
163 |
| - belongs_to :country, include: :cities |
164 |
| - end |
165 |
| - delegate_reflection = person_model.reflect_on_association(:country) |
166 |
| - reflection = described_class.new(delegate_reflection) |
167 |
| - |
168 |
| - actual_includes = reflection.association_relation.includes_values |
169 |
| - expected_includes = Country.includes(:cities).includes_values |
170 |
| - expect(actual_includes).to eq expected_includes |
171 |
| - end |
172 |
| - end |
| 112 | + delegate_reflection = person_model.reflect_on_association(:country) |
| 113 | + reflection = described_class.new(delegate_reflection) |
173 | 114 |
|
174 |
| - context 'when the reflection options contain :group' do |
175 |
| - it 'creates an ActiveRecord::Relation from the group' do |
176 |
| - country_model = define_model(:country, mood: :string) do |
177 |
| - has_many :people, group: 'age' |
178 |
| - end |
179 |
| - define_model(:person, country_id: :integer, age: :integer) |
180 |
| - delegate_reflection = country_model.reflect_on_association(:people) |
181 |
| - reflection = described_class.new(delegate_reflection) |
182 |
| - |
183 |
| - actual_sql = reflection.association_relation.to_sql |
184 |
| - expected_sql = Person.group('age').to_sql |
185 |
| - expect(actual_sql).to eq expected_sql |
186 |
| - end |
187 |
| - end |
188 |
| - |
189 |
| - context 'when the reflection options contain :having' do |
190 |
| - it 'creates an ActiveRecord::Relation from the having' do |
191 |
| - country_model = define_model(:country) do |
192 |
| - has_many :people, having: 'country_id > 1' |
193 |
| - end |
194 |
| - define_model(:person, country_id: :integer) |
195 |
| - delegate_reflection = country_model.reflect_on_association(:people) |
196 |
| - reflection = described_class.new(delegate_reflection) |
197 |
| - |
198 |
| - actual_sql = reflection.association_relation.to_sql |
199 |
| - expected_sql = Person.having('country_id > 1').to_sql |
200 |
| - expect(actual_sql).to eq expected_sql |
201 |
| - end |
202 |
| - end |
203 |
| - |
204 |
| - context 'when the reflection options contain :limit' do |
205 |
| - it 'creates an ActiveRecord::Relation from the limit' do |
206 |
| - country_model = define_model(:country) do |
207 |
| - has_many :people, limit: 10 |
208 |
| - end |
209 |
| - define_model(:person, country_id: :integer) |
210 |
| - delegate_reflection = country_model.reflect_on_association(:people) |
211 |
| - reflection = described_class.new(delegate_reflection) |
212 |
| - |
213 |
| - actual_sql = reflection.association_relation.to_sql |
214 |
| - expected_sql = Person.limit(10).to_sql |
215 |
| - expect(actual_sql).to eq expected_sql |
216 |
| - end |
217 |
| - end |
218 |
| - |
219 |
| - context 'when the reflection options contain :offset' do |
220 |
| - it 'creates an ActiveRecord::Relation from the offset' do |
221 |
| - country_model = define_model(:country) do |
222 |
| - has_many :people, offset: 5 |
223 |
| - end |
224 |
| - define_model(:person, country_id: :integer) |
225 |
| - delegate_reflection = country_model.reflect_on_association(:people) |
226 |
| - reflection = described_class.new(delegate_reflection) |
227 |
| - |
228 |
| - actual_sql = reflection.association_relation.to_sql |
229 |
| - expected_sql = Person.offset(5).to_sql |
230 |
| - expect(actual_sql).to eq expected_sql |
231 |
| - end |
232 |
| - end |
233 |
| - |
234 |
| - context 'when the reflection options contain :select' do |
235 |
| - it 'creates an ActiveRecord::Relation from the select' do |
236 |
| - country_model = define_model(:country) do |
237 |
| - has_many :people, select: 'age' |
238 |
| - end |
239 |
| - define_model(:person, country_id: :integer, age: :integer) |
240 |
| - delegate_reflection = country_model.reflect_on_association(:people) |
241 |
| - reflection = described_class.new(delegate_reflection) |
242 |
| - |
243 |
| - actual_sql = reflection.association_relation.to_sql |
244 |
| - expected_sql = Person.select('age').to_sql |
245 |
| - expect(actual_sql).to eq expected_sql |
246 |
| - end |
| 115 | + actual_sql = reflection.association_relation.to_sql |
| 116 | + expected_sql = Country.all.to_sql |
| 117 | + expect(actual_sql).to eq expected_sql |
247 | 118 | end
|
248 | 119 | end
|
249 | 120 | end
|
|
0 commit comments