passive_record 0.4.5 → 0.4.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4650224c2791ed204faef1985b4ea12f646dae7e
4
- data.tar.gz: 9a0af9c4a3d86353cffa3b1b860040de882a2e59
3
+ metadata.gz: bddfa4e4c2c50662999f1bc1949e44f1e0878a93
4
+ data.tar.gz: 50b8eba073d411dd99e0cca965d6e5e7676418ca
5
5
  SHA512:
6
- metadata.gz: 6a1c134e1dbac7da33ffe7055f31b7b263deb1c4e7be5cd02b91b252370a07c00c2a7918e1230ea83259ff19d7e10fd3b3705bdc22e6c9c049826d9abd00d368
7
- data.tar.gz: f84219b4fbca873039a48d226e5b20d91854bc2a8083c67ed2e3d78f6f0e3ef5d6a49c85d1fd7e450a5d6d718b69af154425fe3257a36bc434577a6901b03445
6
+ metadata.gz: 90549bebdef9863aa15c1d0fd32a0dd71eeb411a8705d63e263b84656bd4e79db976467aa39d41fdc205e7cf0c7fd4e43590d3a9b7973137548e9cc0bcbc3264
7
+ data.tar.gz: ad5276c142395eb0b9432952f6bae95ab1e3dd0179d91964dac484aa0358f47d88738e8f13f2fb3e641b0862b1bd64e7ccd5cfa9368185c9669c87cba531eba3
@@ -4,6 +4,11 @@ module PassiveRecord
4
4
  def to_relation(parent_model)
5
5
  HasManyThroughRelation.new(self, parent_model)
6
6
  end
7
+
8
+ def nested_association
9
+ thru_klass = base_association.child_class_name.singularize.constantize
10
+ thru_klass.associations.detect { |assn| assn.child_class_name == child_class_name }
11
+ end
7
12
  end
8
13
 
9
14
  class HasManyThroughRelation < HasManyRelation
@@ -85,11 +90,33 @@ module PassiveRecord
85
90
  end
86
91
  end
87
92
 
88
- def where(conditions={})
89
- child_class.where(conditions.merge(
93
+ def intermediary_conditions
94
+ nested_conds = {
90
95
  intermediary_relation.association.children_name_sym.to_s.singularize.to_sym => {
91
96
  parent_model_id_field.to_sym => parent_model.id
92
- }))
97
+ }
98
+ }
99
+
100
+ if nested_association.is_a?(HasManyThroughAssociation)
101
+ n = nested_association
102
+ hash = nested_conds
103
+ compound_key = []
104
+
105
+ until n.is_a?(HasManyAssociation)
106
+ key = n.through_class.to_s.singularize.to_sym
107
+ compound_key.push(key)
108
+ hash = {key => hash}
109
+ n = n.nested_association
110
+ end
111
+
112
+ hash
113
+ else
114
+ nested_conds
115
+ end
116
+ end
117
+
118
+ def where(conditions={})
119
+ child_class.where(conditions.merge(intermediary_conditions))
93
120
  end
94
121
  end
95
122
  end
@@ -8,6 +8,8 @@ module PassiveRecord
8
8
  def target_name_symbol
9
9
  child_name_sym
10
10
  end
11
+
12
+ def children_name_sym; child_name_sym end
11
13
  end
12
14
 
13
15
  class Relation < Struct.new(:association, :parent_model)
@@ -98,7 +98,11 @@ module PassiveRecord
98
98
  if association.is_a?(Associations::Relation) && !association.singular?
99
99
  association.where(association_field => val).any?
100
100
  else
101
- association.send(association_field) == val
101
+ if val.is_a?(Hash)
102
+ evaluate_nested_conditions(association, association_field, val)
103
+ else
104
+ association.send(association_field) == val
105
+ end
102
106
  end
103
107
  end
104
108
  end
@@ -1,4 +1,4 @@
1
1
  module PassiveRecord
2
2
  # passive_record version
3
- VERSION = "0.4.5"
3
+ VERSION = "0.4.6"
4
4
  end
@@ -177,6 +177,15 @@ describe "passive record models" do
177
177
  posts = Post.find_all_by comments: { user: user }
178
178
  expect(posts.count).to eq(2)
179
179
  end
180
+
181
+ it 'should find records through a doubly-nested query' do
182
+ feed = Feed.create
183
+ blog = feed.create_blog
184
+ post = blog.create_post
185
+
186
+ # expect( Post.find_by(blog: { feed_id: feed.id }) ).to eq(post)
187
+ expect( Post.find_by(blog: { feed: { id: feed.id }}) ).to eq(post)
188
+ end
180
189
  end
181
190
 
182
191
  context 'queries with ranges' do
@@ -259,28 +268,51 @@ describe "passive record models" do
259
268
  end
260
269
  end
261
270
 
262
- context 'scopes on relations' do
263
- let(:feed) { Feed.create }
264
- let(:a_blog) { feed.create_blog } #.create }
265
- let(:not_recent_post) { a_blog.create_post(published_at: 10.days.ago) }
266
-
267
- let(:recent_post) do
271
+ context 'querying with scopes through relationships' do
272
+ let!(:network) { Network.create }
273
+ let!(:stream) { network.create_stream }
274
+ let!(:channel) { stream.create_channel }
275
+ let!(:feed) { channel.create_feed }
276
+ let!(:a_blog) { feed.create_blog }
277
+ let!(:not_recent_post) { a_blog.create_post(published_at: 10.days.ago) }
278
+ let!(:recent_post) do
268
279
  a_blog.create_post(published_at: 1.day.ago)
269
280
  end
270
281
 
271
282
  describe 'should find related models through a has many' do
272
- it 'should restrict as expected' do
283
+ it 'should restrict' do
273
284
  expect(a_blog.posts.recent).to include(recent_post)
274
285
  expect(a_blog.posts.recent).not_to include(not_recent_post)
275
286
  end
276
287
  end
277
288
 
278
289
  describe 'should find related models on a has_many through' do
279
- it 'should restrict as expected' do
290
+ it 'should restrict' do
280
291
  expect(feed.posts.recent).to include(recent_post)
281
292
  expect(feed.posts.recent).not_to include(not_recent_post)
282
293
  end
283
294
  end
295
+
296
+ describe 'should find related models on a nested has_many thru' do
297
+ it 'should restrict' do
298
+ expect(channel.posts.recent).to include(recent_post)
299
+ expect(channel.posts.recent).not_to include(not_recent_post)
300
+ end
301
+ end
302
+
303
+ describe 'should find related models on a double-nested has_many thru' do
304
+ it 'should restrict' do
305
+ expect(stream.posts.recent).to include(recent_post)
306
+ expect(stream.posts.recent).not_to include(not_recent_post)
307
+ end
308
+ end
309
+
310
+ describe 'should find related models on a deeply nested has_many thru' do
311
+ it 'should restrict' do
312
+ expect(network.posts.recent).to include(recent_post)
313
+ expect(network.posts.recent).not_to include(not_recent_post)
314
+ end
315
+ end
284
316
  end
285
317
  end
286
318
  end
data/spec/spec_helper.rb CHANGED
@@ -102,7 +102,25 @@ end
102
102
  ###
103
103
  #
104
104
 
105
+ class Network < Model
106
+ has_many :streams
107
+ has_many :posts, :through => :streams
108
+ end
109
+
110
+ class Stream < Model
111
+ belongs_to :network
112
+ has_many :channels
113
+ has_many :posts, :through => :channels
114
+ end
115
+
116
+ class Channel < Model
117
+ belongs_to :stream
118
+ has_many :feeds
119
+ has_many :posts, :through => :feeds
120
+ end
121
+
105
122
  class Feed < Model
123
+ belongs_to :channel
106
124
  has_many :blogs
107
125
  has_many :posts, :through => :blogs
108
126
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: passive_record
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.5
4
+ version: 0.4.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joseph Weissman
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-07-03 00:00:00.000000000 Z
11
+ date: 2016-07-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport