Module: ActionView::Helpers::TagHelper

Defined in:
lib/form_helper_css/form_helper_css.rb

Constant Summary collapse

FORM_HELPER_CSS_OPTIONS =
{:append => false}

Instance Method Summary collapse

Instance Method Details

#content_tag_string_with_css(name, content, options, escape = true) ⇒ Object



45
46
47
# File 'lib/form_helper_css/form_helper_css.rb', line 45

def (name, content, options, escape=true)
  (name, content, css_options_for_tag(name, options || {}), escape)
end

#css_options_for_tag(name, options = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/form_helper_css/form_helper_css.rb', line 9

def css_options_for_tag(name, options={})
  name = name.to_sym
  options = options.stringify_keys
  class_was_array = options['class'].is_a?(Array)
  options['class'] = options['class'].join(' ') if class_was_array
  if FORM_HELPER_CSS_OPTIONS[:append] == false && options.has_key?('class')
    options['class'] = options['class'].split(' ') if class_was_array
    return options
  elsif name == :input and options['type']
    return options if (options['type'] == 'hidden')
    if FORM_HELPER_CSS_OPTIONS[:append] && options['class']
      options['class'] << ' ' + options['type'].to_s.dup
    else
      options['class'] = options['type'].to_s.dup
    end
    options['class'] << ' button' if ['submit', 'reset'].include? options['type']
    options['class'] << ' text' if options['type'] == 'password'
  elsif name == :textarea
    if FORM_HELPER_CSS_OPTIONS[:append] && options['class']
      options['class'] << ' text'
    else
      options['class'] = 'text'
    end
  end
  if options['class']
    options['class'] = options['class'].to_s.strip.split(/\s+/).uniq.join(' ') # de-dup the class list
  end
  options['class'] = options['class'].split(' ') if class_was_array
  options
end

#tag_with_css(name, options = nil, open = false, escape = true) ⇒ Object



40
41
42
# File 'lib/form_helper_css/form_helper_css.rb', line 40

def tag_with_css(name, options=nil, open=false, escape=true)
  tag_without_css(name, css_options_for_tag(name, options || {}), open, escape)
end