Skip to content

Commit

Permalink
fixes #10528 - lookup value should allow false and 0 values
Browse files Browse the repository at this point in the history
  • Loading branch information
unorthodoxgeek authored and Dominic Cleal committed May 19, 2015
1 parent 0fd7412 commit ee6fc20
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
8 changes: 7 additions & 1 deletion app/models/lookup_value.rb
Expand Up @@ -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
Expand All @@ -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)
Expand Down
14 changes: 14 additions & 0 deletions test/unit/lookup_value_test.rb
Expand Up @@ -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

0 comments on commit ee6fc20

Please sign in to comment.