Module: Tungsten::FormHelper

Includes:
ButtonHelper
Defined in:
app/helpers/tungsten/form_helper.rb

Constant Summary collapse

INPUT_OPTIONS =
{
  email: {
    type: "email",
    placeholder: "Email address",
    pattern: "[^@]+@[^@]+\\.[a-zA-Z]{2,}",
    autocorrect: "off",
    autocapitalize: "off",
    spellcheck: "false",
    data: { message: "Please enter a valid email address." }
  },

  password: {
    type: "password",
    placeholder: "Password"
  },

  text: {
    type: "text"
  },

  number: {
    type: "number",
    step: "any"
  },

  tel: {
    type: "tel",
    placeholder: "Phone number"
  },

  url: {
    type: "text",
    placeholder: "Web address",
    autocorrect: "off",
    autocapitalize: "off",
    spellcheck: "false",
    pattern: ".+\\.[a-zA-Z]{2,}"
  },

  card_number: {
    type: "text",
    required: true,
    pattern: "[0-9 -]{13,20}",
    placeholder: "Credit Card Number",
    data: {
      stripe: "number",
      message: "Please enter a valid credit card number."
    }
  },

  card_month: {
    type: "text",
    required: true,
    pattern: "0[1-9]|1[012]",
    placeholder: "MM",
    data: {
      stripe: "exp_month",
      message: "Please enter a valid expiration month."
    }
  },

  card_year: {
    type: "text",
    required: true,
    pattern: "[0-9]{4}",
    placeholder: "YYYY",
    data: {
      stripe: "exp_year",
      message: "Please enter a valid expiration year."
    }
  },

  card_cvc: {
    type: "text",
    required: true,
    pattern: "[0-9]{3,4}",
    placeholder: "CVC",
    data: {
      stripe: "cvc",
      message: "Please enter a valid security code."
    }
  },

  select_country: {
    type: "select",
    country_options: {
      include_blank: "Select a country",
      priority_countries: ["US", "GB", "CA"],
    },

    html_options: {}
  }
}

Instance Method Summary collapse

Methods included from ButtonHelper

#button, #button_classes, #copy_button, #destructive_button, #destructive_icon_button, #destructive_primary_button, #destructive_primary_icon_button, #icon_button, #primary_button, #primary_icon_button

Instance Method Details

#card_cvc_tag(name, value = nil, options = {}) ⇒ Object



184
185
186
# File 'app/helpers/tungsten/form_helper.rb', line 184

def card_cvc_tag(name, value=nil, options={})
  input_tag(:card_cvc, name, value, options)
end

#card_month_tag(name, value = nil, options = {}) ⇒ Object



176
177
178
# File 'app/helpers/tungsten/form_helper.rb', line 176

def card_month_tag(name, value=nil, options={})
  input_tag(:card_month, name, value, options)
end

#card_number_tag(name, value = nil, options = {}) ⇒ Object



172
173
174
# File 'app/helpers/tungsten/form_helper.rb', line 172

def card_number_tag(name, value=nil, options={})
  input_tag(:card_number, name, value, options)
end

#card_year_tag(name, value = nil, options = {}) ⇒ Object



180
181
182
# File 'app/helpers/tungsten/form_helper.rb', line 180

def card_year_tag(name, value=nil, options={})
  input_tag(:card_year, name, value, options)
end

#checkbox_input_tag(name, checked = false, options = {}) ⇒ Object



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'app/helpers/tungsten/form_helper.rb', line 209

def checkbox_input_tag(name, checked = false, options = {})
  value = true

  if checked.is_a? Hash
    options = checked
    checked = false
  end

  options[:type] = :checkbox

  tick_wrapper( name, options ) do
    options[:class] = 'input'
    concat tag :input, name: name, type: :hidden, value: false
    concat check_box_tag(name, value, checked, options)
  end
end

#email_input_tag(name, value = nil, options = {}) ⇒ Object

Email inputs



139
140
141
# File 'app/helpers/tungsten/form_helper.rb', line 139

def email_input_tag(name, value = nil, options = {})
  input_tag(:email, name, value, options)
end

#number_input_tag(name, value = nil, options = {}) ⇒ Object



164
165
166
# File 'app/helpers/tungsten/form_helper.rb', line 164

def number_input_tag(name, value = nil, options = {})
  input_tag(:number, name, value, options)
end

#password_input_tag(name, value = nil, options = {}) ⇒ Object

Passowrd inputs



144
145
146
# File 'app/helpers/tungsten/form_helper.rb', line 144

def password_input_tag(name, value = nil, options = {})
  input_tag(:password, name, value, options)
end

#radio_button_input_tag(name, value, checked = false, options = {}) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'app/helpers/tungsten/form_helper.rb', line 188

def radio_button_input_tag(name, value, checked = false, options = {})

  if checked.is_a? Hash
    options = checked
    checked = false
  end

  options[:type] = :radio
  (options[:class] ||= '') << ' input'
  options[:data] ||= {}
  options[:data][:show] = options.delete(:show)

  tick_wrapper( name, options ) do
    radio_button_tag(name, value, checked, options)
  end
end

#search_input_tag(name, value = nil, options = {}) ⇒ Object



168
169
170
# File 'app/helpers/tungsten/form_helper.rb', line 168

def search_input_tag(name, value = nil, options = {})
  input_tag(:search, name, value, options)
end

#select_country_tag(name, options = {}, country_options = {}) ⇒ Object

Country select



126
127
128
129
130
131
132
133
134
135
136
# File 'app/helpers/tungsten/form_helper.rb', line 126

def select_country_tag(name, options = {}, country_options = {})
  country_options.reverse_merge! INPUT_OPTIONS[:select_country][:country_options]

  options = INPUT_OPTIONS[:select_country][:html_options].deep_merge options
  options[:class] ||= ' '
  options[:class] += " #{label_class(:select)}"

  (:label, class: options.delete(:class) ) do
    country_select :user, :country, country_options, options
  end
end

#select_input_tag(name, option_tags = nil, options = {}, &block) ⇒ Object



226
227
228
229
230
231
232
233
234
235
236
237
# File 'app/helpers/tungsten/form_helper.rb', line 226

def select_input_tag(name, option_tags=nil, options={}, &block)
  if option_tags.is_a? Hash
    options = option_tags
    option_tags = nil
  end

  options[:label] ||= options.delete(:placeholder)

  option_tags ||= capture(&block).html_safe if block_given?
  
  input_tag(:select, name, option_tags.html_safe, options)
end

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



205
206
207
# File 'app/helpers/tungsten/form_helper.rb', line 205

def slider_input_tag(name, options={})
  input_tag(:range, name, options)
end

#stacked_form_for(record, options = {}, &block) ⇒ Object



112
113
114
115
116
# File 'app/helpers/tungsten/form_helper.rb', line 112

def stacked_form_for(record, options = {}, &block)
  form_for record, options do |f|
    stacked_form_tag f, &block
  end
end

#stacked_form_tag(form = nil) ⇒ Object



118
119
120
121
122
123
# File 'app/helpers/tungsten/form_helper.rb', line 118

def stacked_form_tag(form = nil)
  form.style = 'stacked' if form
   :div, class: 'stacked-form' do
    yield form if block_given?
  end
end

#switch_input_tag(name, checked = false, options = {}) ⇒ Object



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'app/helpers/tungsten/form_helper.rb', line 239

def switch_input_tag(name, checked = false, options = {})

  if checked.is_a? Hash
    options = checked
    checked = false
  end

  if label_text = options.delete(:label)
    label_text = (:span, class: 'label-text') { label_text }
  end
  
  if label_description = options.delete(:description)
    label_description = (:span, class: 'label-description') { label_description }
  end


  (:label, class: "check-switch switch-label #{ 'check-switch-full-width' if options[:full_width] }", data: { input: 'checkbox' }) do
    concat tag :input, name: name, type: :hidden, value: false
    concat label_text
    concat check_box_tag(name, true, checked, options)

    concat (:span, class: 'check-switch-panel') {
      concat (:span, class: 'check-switch-tick') { '' }
    }

    concat (:span, class: 'check-switch-label') { 'Enable' }
    concat label_description

  end

end

#table_form_for(record, options = {}, &block) ⇒ Object



99
100
101
102
103
# File 'app/helpers/tungsten/form_helper.rb', line 99

def table_form_for(record, options = {}, &block)
  form_for record, options do |f|
    table_form_tag f, &block
  end
end

#table_form_tag(form = nil) ⇒ Object



105
106
107
108
109
110
# File 'app/helpers/tungsten/form_helper.rb', line 105

def table_form_tag(form = nil)
  form.style = 'table' if form
   :div, class: ['table', 'table-form'] do
    yield form if block_given?
  end
end

#tel_input_tag(name, value = nil, options = {}) ⇒ Object



156
157
158
# File 'app/helpers/tungsten/form_helper.rb', line 156

def tel_input_tag(name, value = nil, options = {})
  input_tag(:tel, name, value, options)
end

#text_input_tag(name, value = nil, options = {}) ⇒ Object



148
149
150
# File 'app/helpers/tungsten/form_helper.rb', line 148

def text_input_tag(name, value = nil, options = {})
  input_tag(:text, name, value, options)
end

#textarea_tag(name, value = nil, options = {}, &block) ⇒ Object



160
161
162
# File 'app/helpers/tungsten/form_helper.rb', line 160

def textarea_tag(name, value = nil, options = {}, &block)
  input_tag(:textarea, name, value, options, &block)
end

#url_input_tag(name, value = nil, options = {}) ⇒ Object



152
153
154
# File 'app/helpers/tungsten/form_helper.rb', line 152

def url_input_tag(name, value = nil, options = {})
  input_tag(:url, name, value, options)
end