Sha256: 790a26afe0d5158dde21de986603a8bb9242da52b1d74b345ad6a022775d052e

Contents?: true

Size: 1.92 KB

Versions: 78

Compression:

Stored size: 1.92 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, "autocomplete" => "off")) : "".html_safe
          end
      end
    end
  end
end

Version data entries

78 entries across 75 versions & 8 rubygems

Version Path
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/actionview-7.1.3.4/lib/action_view/helpers/tags/check_box.rb
actionview-8.0.1 lib/action_view/helpers/tags/check_box.rb
actionview-8.0.0.1 lib/action_view/helpers/tags/check_box.rb
actionview-7.2.2.1 lib/action_view/helpers/tags/check_box.rb
actionview-7.1.5.1 lib/action_view/helpers/tags/check_box.rb
actionview-7.0.8.7 lib/action_view/helpers/tags/check_box.rb
actionview-8.0.0 lib/action_view/helpers/tags/check_box.rb
actionview-7.2.2 lib/action_view/helpers/tags/check_box.rb
actionview-7.1.5 lib/action_view/helpers/tags/check_box.rb
actionview-8.0.0.rc2 lib/action_view/helpers/tags/check_box.rb
actionview-7.2.1.2 lib/action_view/helpers/tags/check_box.rb
actionview-7.1.4.2 lib/action_view/helpers/tags/check_box.rb
actionview-7.0.8.6 lib/action_view/helpers/tags/check_box.rb
actionview-8.0.0.rc1 lib/action_view/helpers/tags/check_box.rb
actionview-7.2.1.1 lib/action_view/helpers/tags/check_box.rb
actionview-7.1.4.1 lib/action_view/helpers/tags/check_box.rb
actionview-7.0.8.5 lib/action_view/helpers/tags/check_box.rb
actionview-8.0.0.beta1 lib/action_view/helpers/tags/check_box.rb
omg-actionview-8.0.0.alpha9 lib/action_view/helpers/tags/check_box.rb
omg-actionview-8.0.0.alpha8 lib/action_view/helpers/tags/check_box.rb