Sha256: 44a2067d0b792a665bd503c3884f4174077660f7acce2cca6655d87e6982e6f0

Contents?: true

Size: 1.9 KB

Versions: 114

Compression:

Stored size: 1.9 KB

Contents

# frozen_string_literal: true

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?(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

114 entries across 106 versions & 7 rubygems

Version Path
zuora_connect_ui-0.8.2 vendor/ruby/2.6.0/gems/actionview-5.2.3/lib/action_view/helpers/tags/check_box.rb
zuora_connect_ui-0.8.2 vendor/ruby/2.6.0/gems/actionview-6.0.0/lib/action_view/helpers/tags/check_box.rb
actionview-6.0.0 lib/action_view/helpers/tags/check_box.rb
zuora_connect_ui-0.8.1 vendor/ruby/2.6.0/gems/actionview-5.2.3/lib/action_view/helpers/tags/check_box.rb
zuora_connect_ui-0.8.0 vendor/ruby/2.6.0/gems/actionview-5.2.3/lib/action_view/helpers/tags/check_box.rb
spiral_form-0.1.1 vendor/bundle/gems/actionview-5.2.3/lib/action_view/helpers/tags/check_box.rb
spiral_form-0.1.0 vendor/bundle/gems/actionview-5.2.3/lib/action_view/helpers/tags/check_box.rb
actionview-6.0.0.rc2 lib/action_view/helpers/tags/check_box.rb
zuora_connect_ui-0.7.1 vendor/ruby/2.6.0/gems/actionview-5.2.3/lib/action_view/helpers/tags/check_box.rb
zuora_connect_ui-0.7.0 vendor/ruby/2.6.0/gems/actionview-5.2.3/lib/action_view/helpers/tags/check_box.rb
jets-1.9.7 vendor/rails/actionview/lib/action_view/helpers/tags/check_box.rb
jets-1.9.6 vendor/rails/actionview/lib/action_view/helpers/tags/check_box.rb
jets-1.9.5 vendor/rails/actionview/lib/action_view/helpers/tags/check_box.rb
jets-1.9.4 vendor/rails/actionview/lib/action_view/helpers/tags/check_box.rb
jets-1.9.3 vendor/rails/actionview/lib/action_view/helpers/tags/check_box.rb
jets-1.9.2 vendor/rails/actionview/lib/action_view/helpers/tags/check_box.rb
jets-1.9.1 vendor/rails/actionview/lib/action_view/helpers/tags/check_box.rb
jets-1.9.0 vendor/rails/actionview/lib/action_view/helpers/tags/check_box.rb
jets-1.8.14 vendor/rails/actionview/lib/action_view/helpers/tags/check_box.rb
jets-1.8.13 vendor/rails/actionview/lib/action_view/helpers/tags/check_box.rb