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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8105f9981da0c30ff4cea3a5de92bf724b6a53d2
4
- data.tar.gz: 74efbf9e4cfbe05de9745eaae179f69317b91b0a
3
+ metadata.gz: 6a1f2fc09b1f3aa4398ce4a15a126112e3dac101
4
+ data.tar.gz: 9aa4a6bff6c18594e8f4dbabb3efd4abde222a94
5
5
  SHA512:
6
- metadata.gz: 5129d5f5f6104a49305680d9093a4b33f5efc322397d63fc2935a0eafea2900ecf29d489939a2f55d2c73cff035ed76c24eaccf9512a0acce08b8aeed58356cc
7
- data.tar.gz: 897479481ce00c0e9b20305bac81b69d2433b83aee09f4798b1afe3c0a3c5ddb244a0a0fbab49f30618108dde7eabb8f25283766a2efa21c39fefbe2674dbdb9
6
+ metadata.gz: cfabb7c0d78aa856f3a10eda8260e1865d6ee83727ac717d0b0b9115b8d0c2dc573ebc40de763ed39641e91f2881cb1dfaac02767c470edc06afa4ef337205e3
7
+ data.tar.gz: 8d08ada30d3c68e32f62e97df0a8e4795ab8f8050551e30ba9d81e10216b124ed85c99c78202d4a9cccf7ce48a3dc25951f08a79cfb9e163a299abeffda457ca
data/CHANGELOG.md ADDED
@@ -0,0 +1,16 @@
1
+ CHANGELOG
2
+ =========
3
+
4
+ 2015-11-03 - v0.8.3
5
+ -------------------
6
+
7
+ ### Added
8
+ * before\_record\_update hook
9
+
10
+
11
+ 2015-11-03 - v0.8.2
12
+ -------------------
13
+
14
+ ### Added
15
+ * Smarter find on collection object
16
+
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
@@ -1,3 +1,3 @@
1
1
  module RecordCollection
2
- VERSION = "0.8.2"
2
+ VERSION = "0.8.3"
3
3
  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.2
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