Sha256: 78a53379f47b9a00636178b1978a6e78623c64c90c000d2f0be128791c647835

Contents?: true

Size: 1.5 KB

Versions: 27

Compression:

Stored size: 1.5 KB

Contents

module CustomAttributes
  class BoolFieldType < List
    include Singleton

    self.multiple_supported = false

    def label
      'label_boolean'
    end

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

    # Boolean supports either True or False as value
    def possible_values_options(_custom_field, _object = nil)
      [[::I18n.t(:general_text_Yes), '1'], [::I18n.t(:general_text_No), '0']]
    end

    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

    # Boolean supports either checkbox, radiobutton or select field as edit tag
    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

    # Renders the edit tag as a simple check box
    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.content_tag('span', s, options)
    end
  end
end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
create_custom_attributes-0.6.3 lib/custom_attributes/field_types/bool_field_type.rb
create_custom_attributes-0.6.2 lib/custom_attributes/field_types/bool_field_type.rb
create_custom_attributes-0.6.1 lib/custom_attributes/field_types/bool_field_type.rb
create_custom_attributes-0.5.25 lib/custom_attributes/field_types/bool_field_type.rb
create_custom_attributes-0.5.24 lib/custom_attributes/field_types/bool_field_type.rb
create_custom_attributes-0.5.23 lib/custom_attributes/field_types/bool_field_type.rb
create_custom_attributes-0.5.22 lib/custom_attributes/field_types/bool_field_type.rb
create_custom_attributes-0.5.21 lib/custom_attributes/field_types/bool_field_type.rb
create_custom_attributes-0.5.20 lib/custom_attributes/field_types/bool_field_type.rb
create_custom_attributes-0.5.19 lib/custom_attributes/field_types/bool_field_type.rb
create_custom_attributes-0.5.18 lib/custom_attributes/field_types/bool_field_type.rb
create_custom_attributes-0.5.17 lib/custom_attributes/field_types/bool_field_type.rb
create_custom_attributes-0.5.16 lib/custom_attributes/field_types/bool_field_type.rb
create_custom_attributes-0.5.15 lib/custom_attributes/field_types/bool_field_type.rb
create_custom_attributes-0.5.14 lib/custom_attributes/field_types/bool_field_type.rb
create_custom_attributes-0.5.13 lib/custom_attributes/field_types/bool_field_type.rb
create_custom_attributes-0.5.12 lib/custom_attributes/field_types/bool_field_type.rb
create_custom_attributes-0.5.11 lib/custom_attributes/field_types/bool_field_type.rb
create_custom_attributes-0.5.10 lib/custom_attributes/field_types/bool_field_type.rb
create_custom_attributes-0.5.9 lib/custom_attributes/field_types/bool_field_type.rb