Module: ClientSideValidations::ActionView::Helpers::FormBuilder

Defined in:
lib/client_side_validations/action_view/form_builder.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



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
# File 'lib/client_side_validations/action_view/form_builder.rb', line 5

def self.included(base)
  (base.field_helpers.map(&:to_s) - %w(apply_form_for_options! label check_box radio_button fields_for hidden_field)).each do |selector|
    base.class_eval <<-RUBY_EVAL
      def #{selector}_with_client_side_validations(method, options = {})
        build_validation_options(method, options)
        options.delete(:validate)
        #{selector}_without_client_side_validations(method, options)
      end
    RUBY_EVAL

    base.class_eval { alias_method_chain selector, :client_side_validations }
  end

  base.class_eval do
    alias_method_chain :initialize,                :client_side_validations
    alias_method_chain :fields_for,                :client_side_validations
    alias_method_chain :check_box,                 :client_side_validations
    alias_method_chain :radio_button,              :client_side_validations
    alias_method_chain :select,                    :client_side_validations
    alias_method_chain :collection_select,         :client_side_validations
    alias_method_chain :collection_check_boxes,    :client_side_validations
    alias_method_chain :collection_radio_buttons,  :client_side_validations
    alias_method_chain :grouped_collection_select, :client_side_validations
    alias_method_chain :time_zone_select,          :client_side_validations
  end
end

Instance Method Details

#check_box_with_client_side_validations(method, options = {}, checked_value = '1', unchecked_value = '0') ⇒ Object



58
59
60
61
62
# File 'lib/client_side_validations/action_view/form_builder.rb', line 58

def check_box_with_client_side_validations(method, options = {}, checked_value = '1', unchecked_value = '0')
  build_validation_options(method, options)
  options.delete(:validate)
  check_box_without_client_side_validations(method, options, checked_value, unchecked_value)
end

#client_side_form_settings(_options, form_helper) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/client_side_validations/action_view/form_builder.rb', line 32

def client_side_form_settings(_options, form_helper)
  {
    type: self.class.to_s,
    input_tag: form_helper.class.field_error_proc.call(%(<span id="input_tag" />),  Struct.new(:error_message, :tag_id).new([], '')),
    label_tag: form_helper.class.field_error_proc.call(%(<label id="label_tag" />), Struct.new(:error_message, :tag_id).new([], ''))
  }
end

#collection_check_boxes_with_client_side_validations(method, collection, value_method, text_method, options = {}, html_options = {}, &block) ⇒ Object



82
83
84
85
86
# File 'lib/client_side_validations/action_view/form_builder.rb', line 82

def collection_check_boxes_with_client_side_validations(method, collection, value_method, text_method, options = {}, html_options = {}, &block)
  build_validation_options(method, html_options.merge(name: options[:name]))
  html_options.delete(:validate)
  collection_check_boxes_without_client_side_validations(method, collection, value_method, text_method, options, html_options, &block)
end

#collection_radio_buttons_with_client_side_validations(method, collection, value_method, text_method, options = {}, html_options = {}, &block) ⇒ Object



88
89
90
91
92
# File 'lib/client_side_validations/action_view/form_builder.rb', line 88

def collection_radio_buttons_with_client_side_validations(method, collection, value_method, text_method, options = {}, html_options = {}, &block)
  build_validation_options(method, html_options.merge(name: options[:name]))
  html_options.delete(:validate)
  collection_radio_buttons_without_client_side_validations(method, collection, value_method, text_method, options, html_options, &block)
end

#collection_select_with_client_side_validations(method, collection, value_method, text_method, options = {}, html_options = {}) ⇒ Object



76
77
78
79
80
# File 'lib/client_side_validations/action_view/form_builder.rb', line 76

def collection_select_with_client_side_validations(method, collection, value_method, text_method, options = {}, html_options = {})
  build_validation_options(method, html_options.merge(name: options[:name]))
  html_options.delete(:validate)
  collection_select_without_client_side_validations(method, collection, value_method, text_method, options, html_options)
end

#fields_for_with_client_side_validations(record_name, record_object = nil, fields_options = {}, &block) ⇒ Object



53
54
55
56
# File 'lib/client_side_validations/action_view/form_builder.rb', line 53

def fields_for_with_client_side_validations(record_name, record_object = nil, fields_options = {}, &block)
  fields_options[:validate] ||= @options[:validate] if @options[:validate] && !fields_options.key?(:validate)
  fields_for_without_client_side_validations(record_name, record_object, fields_options, &block)
end

#grouped_collection_select_with_client_side_validations(method, collection, group_method, group_label_method, option_key_method, option_value_method, options = {}, html_options = {}) ⇒ Object



94
95
96
97
98
# File 'lib/client_side_validations/action_view/form_builder.rb', line 94

def grouped_collection_select_with_client_side_validations(method, collection, group_method, group_label_method, option_key_method, option_value_method, options = {}, html_options = {})
  build_validation_options(method, html_options.merge(name: options[:name]))
  html_options.delete(:validate)
  grouped_collection_select_without_client_side_validations(method, collection, group_method, group_label_method, option_key_method, option_value_method, options, html_options)
end

#initialize_with_client_side_validations(object_name, object, template, options) ⇒ Object



48
49
50
51
# File 'lib/client_side_validations/action_view/form_builder.rb', line 48

def initialize_with_client_side_validations(object_name, object, template, options)
  initialize_without_client_side_validations(object_name, object, template, options)
  @options[:validators] = { object => {} }
end

#radio_button_with_client_side_validations(method, tag_value, options = {}) ⇒ Object



64
65
66
67
68
# File 'lib/client_side_validations/action_view/form_builder.rb', line 64

def radio_button_with_client_side_validations(method, tag_value, options = {})
  build_validation_options(method, options)
  options.delete(:validate)
  radio_button_without_client_side_validations(method, tag_value, options)
end

#select_with_client_side_validations(method, choices = nil, options = {}, html_options = {}, &block) ⇒ Object



70
71
72
73
74
# File 'lib/client_side_validations/action_view/form_builder.rb', line 70

def select_with_client_side_validations(method, choices = nil, options = {}, html_options = {}, &block)
  build_validation_options(method, html_options.merge(name: options[:name]))
  html_options.delete(:validate)
  select_without_client_side_validations(method, choices, options, html_options, &block)
end

#time_zone_select_with_client_side_validations(method, priority_zones = nil, options = {}, html_options = {}) ⇒ Object



100
101
102
103
104
# File 'lib/client_side_validations/action_view/form_builder.rb', line 100

def time_zone_select_with_client_side_validations(method, priority_zones = nil, options = {}, html_options = {})
  build_validation_options(method, html_options.merge(name: options[:name]))
  html_options.delete(:validate)
  time_zone_select_without_client_side_validations(method, priority_zones, options, html_options)
end

#validate(*attrs) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/client_side_validations/action_view/form_builder.rb', line 40

def validate(*attrs)
  options = attrs.pop if attrs.last.is_a?(Hash)
  (attrs.present? ? attrs : @object._validators.keys).each do |attr|
    build_validation_options(attr, validate: options)
  end
  nil
end