passive_record 0.4.4 → 0.4.5

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: 3bbf676f1d3d94dce7e4b0b0d2faa89476149335
4
- data.tar.gz: c1607589beb7a3f7cf199cf1f8bdaf67b12568bc
3
+ metadata.gz: 4650224c2791ed204faef1985b4ea12f646dae7e
4
+ data.tar.gz: 9a0af9c4a3d86353cffa3b1b860040de882a2e59
5
5
  SHA512:
6
- metadata.gz: 158a69a9f39cb6cd8df9dae55f2a4554498af25d3044c94d3f1b3af69ed7ee810891efa365470ebb95fddfe151de963d8aa9abc94066638f46ee47259af44acd
7
- data.tar.gz: 23c5791750f524c311a0e98f3849e7b399f28c883b174635289eb25d1ccfa09662c6b35d4c103d18f18321471feb6579465e9998db66e7ab2edb34232194cf0a
6
+ metadata.gz: 6a1c134e1dbac7da33ffe7055f31b7b263deb1c4e7be5cd02b91b252370a07c00c2a7918e1230ea83259ff19d7e10fd3b3705bdc22e6c9c049826d9abd00d368
7
+ data.tar.gz: f84219b4fbca873039a48d226e5b20d91854bc2a8083c67ed2e3d78f6f0e3ef5d6a49c85d1fd7e450a5d6d718b69af154425fe3257a36bc434577a6901b03445
data/README.md CHANGED
@@ -184,7 +184,7 @@ PassiveRecord may be right for you!
184
184
  `conditions` here is expected to be a hash of attribute values. Note that there is special behavior for certain kinds of values.
185
185
 
186
186
  - Ranges select models with an attribute covered by the range (behaving like `BETWEEN`). For instance you might query for users with birthdays between yesterday and today with `User.where(birthday: 1.day.ago...1.day.from_now)`
187
- - Arrays select models with an attribute whose value is in the array (behaving like `IN`), so for instance you may query for users whose job title is included in a list of job titles like: `User.find_all_by(job_title: ['manager', 'developer', 'qa'])`
187
+ - Arrays select models with an attribute whose value is in the array (behaving like `IN`), so for instance you may query for users whose job title is included in a list of job titles like: `User.where(job_title: ['manager', 'developer', 'qa'])`
188
188
  - Hash values (subhashes) select models with related models who attributes match the inner hash. So `Doctor.where(appointments: { patient: patient })` would lookup doctors whose appointments include an appointment with `patient`.
189
189
 
190
190
  ## Hooks
@@ -199,7 +199,7 @@ PassiveRecord may be right for you!
199
199
  # Prior Art
200
200
 
201
201
  - Approaches exist that use ActiveRecord directly, and then override various methods in such a way to prevent AR from trying to persist the model. The canonical example here is the [tableless model](http://railscasts.com/episodes/193-tableless-model?view=asciicast) approach, and the use case given there is a model that wraps around sending an email. This is maybe interesting because, similar to the round-trip with a database, sending mail is externally "effectful" (and so, for instance, you may wish to take additional care around confirmation or retry logic, in order ensure you are not sending the same message more than once.)
202
- - These approaches are seen as somewhat hacky today, given that [ActiveModel](https://github.com/rails/rails/tree/master/activemodel) can give plain old Ruby objects a lot of the augmentations that ActiveRecord gives, such as validations, hooks and attribute management. However I don't really see a way to do relations that interoperate with ActiveRecord the way you could, at least to some degree, with tableless models.
202
+ - These approaches are seen as somewhat hacky today, given that [ActiveModel](https://github.com/rails/rails/tree/master/activemodel) can give plain old Ruby objects a lot of the augmentations that ActiveRecord gives, such as validations, hooks and attribute management. However I don't really see a way to do relations that interoperate with ActiveRecord the way you could, at least to some degree, with tableless models.
203
203
  - It's not really clear to me yet if it's interesting for PassiveRecord to be able to interoperate smoothly with ActiveRecord relations. It seems like we might be able to pull some similar tricks as the "tableless" approach in order to permit at least some relations to work between them. But their intentions are so different I can't help but think there would be very strange bugs lurking in any such integration -- so the encouraged architecture would be a complete separation between active and passive models.
204
204
 
205
205
  ## Copyright
@@ -19,6 +19,7 @@ module PassiveRecord
19
19
  def all
20
20
  child_class.where(parent_model_id_field => parent_model.id).all
21
21
  end
22
+
22
23
  def_delegators :all, :each, :last, :all?, :empty?
23
24
 
24
25
  def where(conditions={})
@@ -33,6 +34,14 @@ module PassiveRecord
33
34
  def singular?
34
35
  false
35
36
  end
37
+
38
+ def method_missing(meth,*args,&blk)
39
+ if child_class.methods.include?(meth)
40
+ where.send(meth,*args,&blk)
41
+ else
42
+ super(meth,*args,&blk)
43
+ end
44
+ end
36
45
  end
37
46
  end
38
47
  end
@@ -84,6 +84,13 @@ module PassiveRecord
84
84
  intermediary_relation.all
85
85
  end
86
86
  end
87
+
88
+ def where(conditions={})
89
+ child_class.where(conditions.merge(
90
+ intermediary_relation.association.children_name_sym.to_s.singularize.to_sym => {
91
+ parent_model_id_field.to_sym => parent_model.id
92
+ }))
93
+ end
87
94
  end
88
95
  end
89
96
  end
@@ -1,4 +1,4 @@
1
1
  module PassiveRecord
2
2
  # passive_record version
3
- VERSION = "0.4.4"
3
+ VERSION = "0.4.5"
4
4
  end
@@ -258,6 +258,30 @@ describe "passive record models" do
258
258
  end
259
259
  end
260
260
  end
261
+
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
268
+ a_blog.create_post(published_at: 1.day.ago)
269
+ end
270
+
271
+ describe 'should find related models through a has many' do
272
+ it 'should restrict as expected' do
273
+ expect(a_blog.posts.recent).to include(recent_post)
274
+ expect(a_blog.posts.recent).not_to include(not_recent_post)
275
+ end
276
+ end
277
+
278
+ describe 'should find related models on a has_many through' do
279
+ it 'should restrict as expected' do
280
+ expect(feed.posts.recent).to include(recent_post)
281
+ expect(feed.posts.recent).not_to include(not_recent_post)
282
+ end
283
+ end
284
+ end
261
285
  end
262
286
  end
263
287
  end
data/spec/spec_helper.rb CHANGED
@@ -100,8 +100,20 @@ class Role < Model
100
100
  end
101
101
 
102
102
  ###
103
+ #
104
+
105
+ class Feed < Model
106
+ has_many :blogs
107
+ has_many :posts, :through => :blogs
108
+ end
109
+
110
+ class Blog < Model
111
+ has_many :posts
112
+ belongs_to :feed
113
+ end
103
114
 
104
115
  class Post < Model
116
+ belongs_to :blog
105
117
  has_many :comments
106
118
  has_many :commenters, :through => :comments, :class_name => "User"
107
119
 
@@ -119,6 +131,12 @@ class Post < Model
119
131
  def self.published_within_days(n)
120
132
  where(:published_at => n.days.ago..Time.now)
121
133
  end
134
+
135
+ # hmmm -- why *exactly* do we need this?
136
+ def feed_id
137
+ raise 'not impl'
138
+ # blog.feed_id
139
+ end
122
140
  end
123
141
 
124
142
  class User < Model
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
4
+ version: 0.4.5
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-04-21 00:00:00.000000000 Z
11
+ date: 2016-07-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport