let custom values return typed values

this allows to have type-correct JSON output, e.g.:

12 instead of "12"
true instead of "1"
pull/2550/head
Jan Sandbrink 10 years ago
parent a5e58dc8d0
commit 05884417f7
  1. 26
      app/models/custom_value.rb
  2. 7
      lib/api/v3/utilities/custom_field_injector.rb
  3. 12
      spec/lib/api/v3/utilities/custom_field_injector_spec.rb

@ -38,6 +38,32 @@ class CustomValue < ActiveRecord::Base
after_initialize :set_default_value
# returns the value of this custom value, but converts it according to the field_format
# of the custom field beforehand
def typed_value
return nil if value.nil?
# strings may be blank, all other types can only be nil if their value is blank
return nil if value.blank? && !(['string', 'text'].include?(custom_field.field_format))
case custom_field.field_format
when 'int'
value.to_i
when 'float'
value.to_f
when 'date'
Date.iso8601(value)
when 'bool'
value == '1'
when 'user'
User.find(value)
when 'version'
Version.find(value)
else
value
end
end
def set_default_value
if new_record? && custom_field && (customized_type.blank? || (customized && customized.new_record?))
self.value ||= custom_field.default_value

@ -183,9 +183,10 @@ module API
def link_value_getter_for(custom_field, path_method)
-> (*) {
custom_value = represented.custom_value_for(custom_field)
value = custom_value.value if custom_value
value = custom_value.typed_value if custom_value
path = api_v3_paths.send(path_method, value.is_a?(String) ? value : value.id) if value
{ href: (api_v3_paths.send(path_method, value) if value) }
{ href: path }
}
end
@ -214,7 +215,7 @@ module API
def property_value_getter_for(custom_field)
-> (*) {
custom_value = custom_value_for(custom_field)
value = custom_value.value if custom_value
value = custom_value.typed_value if custom_value
if custom_field.field_format == 'text'
::API::Decorators::Formattable.new(value, format: 'plain')

@ -181,7 +181,7 @@ describe ::API::V3::Utilities::CustomFieldInjector do
let(:represented) {
double('represented',
custom_value_for: double('custom_value',
value: custom_value))
typed_value: custom_value))
}
let(:custom_value) { '' }
let(:modified_class) { Class.new(::API::Decorators::Single) }
@ -192,7 +192,7 @@ describe ::API::V3::Utilities::CustomFieldInjector do
end
context 'link custom field' do
let(:custom_value) { '2' }
let(:custom_value) { FactoryGirl.build(:user, id: 2) }
let(:field_format) { 'user' }
it_behaves_like 'has an untitled link' do
@ -221,7 +221,7 @@ describe ::API::V3::Utilities::CustomFieldInjector do
context 'int custom field' do
it_behaves_like 'injects property custom field' do
let(:field_format) { 'int' }
let(:custom_value) { '42' }
let(:custom_value) { 42 }
let(:json_value) { 42 }
let(:expected_setter) { json_value }
end
@ -230,7 +230,7 @@ describe ::API::V3::Utilities::CustomFieldInjector do
context 'float custom field' do
it_behaves_like 'injects property custom field' do
let(:field_format) { 'float' }
let(:custom_value) { '3.14' }
let(:custom_value) { 3.14 }
let(:json_value) { 3.14 }
let(:expected_setter) { json_value }
end
@ -239,7 +239,7 @@ describe ::API::V3::Utilities::CustomFieldInjector do
context 'bool custom field' do
it_behaves_like 'injects property custom field' do
let(:field_format) { 'bool' }
let(:custom_value) { '1' }
let(:custom_value) { true }
let(:json_value) { true }
let(:expected_setter) { json_value }
end
@ -248,7 +248,7 @@ describe ::API::V3::Utilities::CustomFieldInjector do
context 'date custom field' do
it_behaves_like 'injects property custom field' do
let(:field_format) { 'date' }
let(:custom_value) { Date.today.to_date.iso8601 }
let(:custom_value) { Date.today.to_date }
let(:json_value) { custom_value.to_date.iso8601 }
let(:expected_setter) { json_value }
end

Loading…
Cancel
Save