From ee6fc204bf45aa43d19290c42a56e80a2c5577f0 Mon Sep 17 00:00:00 2001 From: Tom Caspy Date: Mon, 18 May 2015 14:05:08 +0300 Subject: [PATCH] fixes #10528 - lookup value should allow false and 0 values --- app/models/lookup_value.rb | 8 +++++++- test/unit/lookup_value_test.rb | 14 ++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/app/models/lookup_value.rb b/app/models/lookup_value.rb index 0e103c2e045..3de72775f27 100644 --- a/app/models/lookup_value.rb +++ b/app/models/lookup_value.rb @@ -7,7 +7,7 @@ class LookupValue < ActiveRecord::Base belongs_to :lookup_key, :counter_cache => true validates :match, :presence => true, :uniqueness => {:scope => :lookup_key_id} - validates :value, :presence => true + validate :value_present? delegate :key, :to => :lookup_key before_validation :sanitize_match before_validation :validate_and_cast_value @@ -24,6 +24,12 @@ class LookupValue < ActiveRecord::Base scoped_search :on => :match, :complete_value => true scoped_search :in => :lookup_key, :on => :key, :rename => :lookup_key, :complete_value => true + def value_present? + if value.nil? + self.errors.add(:value, :blank) + end + end + def value=(val) if val.is_a?(HashWithIndifferentAccess) super(val.deep_to_hash) diff --git a/test/unit/lookup_value_test.rb b/test/unit/lookup_value_test.rb index ec617b94eab..ca8fcfdd609 100644 --- a/test/unit/lookup_value_test.rb +++ b/test/unit/lookup_value_test.rb @@ -146,4 +146,18 @@ def valid_attrs2 assert_equal lv.value_before_type_cast, "<%= [4,5,6] %>" assert_equal lv.value, "<%= [4,5,6] %>" end + + test "boolean lookup value should allow for false value" do + #boolean key + key = lookup_keys(:three) + value = LookupValue.new(:value => false, :match => "hostgroup=Common", :lookup_key_id => key.id) + assert value.valid? + end + + test "boolean lookup value should not allow for nil" do + #boolean key + key = lookup_keys(:three) + value = LookupValue.new(:value => nil, :match => "hostgroup=Common", :lookup_key_id => key.id) + refute value.valid? + end end