passive_record 0.3.6 → 0.3.7
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/README.md +3 -2
- data/lib/passive_record/core/query.rb +18 -7
- data/lib/passive_record/version.rb +1 -1
- data/spec/passive_record_spec.rb +24 -7
- data/spec/spec_helper.rb +9 -2
- 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: fc8b97dbc45fa4523ccd68e4ab1fcd5e8cb7a195
|
4
|
+
data.tar.gz: 6213a7c2a314d2b6231d56708cd189fc4cc33d82
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9ef69a79461f22a4d8f633996b7783d29eca541f448638087e57015f2513df998c24ca5208bf6cb9b46f254e0c582b27b6edc60cfdf2568830193969868f030a
|
7
|
+
data.tar.gz: 29506381b9effa9c5e2864e2ddb48137a6b3d716c39aa754ea3e8d16071e83b28331f353012728315bda8f1793bfba12d0365d2e2925bf95509e4cff8a156b58
|
data/README.md
CHANGED
@@ -140,7 +140,7 @@ PassiveRecord may be right for you!
|
|
140
140
|
- `parent.children<<` (insert a related model)
|
141
141
|
- `parent.children.all?(&predicate)`
|
142
142
|
- `parent.children.empty?`
|
143
|
-
- `parent.children.where` (returns a `Core::Query`)
|
143
|
+
- `parent.children.where(conditions)` (returns a `Core::Query`)
|
144
144
|
|
145
145
|
### Relations
|
146
146
|
|
@@ -155,7 +155,7 @@ PassiveRecord may be right for you!
|
|
155
155
|
|
156
156
|
### Queries
|
157
157
|
|
158
|
-
`Core::Query` objects acquired through `where` are chainable, accept nested queries, and have the following public methods:
|
158
|
+
`Core::Query` objects acquired through `where` are chainable, accept nested queries and scopes, and have the following public methods:
|
159
159
|
|
160
160
|
- `where(conditions).all`
|
161
161
|
- `where(conditions).each` enumerates over `where(conditions).all`, so we have `where(conditions).count`, `where(conditions).first`, etc.
|
@@ -163,6 +163,7 @@ PassiveRecord may be right for you!
|
|
163
163
|
- `where(conditions).first_or_create(attrs)`
|
164
164
|
- `where(conditions).where(further_conditions)` (chaining)
|
165
165
|
- `where.not(conditions)` (negation)
|
166
|
+
- `where.scope` (scoping with model class methods that return a query)
|
166
167
|
|
167
168
|
## Hooks
|
168
169
|
|
@@ -9,7 +9,7 @@ module PassiveRecord
|
|
9
9
|
def initialize(klass,conditions={})
|
10
10
|
@klass = klass
|
11
11
|
@conditions = conditions
|
12
|
-
@
|
12
|
+
@scope = nil
|
13
13
|
end
|
14
14
|
|
15
15
|
def not(conditions={})
|
@@ -19,7 +19,15 @@ module PassiveRecord
|
|
19
19
|
def all
|
20
20
|
return [] unless conditions
|
21
21
|
matching = method(:matching_instances)
|
22
|
-
|
22
|
+
if @scope
|
23
|
+
if negated?
|
24
|
+
klass.reject(&@scope.method(:matching_instances))
|
25
|
+
else
|
26
|
+
klass.select(&@scope.method(:matching_instances))
|
27
|
+
end
|
28
|
+
else
|
29
|
+
klass.select(&matching)
|
30
|
+
end
|
23
31
|
end
|
24
32
|
def_delegators :all, :each
|
25
33
|
|
@@ -46,19 +54,22 @@ module PassiveRecord
|
|
46
54
|
@klass == other_query.klass && @conditions == other_query.conditions
|
47
55
|
end
|
48
56
|
|
57
|
+
def negated?
|
58
|
+
false
|
59
|
+
end
|
60
|
+
|
49
61
|
def method_missing(meth,*args,&blk)
|
50
62
|
if klass.methods.include?(meth)
|
51
|
-
|
63
|
+
# okay, so this thing is a query
|
64
|
+
# and we need to 'compose' with it somehow
|
65
|
+
@scope = klass.send(meth,*args,&blk)
|
66
|
+
self
|
52
67
|
else
|
53
68
|
super(meth,*args,&blk)
|
54
69
|
end
|
55
70
|
end
|
56
71
|
|
57
72
|
protected
|
58
|
-
def negated?
|
59
|
-
false
|
60
|
-
end
|
61
|
-
|
62
73
|
def evaluate_condition(instance, field, value)
|
63
74
|
if value.is_a?(Hash)
|
64
75
|
evaluate_nested_conditions(instance, field, value)
|
data/spec/passive_record_spec.rb
CHANGED
@@ -48,7 +48,7 @@ describe "passive record models" do
|
|
48
48
|
child.dogs << dog
|
49
49
|
expect(dog.inspect).to eq("Family::Dog (id: #{dog.id.inspect}, breed: \"#{dog.breed}\", created_at: #{dog.created_at}, sound: \"bark\", child_id: #{child.id.inspect})")
|
50
50
|
|
51
|
-
expect(child.inspect).to eq("Family::Child (id: #{child.id.inspect}, created_at: #{
|
51
|
+
expect(child.inspect).to eq("Family::Child (id: #{child.id.inspect}, created_at: #{child.created_at}, name: \"Alice\", toy_id: nil, dog_ids: [#{dog.id.inspect}], parent_id: nil)")
|
52
52
|
end
|
53
53
|
end
|
54
54
|
|
@@ -189,12 +189,29 @@ describe "passive record models" do
|
|
189
189
|
end
|
190
190
|
|
191
191
|
context 'queries with scopes' do
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
192
|
+
let(:post) { Post.create(published_at: 10.days.ago) }
|
193
|
+
let(:another_post) {Post.create(published_at: 2.days.ago)}
|
194
|
+
|
195
|
+
describe 'should restrict using class method' do
|
196
|
+
it 'should use a class method as a scope' do
|
197
|
+
expect(Post.recent).not_to include(post)
|
198
|
+
expect(Post.recent).to include(another_post)
|
199
|
+
end
|
200
|
+
|
201
|
+
it 'should negate a nullary scope' do
|
202
|
+
expect(Post.where.not.recent).to include(post)
|
203
|
+
expect(Post.where.not.recent).not_to include(another_post)
|
204
|
+
end
|
205
|
+
|
206
|
+
it 'should use a class method with an argument as a scope' do
|
207
|
+
expect(Post.where.published_within_days(3)).not_to include(post)
|
208
|
+
expect(Post.where.published_within_days(3)).to include(another_post)
|
209
|
+
end
|
210
|
+
|
211
|
+
it 'should negate a scope with an argument' do
|
212
|
+
expect(Post.where.not.published_within_days(3)).to include(post)
|
213
|
+
expect(Post.where.not.published_within_days(3)).not_to include(another_post)
|
214
|
+
end
|
198
215
|
end
|
199
216
|
end
|
200
217
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -87,7 +87,7 @@ class Role < Model
|
|
87
87
|
has_and_belongs_to_many :users
|
88
88
|
end
|
89
89
|
|
90
|
-
###
|
90
|
+
###
|
91
91
|
|
92
92
|
class Post < Model
|
93
93
|
has_many :comments
|
@@ -95,7 +95,14 @@ class Post < Model
|
|
95
95
|
|
96
96
|
attr_accessor :published_at
|
97
97
|
before_create { @published_at = Time.now }
|
98
|
-
|
98
|
+
|
99
|
+
def self.recent
|
100
|
+
where(:published_at => 3.days.ago..Time.now)
|
101
|
+
end
|
102
|
+
|
103
|
+
def self.published_within_days(n)
|
104
|
+
where(:published_at => n.days.ago..Time.now)
|
105
|
+
end
|
99
106
|
end
|
100
107
|
|
101
108
|
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.3.
|
4
|
+
version: 0.3.7
|
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-02-
|
11
|
+
date: 2016-02-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|