Class: CustomAttributes::BoolFieldType

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

Instance Method Summary collapse

Methods inherited from FieldType

#after_save_custom_value, available_types, #before_custom_field_save, #cast_custom_value, #cast_value, find, #name, #possible_custom_value_options, #set_custom_field_value, #validate_custom_field, #validate_custom_value, #value_from_keyword

Instance Method Details

#cast_single_value(_custom_field, value, _customizable = nil) ⇒ Object



11
12
13
# File 'lib/custom_attributes/field_types/bool_field_type.rb', line 11

def cast_single_value(_custom_field, value, _customizable = nil)
  value == '1'
end

#edit_tag(view, tag_id, tag_name, custom_value, options = {}) ⇒ Object

Boolean supports either checkbox, radiobutton or select field as edit tag



29
30
31
32
33
34
35
36
37
38
# File 'lib/custom_attributes/field_types/bool_field_type.rb', line 29

def edit_tag(view, tag_id, tag_name, custom_value, options = {})
  case custom_value.custom_field.edit_tag_style
  when 'check_box'
    single_check_box_edit_tag(view, tag_id, tag_name, custom_value, options)
  when 'radio'
    check_box_edit_tag(view, tag_id, tag_name, custom_value, options)
  else
    select_edit_tag(view, tag_id, tag_name, custom_value, options)
  end
end

#labelObject



7
8
9
# File 'lib/custom_attributes/field_types/bool_field_type.rb', line 7

def label
  'label_boolean'
end

#possible_values_options(_custom_field, _object = nil) ⇒ Object

Boolean supports either True or False as value



16
17
18
# File 'lib/custom_attributes/field_types/bool_field_type.rb', line 16

def possible_values_options(_custom_field, _object = nil)
  [[::I18n.t(:general_text_Yes), '1'], [::I18n.t(:general_text_No), '0']]
end

#single_check_box_edit_tag(view, tag_id, tag_name, custom_value, options = {}) ⇒ Object

Renders the edit tag as a simple check box



41
42
43
44
45
46
# File 'lib/custom_attributes/field_types/bool_field_type.rb', line 41

def single_check_box_edit_tag(view, tag_id, tag_name, custom_value, options = {})
  s = ''.html_safe
  s << view.hidden_field_tag(tag_name, '0', id: nil)
  s << view.check_box_tag(tag_name, '1', custom_value.value.to_s == '1', id: tag_id)
  view.('span', s, options)
end

#validate_single_value(_custom_field, value, _customizable = nil) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/custom_attributes/field_types/bool_field_type.rb', line 20

def validate_single_value(_custom_field, value, _customizable = nil)
  if value == '0' || value == '1'
    []
  else
    [::I18n.t('activerecord.errors.messages.not_a_boolean')]
  end
end