Skip to content

Commit

Permalink
use_update option for inherit_attributes.
Browse files Browse the repository at this point in the history
  • Loading branch information
esbanarango committed Dec 29, 2015
1 parent ef3556b commit 027ab2d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/acts_as_inheritable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions spec/acts_as_inheritable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 027ab2d

Please sign in to comment.