Project

General

Profile

« Previous | Next » 

Revision 22a653af

Added by Tom Caspy over 8 years ago

fixes #11095 - lookup keys do not get saved to db on setter invokation

(cherry picked from commit c6b4236846decc58583143f30d537a27b4848953)

View differences:

app/models/concerns/host_common.rb
def lookup_values_attributes=(lookup_values_attributes)
lookup_values_attributes.each_value do |attribute|
attr = attribute.dup
if attr.has_key? :id
lookup_value = lookup_values.find attr.delete(:id)
if id = attr.delete(:id)
lookup_value = self.lookup_values.to_a.select{|i| i.id == id}.first
if lookup_value
mark_for_destruction = ActiveRecord::ConnectionAdapters::Column.value_to_boolean attr.delete(:_destroy)
lookup_value.attributes = attr
mark_for_destruction ? lookup_values.delete(lookup_value) : lookup_value.save!
lookup_value.mark_for_destruction if mark_for_destruction
end
elsif !ActiveRecord::ConnectionAdapters::Column.value_to_boolean attr.delete(:_destroy)
LookupValue.create(attr.merge(:match => lookup_value_match, :host_or_hostgroup => self))
self.lookup_values.build(attr.merge(:match => lookup_value_match, :host_or_hostgroup => self))
end
end
end
app/models/lookup_value.rb
validate :validate_list, :validate_regexp, :ensure_fqdn_exists, :ensure_hostgroup_exists
attr_accessor :host_or_hostgroup
attr_writer :managed_id, :hostgroup_id
serialize :value
attr_name :match
test/unit/host_test.rb
assert host.operatingsystem.architectures.include?(host.architecture), "no association between operatingsystem and architecture"
end
context "lookup value attributes" do
test "invoking lookup_values_attributes= does not save lookup values in db until #save is invoked" do
host = FactoryGirl.create(:host)
assert_no_difference('LookupValue.count') do
host.lookup_values_attributes = {"new_123456" => {"lookup_key_id" => lookup_keys(:complex).id, "value"=>"some_value", "match" => "fqdn=abc.mydomain.net"}}
end
assert_difference('LookupValue.count') do
host.save
end
end
test "same works for destruction of lookup keys" do
host = FactoryGirl.create(:host, :lookup_values_attributes => {"new_123456" => {"lookup_key_id" => lookup_keys(:complex).id, "value"=>"some_value", "match" => "fqdn=abc.mydomain.net"}})
lookup_value = host.lookup_values.first
assert_no_difference('LookupValue.count') do
host.lookup_values_attributes = {"lv" => {:id => lookup_value.id, :_destroy => true}}
end
assert_difference('LookupValue.count', -1) do
host.save
end
end
end
private
def parse_json_fixture(relative_path)

Also available in: Unified diff