Module: Lipstick::Helpers::FormHelper

Includes:
ActionView::Helpers::FormTagHelper
Defined in:
lib/lipstick/helpers/form_helper.rb

Instance Method Summary collapse

Instance Method Details

#button_tag(content_or_options = nil, options = nil, &block) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/lipstick/helpers/form_helper.rb', line 102

def button_tag(content_or_options = nil, options = nil, &block)
  if content_or_options.is_a?(Hash)
    content_or_options[:class] ||= 'btn-default'
    add_css_class(content_or_options, 'btn')
    super
  else
    options ||= {}
    options[:class] ||= 'btn-default'
    add_css_class(options, 'btn')
    super(content_or_options, options, &block)
  end
end

#check_box_tag(&block) ⇒ Object

rubocop:enable Style/OptionalBooleanParameter



24
25
26
27
28
29
30
31
# File 'lib/lipstick/helpers/form_helper.rb', line 24

def check_box_tag(*, &block)
  ('div', class: 'checkbox') do
    ('label') do
      concat(super)
      concat(capture(&block))
    end
  end
end

#date_field_tag(name, value = nil, **opts) ⇒ Object



92
93
94
95
# File 'lib/lipstick/helpers/form_helper.rb', line 92

def date_field_tag(name, value = nil, **opts)
  opts[:class] = "#{opts[:class]} date-picker".strip
  text_field_tag(name, value, opts)
end

#delete_button_tag(url, text: true, **opts) ⇒ Object



115
116
117
118
119
120
121
122
# File 'lib/lipstick/helpers/form_helper.rb', line 115

def delete_button_tag(url, text: true, **opts)
  action = text.is_a?(String) ? text : 'Delete'

  ('div', class: 'btn-group') do
    concat(delete_dropdown_opener(text && action, **opts))
    concat(confirm_delete_dropdown(url, action))
  end
end

#field_block(html_opts = {}, &block) ⇒ Object



8
9
10
11
# File 'lib/lipstick/helpers/form_helper.rb', line 8

def field_block(html_opts = {}, &block)
  add_css_class(html_opts, 'form-group')
  ('div', html_opts, &block)
end

#form_for(obj, opts = {}, &block) ⇒ Object



124
125
126
127
# File 'lib/lipstick/helpers/form_helper.rb', line 124

def form_for(obj, opts = {}, &block)
  opts[:builder] = BootstrapFormBuilder
  super(obj, opts, &block)
end

#grouped_search_field(filter, placeholder) ⇒ Object



69
70
71
72
73
74
# File 'lib/lipstick/helpers/form_helper.rb', line 69

def grouped_search_field(filter, placeholder)
  ('div', class: 'input-group') do
    concat(search_filter_text_field(filter, placeholder))
    concat(('span', search_button, class: 'input-group-btn'))
  end
end

#hidden_fields(&block) ⇒ Object



76
77
78
# File 'lib/lipstick/helpers/form_helper.rb', line 76

def hidden_fields(&block)
  ('div', style: 'display: none;', &block)
end

#inline_form_tag(url_for_options = {}, options = {}, &block) ⇒ Object



35
36
37
38
# File 'lib/lipstick/helpers/form_helper.rb', line 35

def inline_form_tag(url_for_options = {}, options = {}, &block)
  add_css_class(options, 'form-inline')
  form_tag(url_for_options, options, &block)
end

#orig_text_field_tagObject



80
# File 'lib/lipstick/helpers/form_helper.rb', line 80

alias orig_text_field_tag text_field_tag

#radio_button_tag(name, value, checked = false, options = {}, &block) ⇒ Object

rubocop:disable Style/OptionalBooleanParameter



14
15
16
17
18
19
20
21
# File 'lib/lipstick/helpers/form_helper.rb', line 14

def radio_button_tag(name, value, checked = false, options = {}, &block)
  ('div', class: 'radio') do
    ('label') do
      concat(super)
      concat(capture(&block))
    end
  end
end

#search_buttonObject



62
63
64
65
66
67
# File 'lib/lipstick/helpers/form_helper.rb', line 62

def search_button
  button_tag(type: 'submit') do
    concat(icon_tag('search'))
    concat(' Search')
  end
end

#search_filter_text_field(filter, placeholder) ⇒ Object



55
56
57
58
59
60
# File 'lib/lipstick/helpers/form_helper.rb', line 55

def search_filter_text_field(filter, placeholder)
  orig_text_field_tag(:filter, filter,
                      placeholder: placeholder,
                      autocomplete: 'off',
                      class: 'form-control')
end

#search_form_input_tag(filter, placeholder) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/lipstick/helpers/form_helper.rb', line 47

def search_form_input_tag(filter, placeholder)
  ('div', class: 'row') do
    ('div', grouped_search_field(
                         filter, placeholder
                       ), class: 'col-lg-12')
  end
end

#search_form_tag(filter, url: nil, placeholder: 'Search within these entries') ⇒ Object



40
41
42
43
44
45
# File 'lib/lipstick/helpers/form_helper.rb', line 40

def search_form_tag(filter, url: nil,
                    placeholder: 'Search within these entries')
  form_tag(url, method: :get) do
    field_block { search_form_input_tag(filter, placeholder) }
  end
end

#select_tag(name, option_tags = nil, **opts) ⇒ Object



97
98
99
100
# File 'lib/lipstick/helpers/form_helper.rb', line 97

def select_tag(name, option_tags = nil, **opts)
  add_css_class(opts, 'form-control')
  super
end

#text_area_tag(name, content = nil, opts = {}) ⇒ Object



87
88
89
90
# File 'lib/lipstick/helpers/form_helper.rb', line 87

def text_area_tag(name, content = nil, opts = {})
  add_css_class(opts, 'form-control')
  super
end

#text_field_tag(name, value = nil, opts = {}) ⇒ Object



82
83
84
85
# File 'lib/lipstick/helpers/form_helper.rb', line 82

def text_field_tag(name, value = nil, opts = {})
  add_css_class(opts, 'form-control')
  super
end

#validate_form(selector, sym = nil, &block) ⇒ Object

Generates the wrapping code for validating a form. The selector is passed to jQuery, and must uniquely select the form being validated. `sym` is the object name when using a `form_for` helper to generate the form.

e.g. <%=

validate_form('#new-test-object', :test_object) do |v|
  v.validate_field(:name, ...) # Validate the test_object[name] field
end

%>



140
141
142
143
144
145
146
147
148
149
150
# File 'lib/lipstick/helpers/form_helper.rb', line 140

def validate_form(selector, sym = nil, &block)
  opts = {
    type: 'application/vnd.aaf.lipstick.validations+json',
    'data-target': selector,
    class: 'lipstick-validations'
  }

  ('script', opts) do
    validation_json(sym, &block).html_safe
  end
end