Moved form helpers from application_helper to a real FormHelper

This commit is contained in:
Godwin
2014-05-12 20:09:19 -06:00
parent 9ae7135c6a
commit ffa8239e42
30 changed files with 600 additions and 357 deletions
+22 -189
View File
@@ -197,192 +197,6 @@ module ApplicationHelper
end
end
def field(form, name, type = nil, param = nil, html: nil, help: false, attrs: [], classes: nil, label: nil, placeholder: nil, value: nil, checked: nil, required: false)
if form.is_a?(Symbol) || form.is_a?(String)
param = type
type = name
name = form
form = nil
end
if attrs && !attrs.is_a?(Array)
attrs = [attrs]
end
attrs_used = 0
root = 'div'
lang_key = "form.#{name.to_s}"
if form
lang_key = form.object.class.name.underscore.pluralize + '.' + lang_key
elsif params[:controller]
lang_key = params[:controller] + '.' + lang_key
end
select_prompt = nil
show_label = !(/^hidden_field/.match(type.to_s))
label_after = true
value_attribute = !form
if /select(_tag)?$/.match(type.to_s)
if !label
select_prompt = placeholder || (form ? 'Select a ' + (_ ('form.select_' + name.to_s)) : 'form.Select_one')
label_html = ''
show_label = false
end
placeholder = nil
label_after = false
if param
if param.is_a?(Array)
param = options_for_select(param, value)
elsif param.is_a?(Hash)
param = options_from_collection_for_select(param, :first, :last, value)
end
end
value_attribute = false
elsif type.to_s == 'image_field' || type.to_s == 'user_select_field' || type.to_s == 'organization_select_field'
placeholder = nil
label_html = ''
show_label = false
else
if /^password/.match(type.to_s)
placeholder = nil
elsif !placeholder
placeholder = (_ 'form.Enter_your_' + name.to_s)
end
end
if show_label
label_html = eval("(" + (form ? 'form.label' : 'label_tag') + " name, '<span>#{CGI.escapeHTML(_ (label || name.to_s))}</span>'.html_safe)")
end
if label === false || !show_label
label_html = ''
end
if /text_area(_tag)?$/.match(type.to_s)
root = nil
end
html_options = nil
if html
html_options = ''
html.each do |key, v|
html_options += ', ' + key.to_s + ": html[:" + key.to_s + "]"
end
end
if classes
if classes.is_a?(String)
classes = [classes]
end
else
classes = []
end
if type == :image_field
form_html = form.label name do
('<div>' + image_tag(param || 'http://placehold.it/300x300&text=Click%20to%20upload%20an%20Image') + (form.file_field name) + (form.hidden_field (name.to_s + '_cache')) + '</div><span><span>' + name.to_s + '</span></span>').html_safe
end
elsif type == :user_select_field
form_html = form.hidden_field(:id, { class: 'id' }).html_safe
form_html += form.check_box(:_destroy).html_safe
form_html += form.label(:_destroy, '×').html_safe
form_html += form.hidden_field(:user_id, { class: 'user-id'} ).html_safe
if param && param.id
form_html += image_tag(param.avatar.url :thumb).html_safe + ('<div class="username">' + param.username + '</div>').html_safe
if attrs && attrs.length > 0 && attrs[0].is_a?(UserOrganizationRelationship)
form_html += form.select(:relationship, options_for_select(UserOrganizationRelationship::AllRelationships, attrs[0].relationship), {}, {class: 'small'}).html_safe
attrs_used += 1
end
else
classes << 'new'
if attrs && attrs.length > 0 && attrs[0].is_a?(UserOrganizationRelationship)
form_html += ('<a href="#" class="select-user" data-url="' + url_for(attrs[0].organization) + '">' + image_tag('http://placehold.it/120x120&text=%252B').html_safe + '</a><div class="username"></div>').html_safe
form_html += form.select(:relationship, options_for_select(UserOrganizationRelationship::AllRelationships, UserOrganizationRelationship::DefaultRelationship), {}, {class: 'small'}).html_safe
attrs_used += 1
end
end
elsif type == :organization_select_field
form_html = form.hidden_field(:id, { class: 'id' }).html_safe
form_html += form.check_box(:_destroy).html_safe
form_html += form.label(:_destroy, '×').html_safe
form_html += form.hidden_field(:organization_id, { class: 'organzation-id'} ).html_safe
if param && param.id
form_html += image_tag(param.avatar.url :thumb).html_safe + ('<div class="organizationname">' + param.name + '</div>').html_safe
else
classes << 'new'
form_html += ('<a href="#" class="select-organization" data-url="' + url_for(param) + '">' + image_tag('http://placehold.it/120x120&text=%252B').html_safe + '</a><div class="organizationname"></div>').html_safe
end
else
ph = ''
va = ''
if value_attribute
if /^(check_box|radio_button)/.match(type.to_s)
if checked === nil
checked = value == "on" || value.to_s == "1"
end
if /^(radio_button)/.match(type.to_s)
va = ', "' + value + '", checked'
else
va = ', "1", checked'
end
else
va = ', value'
end
end
if placeholder
if form
ph = ", :placeholder => '#{placeholder}'"
else
ph = ", placeholder: '#{placeholder}'"
end
end
form_html = (form ? "form.#{type} :#{name}" : "#{type} :#{name}") + va + ph + (param ? ', param' : '')
attrs.each_index { |i| form_html += (i >= attrs_used ? ', attrs[' + i.to_s + ']' : '') }
if select_prompt
if form
form_html += ', {prompt: select_prompt}'
else
form_html += ', prompt: select_prompt'
end
end
form_html += (html_options || '')
if required
form_html += ', :required => true'
end
form_html = eval(form_html)
if root
form_html = "<#{root}>" + form_html + "</#{root}>"
end
end
if help
form_html = ('<p>' + (_ (lang_key + '.help'), :w, 20) + '</p>').html_safe + form_html.html_safe
end
return ("<div class=\"field #{type.to_s.gsub('_', '-').gsub(/\-tag$/, '')} field-#{name.to_s.gsub('_', '-')}#{classes.length > 0 ? ' ' + classes.join(' ') : ''}\">" + (label_after ? '' : label_html) + form_html + (label_after ? label_html : '') + "</div>").html_safe
end
def actions(actions = [])
if !actions.is_a?(Array)
actions = [actions]
end
html = '<div class="actions">'
actions.each { |action|
if action == :facebook_sign_in
html += '<a href="' + url_for(auth_at_provider_path(:provider => :facebook)) + '" class="facebook-sign-in button">' + (_ action.to_s) + '</a>'
else
html += '<button id="' + action.to_s + '" name="' + action.to_s + '" type="submit">' + (_ action.to_s) + '</button>'
end
}
html += '</div>'
html.html_safe
end
def sortable(objects, id = 'id', url: nil, &block)
result = '<ul class="sortable sortable-' + objects[0].class.name.underscore.gsub('_', '-') + (url ? ('" data-url="' + url) : '') + '" data-id="' + id + '">'
objects.each_index do |i|
@@ -518,13 +332,24 @@ module ApplicationHelper
if f.repeats?
is_array = f.is_array?
opts.each do |key, value|
html += field((id + (is_array ? ('_' + key) : '')).to_sym, options['selection_type'] + '_tag', label: value, value: is_array ? (val ? val[key] : nil) : key, checked: is_array ? (val[key] == "1" || val[key] == "on") : val.to_s == key.to_s, required: f.required)
n = (id + (is_array ? ('_' + key) : '')).to_sym
v = is_array ? (val ? val[key] : nil) : key
o = {:label => value}
if f.required
options[:required] = true
end
html += _form_field(options['selection_type'], n, v, o)
end
else
html += field(id.to_sym, options['selection_type'] + '_tag', opts, value: val, required: f.required)
html += _form_field(options['selection_type'], id.to_sym, options_for_select(opts.invert, val), {})
end
else
html += field(id.to_sym, options['input_type'] + '_tag', label: false, placeholder: f.help, value: response ? ActiveSupport::JSON.decode(response.data) : nil, required: f.required)
#html += field(id.to_sym, options['input_type'] + '_tag', label: false, placeholder: f.help, value: response ? ActiveSupport::JSON.decode(response.data) : nil, required: f.required)
opts = {label: false, placeholder: f.help && f.help.length > 0 ? f.help : false}
if f.required
opts[:required] = true
end
html += _form_field(options['input_type'], id.to_sym, response ? ActiveSupport::JSON.decode(response.data) : nil, opts)
end
html.html_safe
@@ -542,4 +367,12 @@ module ApplicationHelper
end
end
private
def _form_field(type, name, value, options)
if type == 'check_box'
self.send(type + '_tag', name, "1", value, options)
else
self.send(type + '_tag', name, value, options)
end
end
end