Class: CustomAttributes::ListFieldType

Inherits:
List show all
Includes:
Singleton
Defined in:
lib/custom_attributes/field_types/list_field_type.rb

Instance Method Summary collapse

Methods inherited from List

#edit_tag

Methods inherited from FieldType

#after_save_custom_value, available_types, #before_custom_field_save, #cast_custom_value, #cast_single_value, #cast_value, #edit_tag, find, #label, #name, #set_custom_field_value, #validate_single_value, #value_from_keyword

Instance Method Details

#possible_custom_value_options(custom_value) ⇒ Object



5
6
7
8
9
10
11
12
# File 'lib/custom_attributes/field_types/list_field_type.rb', line 5

def possible_custom_value_options(custom_value)
  options = possible_values_options(custom_value.custom_field)
  missing = [custom_value.value].flatten.reject(&:blank?) - options
  if missing.any?
    options += missing
  end
  options
end

#possible_values_options(custom_field, object = nil) ⇒ Object



14
15
16
# File 'lib/custom_attributes/field_types/list_field_type.rb', line 14

def possible_values_options(custom_field, object=nil)
  custom_field.possible_values
end

#validate_custom_field(custom_field) ⇒ Object



18
19
20
21
22
23
# File 'lib/custom_attributes/field_types/list_field_type.rb', line 18

def validate_custom_field(custom_field)
  errors = []
  errors << [:possible_values, :blank] if custom_field.possible_values.blank?
  errors << [:possible_values, :invalid] unless custom_field.possible_values.is_a? Array
  errors
end

#validate_custom_value(custom_value) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/custom_attributes/field_types/list_field_type.rb', line 25

def validate_custom_value(custom_value)
  values = Array.wrap(custom_value.value).reject {|value| value.to_s == ''}
  invalid_values = values - Array.wrap(custom_value.value_was) - custom_value.custom_field.possible_values
  if invalid_values.any?
    [::I18n.t('activerecord.errors.messages.inclusion')]
  else
    []
  end
end