Sha256: 3197828438ba1e41132748a36c76b750b6b65daa1f292b18477723556eb557c7

Contents?: true

Size: 1.43 KB

Versions: 1

Compression:

Stored size: 1.43 KB

Contents

require "input_css/version"

# Modifies the 'options' of the tag helper so that, by default,
# there's a CSS class attribute based on the type attribute
# (Note: only applies to input fields)
module InputCSS
  def tag(name=nil, options=nil, open=false, escape=true)
    if name.present?
      options = css_options_for_tag(name, options)
    end
    super
  end

  def css_options_for_tag(name, options)
    options = HashWithIndifferentAccess.new(options)
    return options if options[:type] == 'hidden'

    # alter CSS class based on type
    # (only for <input ... /> tags)
    if name.to_s.downcase =~ /^input$/
      type, css = options[:type], options[:class]
      type = 'text' if type == 'password'
      options[:class] = "#{css.to_s} #{type.to_s}".gsub!(/^\s*/, '') unless css && css.split.include?(type)
    end
    options
  end
  module_function :css_options_for_tag
end

module ActionView
  module Helpers
    module TagHelper
      if respond_to?(:prepend) # Rails 5 compat
        prepend InputCSS
      else
        def tag_with_default_css(name, options=nil, open=false, escape=true)
          options = InputCSS.css_options_for_tag(name, options)
          tag_without_default_css(name, options, open, escape)
        end
        alias_method_chain :tag, :default_css
      end
    end

    class InstanceTag
      alias_method :tag_without_error_wrapping, :tag_with_default_css
    end if defined?(InstanceTag) # Rails 3 compat
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
input_css-0.2.0 lib/input_css.rb