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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7f039ca57502dbf0119be19278464cfc7cae14ab
4
- data.tar.gz: 414f80e091cf917d897fd89fd28902c6f2563398
3
+ metadata.gz: cd29b55c6736cf66a3dceed9de1b23d662f42803
4
+ data.tar.gz: 1fc06588e4ae2262ee5c138ce388c881746c66c0
5
5
  SHA512:
6
- metadata.gz: 24ce6d75265f9edf97ecf7aa55bb1e4d10f4deed2c7452c0f08758d5890db1bb12fd6369709e7ef06f6ac5bd66df1097ac64a7ca580bf1b5fffbba2bbd1f384b
7
- data.tar.gz: 1f25bb2ec4160669a6332da359410a7f1f8fc0359bb175189f54fd4b84139d61565f92e74bc6e0640f220abda45b01905df3cb16296f44af73e33e92fa00099b
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
- intermediary_key = if intermediary_relation.association.is_a?(HasManyAssociation)
95
- intermediary_relation.association.children_name_sym.to_s.singularize.to_sym
96
- elsif intermediary_relation.association.is_a?(HasManyThroughAssociation)
97
- intermediary_relation.association.through_class.to_s.singularize.to_sym
98
- else
99
- raise "Intermediary association #{intermediary_relation.association} is not has many or has many through...?"
100
- end
101
-
102
- nested_conds = { intermediary_key => { parent_model_id_field.to_sym => parent_model.id } }
103
-
104
- if nested_association.is_a?(HasManyThroughAssociation)
105
- n = nested_association
106
- hash = nested_conds
107
-
108
- until !n.is_a?(HasManyThroughAssociation)
109
- key = n.through_class.to_s.singularize.to_sym
110
- hash = {key => hash}
111
- n = n.nested_association
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
 
@@ -1,4 +1,4 @@
1
1
  module PassiveRecord
2
2
  # passive_record version
3
- VERSION = "0.4.8"
3
+ VERSION = "0.4.10"
4
4
  end
@@ -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(:user) { User.create }
160
+ let(:author) { Author.create }
161
161
 
162
162
  subject(:posts_with_comment_by_user) do
163
- Post.find_by comments: { user: user }
163
+ Post.find_by comments: { author: author }
164
164
  end
165
165
 
166
166
  before do
167
- post.create_comment(user: user)
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(user: user)
176
+ another_post.create_comment(author: author)
177
177
 
178
- posts = Post.find_all_by comments: { user: user }
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
- user = User.create
505
- Comment.create(post: post, user: user)
514
+ author = Author.create
515
+ Comment.create(post: post, author: author)
506
516
 
507
- expect(post.commenters.all).to eq([user])
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 => "User"
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
- class User < Model
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 :user
165
+ belongs_to :author
166
+
167
+ def self.approved; where(approved: true) end
167
168
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: passive_record
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.8
4
+ version: 0.4.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joseph Weissman