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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e447d692e69db528f32f29de1a0532b893b1e686
4
- data.tar.gz: 06d7b54baeff7b9787328dcebe3e21b4c0e40d81
3
+ metadata.gz: c8fb416a4a6126f07276fd4293037396b32a7c71
4
+ data.tar.gz: c5890ee4024f4f22f96881d392ea08a64c526d77
5
5
  SHA512:
6
- metadata.gz: de192342a3af0d2b7688b84ce772d073a211aca22577a4c02ff3ba2294c0bd167cd42936407b53ceb87fc04ca632074a688636c1bf4184d3dbfae299138174e1
7
- data.tar.gz: 9b91e33a7a272c081dc69a0c0bd6a1bb7f3395c12e47f862db563d3792facdea73d84d6307ec2d4b1b97edf1e7cba4f31ac88587722a242159842d66dd042373
6
+ metadata.gz: 102e1dfc77bc9690ac28ad763a833d48c7ddf27bfb13863acfb7532c776e56599bb3e9e8ab87ad12946993a152f62aa1395c034eaa3bdf9c066246e58886ec96
7
+ data.tar.gz: 86a25cf562311ab9c2548a98560fd504c37ea66643205a51dbbff7e75802f16348a91e872430f53b088831ec1c72894c3c88333b245221941fc1c266fe4235b0
@@ -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 turned on explicitly
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
- # Create a new collection with the scope set to the result of the query on the record_class
67
- def where(*args)
68
- self.new(record_class.where(*args))
69
- end
70
-
71
- # Create a new collection with the scope set to the result of the query on the record_class
72
- def joins(*args)
73
- self.new(record_class.joins(*args))
74
- end
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 an update in forms, this triggers plural routes
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
- # update existing scope with new one having applied where clause if possible
204
- def where(*args)
205
- @collection = @collection.where(*args)
206
- self
207
- end
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
- # update existing scope with new one having applied not clause if possible
222
- def not(*args)
223
- @collection = @collection.not(*args)
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
@@ -1,3 +1,3 @@
1
1
  module RecordCollection
2
- VERSION = "0.10.3"
2
+ VERSION = "0.10.4"
3
3
  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.3
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-26 00:00:00.000000000 Z
11
+ date: 2015-12-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler