acts_as_inheritable 0.6.0 → 0.7.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7ae0ca518afeef42a5feed9fa8fb300118aa7544
4
- data.tar.gz: ae752687e79850a03b92bca3d6ff668a212b916a
3
+ metadata.gz: 292b5674ba1ed80d1fd73159a6d02473834c5505
4
+ data.tar.gz: 3bcd5e2486bb25f437a161d1e7b847b04efdab6b
5
5
  SHA512:
6
- metadata.gz: bcdec1699a2fed4f76e282e1fc42fe24195f4bcbec246803e1070901e641febe881bebce3166fa439a0d183f40b2dae65f5f1d8c7e4e2bd4ba08c1c89f4fdbcd
7
- data.tar.gz: 9b210ad22a2ed22641dad41299ca2d2f66cfdc4611153c739aea076f2229ddcfe2fa00cbee3e60b18ebec63ebce5e1baf0287fc2bb6aaed67af1d8e08b24cca0
6
+ metadata.gz: 3299b35236bac0283efa19d797fa3f4a60766d0b9a2e6cccbe1984b84fc8557be2560a847b03ea95431089e9ad2c8d687e2d76cec99bb3b09c75b28a848fc33d
7
+ data.tar.gz: 04afa394b42d8dcac98c8af0702f87410abdd6280d8adf3aae410c038e24659c2c1a627e80967d25c97cab17a30c07a4d912faa1f7a07beb199c69fca4aeb940
data/README.md CHANGED
@@ -68,14 +68,14 @@ son.favorite_color # => Green
68
68
  By adding `acts_as_inheritable` to your models you will have access to these methods:
69
69
 
70
70
  #### inherit_attributes
71
- > Signature `inherit_attributes(force = false, not_force_for = [], use_update = false)`
71
+ > Signature `inherit_attributes(force = false, not_force_for = [], method_to_update = nil)`
72
72
 
73
73
  By default this method will only set values that are [blank?](http://api.rubyonrails.org/classes/Object.html#method-i-blank-3F).
74
74
 
75
75
  ###### params
76
76
  - `force`: Default to true. Set the attribute even if it's _present_.
77
77
  - `not_force_for`: Default to empty array. When setting `force` to _true_, you can also specify the attributes you don't want to overwrite.
78
- - `use_update`: Default to false. Uses the `update_attributes` method instead of the normal asignation (`"#{attribute}="`). This is useful if you're using __inherit_attributes__ inside a `after_save` callback for example.
78
+ - `method_to_update`: Default to nil. Uses the specified method (`update_attributes` or `update_columns`) instead of the normal asignation (`"#{attribute}="`). This is useful if you're using __inherit_attributes__ inside an `after_save` callback or if you want to skip validations for example.
79
79
 
80
80
  #### inherit_relations
81
81
  > Signature `inherit_relations(model_parent = send(:parent), current = self)`
@@ -73,14 +73,15 @@ module ActsAsInheritable
73
73
  parent_name
74
74
  end
75
75
 
76
- def inherit_attributes(force = false, not_force_for = [], use_update = false)
76
+ def inherit_attributes(force = false, not_force_for = [], method_to_update = nil)
77
+ available_methods = ['update_attributes', 'update_columns']
77
78
  if has_parent? && self.class.inheritable_configuration[:attributes]
78
79
  # Attributes
79
80
  self.class.inheritable_configuration[:attributes].each do |attribute|
80
81
  current_val = send(attribute)
81
82
  if (force && !not_force_for.include?(attribute)) || current_val.blank?
82
- if use_update
83
- update_attributes(attribute => parent.send(attribute))
83
+ if method_to_update && available_methods.include?(method_to_update)
84
+ send(method_to_update, {attribute => parent.send(attribute)})
84
85
  else
85
86
  send("#{attribute}=", parent.send(attribute))
86
87
  end
@@ -1,3 +1,3 @@
1
1
  module ActsAsInheritable
2
- VERSION = '0.6.0'
2
+ VERSION = '0.7.0'
3
3
  end
@@ -53,15 +53,26 @@ RSpec.describe "ActiveRecord::Base model with #acts_as_inheritable" do
53
53
  end
54
54
  end
55
55
  end
56
- context 'when `use_update` is set to true' do
56
+ context 'when `method_to_update` is used' do
57
57
  let(:person){ create(:person, :with_parent, favorite_color: nil, last_name: nil, soccer_team: nil) }
58
58
  let!(:person_parent) { person.parent }
59
- it 'inherits values from his parent even if those attributes have a value' do
60
- person.inherit_attributes false, [], true
61
- person.reload
62
- expect(person.favorite_color).to eq person_parent.favorite_color
63
- expect(person.last_name).to eq person_parent.last_name
64
- expect(person.soccer_team).to eq person_parent.soccer_team
59
+ context 'when `method_to_update` is `update_attributes`' do
60
+ it 'inherits values from his parent and saves the record' do
61
+ person.inherit_attributes false, [], 'update_attributes'
62
+ person.reload
63
+ expect(person.favorite_color).to eq person_parent.favorite_color
64
+ expect(person.last_name).to eq person_parent.last_name
65
+ expect(person.soccer_team).to eq person_parent.soccer_team
66
+ end
67
+ end
68
+ context 'when `method_to_update` is `update_columns`' do
69
+ it 'inherits values from his parent and saves the record' do
70
+ person.inherit_attributes false, [], 'update_columns'
71
+ person.reload
72
+ expect(person.favorite_color).to eq person_parent.favorite_color
73
+ expect(person.last_name).to eq person_parent.last_name
74
+ expect(person.soccer_team).to eq person_parent.soccer_team
75
+ end
65
76
  end
66
77
  end
67
78
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: acts_as_inheritable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Esteban Arango Medina