acts_as_inheritable 0.5.0 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +2 -1
- data/lib/acts_as_inheritable.rb +6 -3
- data/lib/acts_as_inheritable/version.rb +1 -1
- data/spec/acts_as_inheritable_spec.rb +11 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7ae0ca518afeef42a5feed9fa8fb300118aa7544
|
4
|
+
data.tar.gz: ae752687e79850a03b92bca3d6ff668a212b916a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bcdec1699a2fed4f76e282e1fc42fe24195f4bcbec246803e1070901e641febe881bebce3166fa439a0d183f40b2dae65f5f1d8c7e4e2bd4ba08c1c89f4fdbcd
|
7
|
+
data.tar.gz: 9b210ad22a2ed22641dad41299ca2d2f66cfdc4611153c739aea076f2229ddcfe2fa00cbee3e60b18ebec63ebce5e1baf0287fc2bb6aaed67af1d8e08b24cca0
|
data/README.md
CHANGED
@@ -68,13 +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 = [], use_update = false)`
|
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
79
|
|
79
80
|
#### inherit_relations
|
80
81
|
> Signature `inherit_relations(model_parent = send(:parent), current = self)`
|
data/lib/acts_as_inheritable.rb
CHANGED
@@ -73,13 +73,17 @@ 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 = [], use_update = false)
|
77
77
|
if has_parent? && self.class.inheritable_configuration[:attributes]
|
78
78
|
# Attributes
|
79
79
|
self.class.inheritable_configuration[:attributes].each do |attribute|
|
80
80
|
current_val = send(attribute)
|
81
81
|
if (force && !not_force_for.include?(attribute)) || current_val.blank?
|
82
|
-
|
82
|
+
if use_update
|
83
|
+
update_attributes(attribute => parent.send(attribute))
|
84
|
+
else
|
85
|
+
send("#{attribute}=", parent.send(attribute))
|
86
|
+
end
|
83
87
|
end
|
84
88
|
end
|
85
89
|
end
|
@@ -91,5 +95,4 @@ module ActsAsInheritable
|
|
91
95
|
# Extend ActiveRecord's functionality
|
92
96
|
ActiveRecord::Base.send :extend, ActsAsInheritable
|
93
97
|
end
|
94
|
-
|
95
98
|
end
|
@@ -53,6 +53,17 @@ 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
|
57
|
+
let(:person){ create(:person, :with_parent, favorite_color: nil, last_name: nil, soccer_team: nil) }
|
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
|
65
|
+
end
|
66
|
+
end
|
56
67
|
end
|
57
68
|
|
58
69
|
describe '#inherit_relations' do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: acts_as_inheritable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Esteban Arango Medina
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-12-
|
11
|
+
date: 2015-12-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|