passive_record 0.4.5 → 0.4.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/passive_record/associations/has_many_through.rb +30 -3
- data/lib/passive_record/associations/has_one.rb +2 -0
- data/lib/passive_record/core/query.rb +5 -1
- data/lib/passive_record/version.rb +1 -1
- data/spec/passive_record_spec.rb +40 -8
- data/spec/spec_helper.rb +18 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bddfa4e4c2c50662999f1bc1949e44f1e0878a93
|
4
|
+
data.tar.gz: 50b8eba073d411dd99e0cca965d6e5e7676418ca
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
89
|
-
|
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
|
@@ -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
|
-
|
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
|
data/spec/passive_record_spec.rb
CHANGED
@@ -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
|
263
|
-
let(:
|
264
|
-
let(:
|
265
|
-
let(:
|
266
|
-
|
267
|
-
let(:
|
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
|
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
|
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.
|
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-
|
11
|
+
date: 2016-07-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|