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 +4 -4
- data/README.md +2 -2
- data/lib/acts_as_inheritable.rb +4 -3
- data/lib/acts_as_inheritable/version.rb +1 -1
- data/spec/acts_as_inheritable_spec.rb +18 -7
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 292b5674ba1ed80d1fd73159a6d02473834c5505
|
4
|
+
data.tar.gz: 3bcd5e2486bb25f437a161d1e7b847b04efdab6b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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 = [],
|
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
|
-
- `
|
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)`
|
data/lib/acts_as_inheritable.rb
CHANGED
@@ -73,14 +73,15 @@ module ActsAsInheritable
|
|
73
73
|
parent_name
|
74
74
|
end
|
75
75
|
|
76
|
-
def inherit_attributes(force = false, not_force_for = [],
|
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
|
83
|
-
|
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
|
@@ -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 `
|
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
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
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
|