Module: WaNew::ClientValidate::ActionView::Helpers::FormHelper

Defined in:
lib/wanew/client_validate/action_view/form_helper.rb

Instance Method Summary collapse

Instance Method Details

#apply_form_for_options!(object_or_array, options) ⇒ Object



36
37
38
39
# File 'lib/wanew/client_validate/action_view/form_helper.rb', line 36

def apply_form_for_options!(object_or_array, options)
  super
  options[:html][:validate] = true if options[:validate]
end

#fields_for(record_or_name_or_array, *args, &block) ⇒ Object



41
42
43
44
45
# File 'lib/wanew/client_validate/action_view/form_helper.rb', line 41

def fields_for(record_or_name_or_array, *args, &block)
  output = super
  @validators.merge!(args.last[:validators]) if @validators
  output
end

#form_for(record_or_name_or_array, *args, &proc) ⇒ Object

Raises:

  • (ArgumentError)


3
4
5
6
7
8
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
# File 'lib/wanew/client_validate/action_view/form_helper.rb', line 3

def form_for(record_or_name_or_array, *args, &proc)
    raise ArgumentError, "Missing block" unless block_given?

    options = args.extract_options!

    case record_or_name_or_array
    when String, Symbol
      ActiveSupport::Deprecation.warn("Using form_for(:name, @resource) is deprecated. Please use form_for(@resource, :as => :name) instead.", caller) unless args.empty?
      object_name = record_or_name_or_array
    when Array
      object = record_or_name_or_array.last
      object_name = options[:as] || ActiveModel::Naming.singular(object)
      apply_form_for_options!(record_or_name_or_array, options)
      args.unshift object
    else
      object = record_or_name_or_array
      object_name = options[:as] || ActiveModel::Naming.singular(object)
      apply_form_for_options!([object], options)
      args.unshift object
    end

    (options[:html] ||= {})[:remote] = true if options.delete(:remote)

		@validators = {}
    # Order matters here. Rails mutates the options object
    script = client_form_settings(object, options)
    form   = super(record_or_name_or_array, *(args << options), &proc)
    # Because of the load order requirement above this sub is necessary
    # Would be nice to not do this
    "#{form}#{script ? script.sub('"validator_hash"', @validators.to_json) : nil}#{script_bind(dom_id(object))}".html_safe
end