diff options
Diffstat (limited to 'app/models/custom_field.rb')
| -rw-r--r-- | app/models/custom_field.rb | 69 |
1 files changed, 57 insertions, 12 deletions
diff --git a/app/models/custom_field.rb b/app/models/custom_field.rb index deb0326f8..de090fac2 100644 --- a/app/models/custom_field.rb +++ b/app/models/custom_field.rb @@ -2,10 +2,11 @@ class CustomField < ApplicationModel extend Enumerize belongs_to :workgroup - enumerize :field_type, in: %i{list integer string attachment} + enumerize :field_type, in: %i{list integer float string attachment} validates :name, uniqueness: {scope: [:resource_type, :workgroup_id]} validates :code, uniqueness: {scope: [:resource_type, :workgroup_id], case_sensitive: false}, presence: true + validates :workgroup, :resource_type, :field_type, presence: true class Collection < HashWithIndifferentAccess def initialize object, workgroup=nil @@ -47,7 +48,7 @@ class CustomField < ApplicationModel end def options - @custom_field.options || {} + @custom_field.options&.stringify_keys || {} end def validate @@ -59,6 +60,10 @@ class CustomField < ApplicationModel @valid end + def required? + !!options["required"] + end + def value @raw_value end @@ -76,7 +81,8 @@ class CustomField < ApplicationModel end def errors_key - "custom_fields.#{code}" + # this must match the ID used in the inputs + "custom_field_#{code}" end def to_hash @@ -91,7 +97,7 @@ class CustomField < ApplicationModel end def preprocess_value_for_assignment val - val + val || default_value end def render_partial @@ -111,7 +117,7 @@ class CustomField < ApplicationModel @instance.custom_field end - delegate :custom_field, :value, :options, to: :@instance + delegate :custom_field, :value, :options, :required?, to: :@instance delegate :code, :name, :field_type, to: :custom_field def to_s @@ -122,7 +128,7 @@ class CustomField < ApplicationModel protected def form_input_id - "custom_field_#{code}" + "custom_field_#{code}".to_sym end def form_input_name @@ -144,26 +150,64 @@ class CustomField < ApplicationModel class Integer < Base def value - @raw_value&.to_i + @raw_value.present? ? @raw_value.to_i : nil end def validate @valid = true - return if @raw_value.is_a?(Fixnum) || @raw_value.is_a?(Float) - unless @raw_value.to_s =~ /\A\d*\Z/ + return if @raw_value.is_a?(Fixnum) + unless @raw_value.to_s =~ /\A-?\d*\Z/ @owner.errors.add errors_key, "'#{@raw_value}' is not a valid integer" @valid = false end end + + class Input < Base::Input + def form_input_options + super.update({ + as: :integer + }) + end + end + end + + class Float < Integer + def value + @raw_value.present? ? @raw_value.to_f : nil + end + + def validate + @valid = true + return if @raw_value.is_a?(Fixnum) || @raw_value.is_a?(Float) + unless @raw_value.to_s =~ /\A-?\d*(\.\d+)?\Z/ + @owner.errors.add errors_key, "'#{@raw_value}' is not a valid float" + @valid = false + end + end + + class Input < Base::Input + def form_input_options + super.update({ + as: :float + }) + end + end end class List < Integer def validate super return unless value.present? - unless value >= 0 && value < options["list_values"].size - @owner.errors.add errors_key, "'#{@raw_value}' is not a valid value" - @valid = false + if options["list_values"].is_a?(Hash) + unless options["list_values"].keys.map(&:to_s).include?(value.to_s) + @owner.errors.add errors_key, "'#{@raw_value}' is not a valid value" + @valid = false + end + else + unless value >= 0 && value < options["list_values"].size + @owner.errors.add errors_key, "'#{@raw_value}' is not a valid value" + @valid = false + end end end @@ -178,6 +222,7 @@ class CustomField < ApplicationModel collection = options["list_values"] collection = collection.each_with_index.to_a if collection.is_a?(Array) collection = collection.map(&:reverse) if collection.is_a?(Hash) + collection = [["", ""]] + collection unless required? super.update({ selected: value, collection: collection |
