diff --git a/lib/acts_as_inheritable.rb b/lib/acts_as_inheritable.rb index 18a9e90..3ce1b7c 100644 --- a/lib/acts_as_inheritable.rb +++ b/lib/acts_as_inheritable.rb @@ -73,13 +73,17 @@ def verify_parent_name(new_relation, model_parent) parent_name end - def inherit_attributes(force = false, not_force_for = []) + def inherit_attributes(force = false, not_force_for = [], use_update = false) if has_parent? && self.class.inheritable_configuration[:attributes] # Attributes self.class.inheritable_configuration[:attributes].each do |attribute| current_val = send(attribute) if (force && !not_force_for.include?(attribute)) || current_val.blank? - send("#{attribute}=", parent.send(attribute)) + if use_update + update_attributes(attribute => parent.send(attribute)) + else + send("#{attribute}=", parent.send(attribute)) + end end end end diff --git a/spec/acts_as_inheritable_spec.rb b/spec/acts_as_inheritable_spec.rb index cb321a3..49bfe2b 100644 --- a/spec/acts_as_inheritable_spec.rb +++ b/spec/acts_as_inheritable_spec.rb @@ -53,6 +53,17 @@ class Person < ActiveRecord::Base end end end + context 'when `use_update` is set to true' do + let(:person){ create(:person, :with_parent, favorite_color: nil, last_name: nil, soccer_team: nil) } + let!(:person_parent) { person.parent } + it 'inherits values from his parent even if those attributes have a value' do + person.inherit_attributes false, [], true + person.reload + expect(person.favorite_color).to eq person_parent.favorite_color + expect(person.last_name).to eq person_parent.last_name + expect(person.soccer_team).to eq person_parent.soccer_team + end + end end describe '#inherit_relations' do