Revision 0accaa2d
Added by Stephen Benjamin about 10 years ago
app/models/lookup_key.rb | ||
---|---|---|
|
||
def value_before_type_cast val
|
||
case key_type.to_sym
|
||
when :json
|
||
when :json, :array
|
||
val = JSON.dump val
|
||
when :yaml, :hash
|
||
val = YAML.dump val
|
||
val.sub!(/\A---\s*$\n/, '')
|
||
when :array
|
||
val = val.inspect
|
||
end unless key_type.blank?
|
||
val
|
||
end
|
test/fixtures/lookup_keys.yml | ||
---|---|---|
path: 'organization,location'
|
||
override: true
|
||
is_param: true
|
||
|
||
seven:
|
||
key: array_test
|
||
key_type: array
|
||
validator_rule:
|
||
default_value: "[{'hostname': 'test.example.com'}]"
|
||
path: 'organization,location'
|
||
override: true
|
||
is_param: true
|
test/unit/lookup_value_test.rb | ||
---|---|---|
end
|
||
end
|
||
|
||
test "should cast and uncast string containing a Hash" do
|
||
lk1 = LookupValue.new(:value => "---\n foo: bar", :match => "hostgroup=Common", :lookup_key => lookup_keys(:six))
|
||
assert lk1.save!
|
||
assert lk1.value.is_a? Hash
|
||
assert_equal lk1.value_before_type_cast, "foo: bar\n"
|
||
|
||
lk2 = LookupValue.new(:value => "{'foo': 'bar'}", :match => "environment=Production", :lookup_key => lookup_keys(:six))
|
||
assert lk2.save!
|
||
assert lk2.value.is_a? Hash
|
||
assert_equal lk2.value_before_type_cast, "foo: bar\n"
|
||
end
|
||
|
||
test "should cast and uncast string containing an Array" do
|
||
lk = LookupValue.new(:value => "[{\"foo\":\"bar\"},{\"baz\":\"qux\"},\"baz\"]", :match => "hostgroup=Common", :lookup_key => lookup_keys(:seven))
|
||
assert lk.save!
|
||
assert lk.value.is_a? Array
|
||
assert_equal lk.value_before_type_cast, "[{\"foo\":\"bar\"},{\"baz\":\"qux\"},\"baz\"]"
|
||
end
|
||
end
|
Also available in: Unified diff
fixes #4639 - array in lookup_value incorrectly displayed in form
(cherry picked from commit 801a90d196d363aadae7bf2be9347ff6e46e0471)