Sha256: a71c6442c87bb572d5d8933dfc309debaf0a722c6abe2c1ef32d4c992dd5e4a6

Contents?: true

Size: 1.84 KB

Versions: 160

Compression:

Stored size: 1.84 KB

Contents

require 'action_view/helpers/tags/checkable'

module ActionView
  module Helpers
    module Tags # :nodoc:
      class CheckBox < Base #:nodoc:
        include Checkable

        def initialize(object_name, method_name, template_object, checked_value, unchecked_value, options)
          @checked_value   = checked_value
          @unchecked_value = unchecked_value
          super(object_name, method_name, template_object, options)
        end

        def render
          options = @options.stringify_keys
          options["type"]     = "checkbox"
          options["value"]    = @checked_value
          options["checked"] = "checked" if input_checked?(object, options)

          if options["multiple"]
            add_default_name_and_id_for_value(@checked_value, options)
            options.delete("multiple")
          else
            add_default_name_and_id(options)
          end

          include_hidden = options.delete("include_hidden") { true }
          checkbox = tag("input", options)

          if include_hidden
            hidden = hidden_field_for_checkbox(options)
            hidden + checkbox
          else
            checkbox
          end
        end

        private

        def checked?(value)
          case value
          when TrueClass, FalseClass
            value == !!@checked_value
          when NilClass
            false
          when String
            value == @checked_value
          else
            if value.respond_to?(:include?)
              value.include?(@checked_value)
            else
              value.to_i == @checked_value.to_i
            end
          end
        end

        def hidden_field_for_checkbox(options)
          @unchecked_value ? tag("input", options.slice("name", "disabled", "form").merge!("type" => "hidden", "value" => @unchecked_value)) : "".html_safe
        end
      end
    end
  end
end

Version data entries

160 entries across 155 versions & 13 rubygems

Version Path
activejob-lock-0.0.1 rails/actionview/lib/action_view/helpers/tags/check_box.rb
actionpack-4.0.13 lib/action_view/helpers/tags/check_box.rb
actionview-4.1.9 lib/action_view/helpers/tags/check_box.rb
actionview-4.1.9.rc1 lib/action_view/helpers/tags/check_box.rb
actionpack-4.0.13.rc1 lib/action_view/helpers/tags/check_box.rb
actionview-4.2.0 lib/action_view/helpers/tags/check_box.rb
actionview-4.2.0.rc3 lib/action_view/helpers/tags/check_box.rb
actionview-4.2.0.rc2 lib/action_view/helpers/tags/check_box.rb
actionview-4.2.0.rc1 lib/action_view/helpers/tags/check_box.rb
actionview-4.1.7.1 lib/action_view/helpers/tags/check_box.rb
actionpack-4.0.11.1 lib/action_view/helpers/tags/check_box.rb
actionview-4.1.8 lib/action_view/helpers/tags/check_box.rb
actionpack-4.0.12 lib/action_view/helpers/tags/check_box.rb
actionview-4.2.0.beta4 lib/action_view/helpers/tags/check_box.rb
actionview-4.2.0.beta3 lib/action_view/helpers/tags/check_box.rb
actionview-4.1.7 lib/action_view/helpers/tags/check_box.rb
actionpack-4.0.11 lib/action_view/helpers/tags/check_box.rb
actionview-4.2.0.beta2 lib/action_view/helpers/tags/check_box.rb
nanumfont-rails-0.1 vendor/bundle/ruby/2.1.0/gems/actionview-4.1.6/lib/action_view/helpers/tags/check_box.rb
actionpack-4.0.10 lib/action_view/helpers/tags/check_box.rb