Sha256: b2f45cec7375dde290c54cd7d392abcf3ad4916262654ac699f6fe3ca0b70438

Contents?: true

Size: 1.52 KB

Versions: 2

Compression:

Stored size: 1.52 KB

Contents

#*************************************************************************************
# TOCOMMENT
#*************************************************************************************
module Caisson::Helpers::Form::Field
  class Checkbox < Caisson::Helpers::Form::Field::Base
    #*************************************************************************************
    # PUBLIC INSTANCE METHODS
    #*************************************************************************************
    def build(name, selected, options={})
      content = []
      content << hidden_field_tag(name, '0', id: nil)
      content << check_box_tag(name, '1', parse_selected(selected), generate_attributes(name, options))
      content << options.delete(:label)

      return content_tag(:label, content.join("\n").html_safe, :for => name.parameterize.underscore)
    end

    private

    #*************************************************************************************
    # PRIVATE INSTANCE METHODS
    #*************************************************************************************
    def generate_attributes(name, options)
      attributes = { class: ['checkbox', 'customized', options.delete(:class)].compact.join(' ') }
      attributes['title'] = options[:title] if options[:title]
      attributes['data-submit'] = 1 if options[:submit]

      return attributes
    end

    def parse_selected(selected)
      case selected
        when '0', 'false' then false
        when '1', 'true' then true
        else selected
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
caisson-0.0.3 lib/caisson/helpers/form/field/checkbox.rb
caisson-0.0.2 lib/caisson/helpers/form/field/checkbox.rb