record_collection 0.8.2 → 0.8.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +16 -0
- data/README.md +13 -0
- data/lib/record_collection/base.rb +10 -0
- data/lib/record_collection/version.rb +1 -1
- data/spec/base/after_record_update_spec.rb +8 -0
- data/spec/base/before_record_update_spec.rb +22 -0
- metadata +4 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6a1f2fc09b1f3aa4398ce4a15a126112e3dac101
|
4
|
+
data.tar.gz: 9aa4a6bff6c18594e8f4dbabb3efd4abde222a94
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cfabb7c0d78aa856f3a10eda8260e1865d6ee83727ac717d0b0b9115b8d0c2dc573ebc40de763ed39641e91f2881cb1dfaac02767c470edc06afa4ef337205e3
|
7
|
+
data.tar.gz: 8d08ada30d3c68e32f62e97df0a8e4795ab8f8050551e30ba9d81e10216b124ed85c99c78202d4a9cccf7ce48a3dc25951f08a79cfb9e163a299abeffda457ca
|
data/CHANGELOG.md
ADDED
data/README.md
CHANGED
@@ -118,6 +118,19 @@ class MyAwesomeCollection < RecordCollection::Base
|
|
118
118
|
end
|
119
119
|
```
|
120
120
|
|
121
|
+
### The `before_record_update` hook
|
122
|
+
The collection implements a general `update(attributes)` method that will
|
123
|
+
update all the attributes that are set in the collection on the records it contains.
|
124
|
+
If you want precondition your data you can do so in this hook:
|
125
|
+
|
126
|
+
```ruby
|
127
|
+
class Project::Prince2::Collection < RecordCollection::Base
|
128
|
+
before_record_update do |record|
|
129
|
+
record.plan_date_set = true if plan_date.present?
|
130
|
+
end
|
131
|
+
end
|
132
|
+
```
|
133
|
+
|
121
134
|
### The `after_record_update` hook
|
122
135
|
The collection implements a general `update(attributes)` method that will
|
123
136
|
update all the attributes that are set in the collection on the records it contains.
|
@@ -28,6 +28,14 @@ module RecordCollection
|
|
28
28
|
@record_class = klass
|
29
29
|
end
|
30
30
|
|
31
|
+
def before_record_update(&blk)
|
32
|
+
if blk
|
33
|
+
@before_record_update = blk
|
34
|
+
else
|
35
|
+
@before_record_update
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
31
39
|
def after_record_update(&blk)
|
32
40
|
if blk
|
33
41
|
@after_record_update = blk
|
@@ -93,7 +101,9 @@ module RecordCollection
|
|
93
101
|
|
94
102
|
def update_collection_attributes!
|
95
103
|
after_blk = self.class.after_record_update
|
104
|
+
before_blk = self.class.before_record_update
|
96
105
|
each do |record|
|
106
|
+
before_blk.call(record) if before_blk
|
97
107
|
record.update changed_attributes
|
98
108
|
after_blk.call(record) if after_blk
|
99
109
|
end
|
@@ -5,10 +5,18 @@ class AfterRecordUpdateCollection < RecordCollection::Base
|
|
5
5
|
end
|
6
6
|
RSpec.describe AfterRecordUpdateCollection do
|
7
7
|
subject { described_class.new }
|
8
|
+
|
8
9
|
it "executes on the record" do
|
9
10
|
employee = Employee.create section: 'SE1', admin: false
|
10
11
|
described_class.new([employee]).update({})
|
11
12
|
employee.reload
|
12
13
|
employee.admin.should be true
|
13
14
|
end
|
15
|
+
|
16
|
+
it "will not be overridden by the update action itself (hence after)" do
|
17
|
+
employee = Employee.create section: 'SE1', admin: false
|
18
|
+
described_class.new([employee]).update(admin: false)
|
19
|
+
employee.reload
|
20
|
+
employee.admin.should be true
|
21
|
+
end
|
14
22
|
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
class BeforeRecordUpdateCollection < RecordCollection::Base
|
3
|
+
attribute :section
|
4
|
+
attribute :admin
|
5
|
+
before_record_update{|record| record.admin = true }
|
6
|
+
end
|
7
|
+
RSpec.describe BeforeRecordUpdateCollection do
|
8
|
+
subject { described_class.new }
|
9
|
+
it "executes on the record" do
|
10
|
+
employee = Employee.create section: 'SE1', admin: false
|
11
|
+
described_class.new([employee]).update({})
|
12
|
+
employee.reload
|
13
|
+
employee.admin.should be true
|
14
|
+
end
|
15
|
+
|
16
|
+
it "will be overriden by the actual update action (hence before)" do
|
17
|
+
employee = Employee.create section: 'SE1', admin: false
|
18
|
+
described_class.new([employee]).update(admin: false)
|
19
|
+
employee.reload
|
20
|
+
employee.admin.should be false
|
21
|
+
end
|
22
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: record_collection
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.8.
|
4
|
+
version: 0.8.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Benjamin ter Kuile
|
@@ -356,6 +356,7 @@ files:
|
|
356
356
|
- ".gitignore"
|
357
357
|
- ".rspec"
|
358
358
|
- ".travis.yml"
|
359
|
+
- CHANGELOG.md
|
359
360
|
- Gemfile
|
360
361
|
- LICENSE.txt
|
361
362
|
- README.md
|
@@ -399,6 +400,7 @@ files:
|
|
399
400
|
- record_collection.gemspec
|
400
401
|
- spec/base/accessors_spec.rb
|
401
402
|
- spec/base/after_record_update_spec.rb
|
403
|
+
- spec/base/before_record_update_spec.rb
|
402
404
|
- spec/base/behaviour_spec.rb
|
403
405
|
- spec/base/finding_records_spec.rb
|
404
406
|
- spec/base/inheritance_spec.rb
|
@@ -516,6 +518,7 @@ summary: Manage collections of records in Ruby on Rails
|
|
516
518
|
test_files:
|
517
519
|
- spec/base/accessors_spec.rb
|
518
520
|
- spec/base/after_record_update_spec.rb
|
521
|
+
- spec/base/before_record_update_spec.rb
|
519
522
|
- spec/base/behaviour_spec.rb
|
520
523
|
- spec/base/finding_records_spec.rb
|
521
524
|
- spec/base/inheritance_spec.rb
|