passive_record 0.4.8 → 0.4.10
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/passive_record/associations/has_many_through.rb +21 -22
- data/lib/passive_record/version.rb +1 -1
- data/spec/passive_record_spec.rb +18 -8
- data/spec/spec_helper.rb +10 -9
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cd29b55c6736cf66a3dceed9de1b23d662f42803
|
4
|
+
data.tar.gz: 1fc06588e4ae2262ee5c138ce388c881746c66c0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6b282a65d73fea26273adb072d74c3f56b828d753c468516ae8317330418c7161c1c0e55df1737a4e000ca7f1eedaa472cb146f35786459181f30bed39abbab6
|
7
|
+
data.tar.gz: f8cb9799687f87993624ff521479224c836acca05e59ce1c5680012a473790ae7cb144e448ce1661eaba8dc5ec0c7e1f90b09ef2d89deb228b274bccda26d960
|
@@ -91,29 +91,28 @@ module PassiveRecord
|
|
91
91
|
end
|
92
92
|
|
93
93
|
def intermediary_conditions
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
94
|
+
if intermediary_relation.association.is_a?(HasManyThroughAssociation)
|
95
|
+
# we are 'thru' a has_many thru at the same level, we should be able to recurse thru its #intermediary_conds
|
96
|
+
conds = intermediary_relation.intermediary_conditions
|
97
|
+
{ association.through_class.to_s.singularize.to_sym => conds }
|
98
|
+
elsif intermediary_relation.association.is_a?(HasManyAssociation) # normal has many?
|
99
|
+
intermediary_key = intermediary_relation.association.children_name_sym.to_s.singularize.to_sym
|
100
|
+
nested_conds = { intermediary_key => { parent_model_id_field.to_sym => parent_model.id } }
|
101
|
+
|
102
|
+
if nested_association.is_a?(HasManyThroughAssociation)
|
103
|
+
n = nested_association
|
104
|
+
hash = nested_conds
|
105
|
+
|
106
|
+
until !n.is_a?(HasManyThroughAssociation)
|
107
|
+
key = n.through_class.to_s.singularize.to_sym
|
108
|
+
hash = {key => hash}
|
109
|
+
n = n.nested_association
|
110
|
+
end
|
111
|
+
|
112
|
+
hash
|
113
|
+
else
|
114
|
+
nested_conds
|
112
115
|
end
|
113
|
-
|
114
|
-
hash
|
115
|
-
else
|
116
|
-
nested_conds
|
117
116
|
end
|
118
117
|
end
|
119
118
|
|
data/spec/passive_record_spec.rb
CHANGED
@@ -157,14 +157,14 @@ describe "passive record models" do
|
|
157
157
|
|
158
158
|
context 'nested queries' do
|
159
159
|
let(:post) { Post.create }
|
160
|
-
let(:
|
160
|
+
let(:author) { Author.create }
|
161
161
|
|
162
162
|
subject(:posts_with_comment_by_user) do
|
163
|
-
Post.find_by comments: {
|
163
|
+
Post.find_by comments: { author: author }
|
164
164
|
end
|
165
165
|
|
166
166
|
before do
|
167
|
-
post.create_comment(
|
167
|
+
post.create_comment(author: author)
|
168
168
|
end
|
169
169
|
|
170
170
|
it 'should find a single record through a nested query' do
|
@@ -173,9 +173,9 @@ describe "passive record models" do
|
|
173
173
|
|
174
174
|
it 'should find multiple records through a nested query' do
|
175
175
|
another_post = Post.create
|
176
|
-
another_post.create_comment(
|
176
|
+
another_post.create_comment(author: author)
|
177
177
|
|
178
|
-
posts = Post.find_all_by comments: {
|
178
|
+
posts = Post.find_all_by comments: { author: author }
|
179
179
|
expect(posts.count).to eq(2)
|
180
180
|
end
|
181
181
|
|
@@ -332,6 +332,16 @@ describe "passive record models" do
|
|
332
332
|
expect(network.posts.recent).not_to include(not_recent_post)
|
333
333
|
end
|
334
334
|
end
|
335
|
+
|
336
|
+
describe 'should find related models on a recursive has_many thru' do
|
337
|
+
let!(:approved_comment) { Post.first.create_comment(approved: true) }
|
338
|
+
let!(:unapproved_comment) { Post.last.create_comment(approved: false) }
|
339
|
+
|
340
|
+
it 'should refine/restrict' do
|
341
|
+
expect(network.comments.approved).to include(approved_comment)
|
342
|
+
expect(network.comments.approved).not_to include(unapproved_comment)
|
343
|
+
end
|
344
|
+
end
|
335
345
|
end
|
336
346
|
end
|
337
347
|
end
|
@@ -501,10 +511,10 @@ describe "passive record models" do
|
|
501
511
|
|
502
512
|
it 'should accept class name' do
|
503
513
|
post = Post.create
|
504
|
-
|
505
|
-
Comment.create(post: post,
|
514
|
+
author = Author.create
|
515
|
+
Comment.create(post: post, author: author)
|
506
516
|
|
507
|
-
expect(post.commenters.all).to eq([
|
517
|
+
expect(post.commenters.all).to eq([author])
|
508
518
|
end
|
509
519
|
end
|
510
520
|
|
data/spec/spec_helper.rb
CHANGED
@@ -105,6 +105,7 @@ end
|
|
105
105
|
class Network < Model
|
106
106
|
has_many :streams
|
107
107
|
has_many :posts, :through => :streams
|
108
|
+
has_many :comments, :through => :posts
|
108
109
|
end
|
109
110
|
|
110
111
|
class Stream < Model
|
@@ -131,9 +132,10 @@ class Blog < Model
|
|
131
132
|
end
|
132
133
|
|
133
134
|
class Post < Model
|
135
|
+
belongs_to :author
|
134
136
|
belongs_to :blog
|
135
137
|
has_many :comments
|
136
|
-
has_many :commenters, :through => :comments, :class_name => "
|
138
|
+
has_many :commenters, :through => :comments, :class_name => "Author"
|
137
139
|
|
138
140
|
attr_accessor :active, :published_at
|
139
141
|
before_create { @published_at = Time.now }
|
@@ -149,19 +151,18 @@ class Post < Model
|
|
149
151
|
def self.published_within_days(n)
|
150
152
|
where(:published_at => n.days.ago..Time.now)
|
151
153
|
end
|
152
|
-
|
153
|
-
# hmmm -- why *exactly* do we need this?
|
154
|
-
def feed_id
|
155
|
-
raise 'not impl'
|
156
|
-
# blog.feed_id
|
157
|
-
end
|
158
154
|
end
|
159
155
|
|
160
|
-
|
156
|
+
# whoa, we're reopening user here -- this might not be expected
|
157
|
+
class Author < Model
|
158
|
+
has_many :posts
|
161
159
|
has_many :comments
|
162
160
|
end
|
163
161
|
|
164
162
|
class Comment < Model
|
163
|
+
attr_accessor :approved
|
165
164
|
belongs_to :post
|
166
|
-
belongs_to :
|
165
|
+
belongs_to :author
|
166
|
+
|
167
|
+
def self.approved; where(approved: true) end
|
167
168
|
end
|