Module: Rich::I18n::Formtastic

Defined in:
lib/rich/i18n/formtastic.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



6
7
8
# File 'lib/rich/i18n/formtastic.rb', line 6

def self.included(base)
  base.alias_method_chain :input, :enrichments
end

Instance Method Details

#input_with_enrichments(*args) ⇒ Object



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
39
40
41
42
43
44
45
46
47
# File 'lib/rich/i18n/formtastic.rb', line 10

def input_with_enrichments(*args)
  object      = ((@object.class.name if @object) || @object_name.match(/\[(\w+)_attributes\]/).captures.first).camelize
  method_arg  = args.shift
  method      = method_arg.to_s
  options     = args.extract_options!
  wrapper_tag = options.delete(:wrapper_tag)

  unless options.include?(:label)
    keys = [:"label.#{object}.#{method}", :"label.#{method}", :"word.#{method}"]

    if @options.include?(:name)
      keys.unshift :"label.forms.(#{@options[:name]}).#{method}"
      keys.unshift :"label.forms.(#{@options[:name]}).#{object}.#{method}"
    end

    options[:label] = keys.shift.t(:default => keys, :capitalize => true).to_s
  end

  unless options.include?(:seatholder)
    keys = [:"seatholder.#{object}.#{method}", :"seatholder.#{method}", ""]

    if @options.include?(:name)
      keys.unshift :"seatholder.forms.(#{@options[:name]}).#{method}"
      keys.unshift :"seatholder.forms.(#{@options[:name]}).#{object}.#{method}"
    end

    seatholder = keys.shift.t :default => keys, :capitalize => true
    options[:seatholder] = seatholder.to_s unless seatholder.empty?
  end

  (options[:input_html] ||= {}).store :seatholder, options.delete(:seatholder) unless @object && @object.respond_to?(:errors) && !@object.errors[method.to_sym].blank?

  if (output = input_without_enrichments method_arg, options) and wrapper_tag
    output.gsub(/^\<li/, "<#{wrapper_tag}").gsub(/\<\/li\>$/, "</#{wrapper_tag}>")
  else
    output
  end
end