record_collection 0.10.3 → 0.10.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -0
- data/lib/record_collection/base.rb +20 -38
- data/lib/record_collection/version.rb +1 -1
- data/spec/base/finding_records_spec.rb +20 -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: c8fb416a4a6126f07276fd4293037396b32a7c71
|
4
|
+
data.tar.gz: c5890ee4024f4f22f96881d392ea08a64c526d77
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 102e1dfc77bc9690ac28ad763a833d48c7ddf27bfb13863acfb7532c776e56599bb3e9e8ab87ad12946993a152f62aa1395c034eaa3bdf9c066246e58886ec96
|
7
|
+
data.tar.gz: 86a25cf562311ab9c2548a98560fd504c37ea66643205a51dbbff7e75802f16348a91e872430f53b088831ec1c72894c3c88333b245221941fc1c266fe4235b0
|
data/CHANGELOG.md
CHANGED
@@ -1,6 +1,12 @@
|
|
1
1
|
CHANGELOG
|
2
2
|
=========
|
3
3
|
|
4
|
+
2015-12-28 - v0.10.4
|
5
|
+
--------------------
|
6
|
+
### Added
|
7
|
+
* Allow #find(ids) on collection instance that could be the result of a
|
8
|
+
`joins` or an `includes` scope setup call
|
9
|
+
|
4
10
|
2015-12-26 - v0.10.3
|
5
11
|
--------------------
|
6
12
|
### Added
|
@@ -46,7 +46,7 @@ module RecordCollection
|
|
46
46
|
alias_method :old_validates, :validates
|
47
47
|
def validates(attr, options)
|
48
48
|
# Collection nil attributes mean they do not play a role for the collection.
|
49
|
-
# So validating when the value is nil is not the default behaviour. I to be
|
49
|
+
# So validating when the value is nil is not the default behaviour. I to be used explicitly
|
50
50
|
# by specifying allow_nil: false
|
51
51
|
options[:allow_nil] = true unless options.has_key?(:allow_nil)
|
52
52
|
old_validates attr, options
|
@@ -63,23 +63,15 @@ module RecordCollection
|
|
63
63
|
self.new(collection)
|
64
64
|
end
|
65
65
|
|
66
|
-
#
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
# Create a new collection with the scope set to the result of the query on the record_class
|
77
|
-
def includes(*args)
|
78
|
-
self.new(record_class.includes(*args))
|
79
|
-
end
|
80
|
-
|
81
|
-
def all(*args)
|
82
|
-
self.new(record_class.all(*args))
|
66
|
+
# instantiate a new instance of the collection with the
|
67
|
+
# scope set to the received argument:
|
68
|
+
# Collection.joins(:project).where(projects: {state: 'active'})
|
69
|
+
# The joins returns the instance, the second where is handled by
|
70
|
+
# the instance itself as an instance delegate method
|
71
|
+
%i[where joins includes all].each do |delegate_method|
|
72
|
+
define_method delegate_method do |*args|
|
73
|
+
self.new(record_class.public_send(delegate_method, *args))
|
74
|
+
end
|
83
75
|
end
|
84
76
|
end
|
85
77
|
|
@@ -143,7 +135,7 @@ module RecordCollection
|
|
143
135
|
end
|
144
136
|
|
145
137
|
def persisted?
|
146
|
-
# Behave like
|
138
|
+
# Behave like a non persisted record in forms, this triggers plural (collection) routes
|
147
139
|
false
|
148
140
|
end
|
149
141
|
|
@@ -200,27 +192,17 @@ module RecordCollection
|
|
200
192
|
self
|
201
193
|
end
|
202
194
|
|
203
|
-
#
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
# update existing scope with new one having applied joins clause if possible
|
210
|
-
def joins(*args)
|
211
|
-
@collection = @collection.joins(*args)
|
212
|
-
self
|
213
|
-
end
|
214
|
-
|
215
|
-
# update existing scope with new one having applied includes clause if possible
|
216
|
-
def includes(*args)
|
217
|
-
@collection = @collection.includes(*args)
|
218
|
-
self
|
195
|
+
# delegate methods to the collection returning self
|
196
|
+
%i[joins includes where not].each do |delegate_method|
|
197
|
+
define_method delegate_method do |*args|
|
198
|
+
@collection = @collection.public_send(delegate_method, *args)
|
199
|
+
self
|
200
|
+
end
|
219
201
|
end
|
220
202
|
|
221
|
-
|
222
|
-
|
223
|
-
@collection = @collection.
|
203
|
+
def find(ids)
|
204
|
+
ids = ids.split(RecordCollection.ids_separator) if ids.is_a?(String)
|
205
|
+
@collection = @collection.find(Array.wrap(ids))
|
224
206
|
self
|
225
207
|
end
|
226
208
|
end
|
@@ -23,6 +23,26 @@ RSpec.describe Employee::Collection do
|
|
23
23
|
employee2 = Employee.create name: 'E2', section: 'QNP', admin: false, vegan: false
|
24
24
|
described_class.find([employee1.id, employee2.id]).collection.should match_array [employee1, employee2]
|
25
25
|
end
|
26
|
+
|
27
|
+
it "works with an includes statement (find on instance)" do
|
28
|
+
employee1 = Employee.create name: 'E1', section: 'ABC', admin: true, vegan: false
|
29
|
+
described_class.includes(:project).find([employee1.id]).should be_a RecordCollection::Base
|
30
|
+
end
|
31
|
+
|
32
|
+
it "works with an includes statement (find on instance) and string argument" do
|
33
|
+
employee1 = Employee.create name: 'E1', section: 'ABC', admin: true, vegan: false
|
34
|
+
employee2 = Employee.create name: 'E2', section: 'QNP', admin: false, vegan: false
|
35
|
+
result = described_class.includes(:project).find("#{employee1.id}~#{employee2.id}")
|
36
|
+
result.should be_a RecordCollection::Base
|
37
|
+
result.collection.should match_array [employee1, employee2]
|
38
|
+
end
|
39
|
+
|
40
|
+
it "works with an includes statement (find on instance) and integer id argument" do
|
41
|
+
employee1 = Employee.create name: 'E1', section: 'ABC', admin: true, vegan: false
|
42
|
+
result = described_class.includes(:project).find(employee1.id)
|
43
|
+
result.should be_a RecordCollection::Base
|
44
|
+
result.collection.should eq [employee1]
|
45
|
+
end
|
26
46
|
end
|
27
47
|
|
28
48
|
it "finds single id as string as collection" do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: record_collection
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.10.
|
4
|
+
version: 0.10.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Benjamin ter Kuile
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-12-
|
11
|
+
date: 2015-12-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|