public
Last active

Formtastic 2.0 custom input upgrade examples

  • Download Gist
formtastic_extentions.rb
Ruby
1 2 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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
# This file contains custom input method from formtastic 1.2 and their
# replacement classes in 2.0
 
module FormtasticExtensions
class WysiwygInput < Formtastic::Inputs::TextInput
 
def input_html_options
wysiwyg_type = input_options.delete(:controls)
 
case wysiwyg_type
when :brief
super.merge(:class => "#{super[:class]} rnzckeditor-brief")
else
super.merge(:class => "#{super[:class]} rnzckeditor")
end
end
# workaround for CK Editor behaviour
# does not update text area, so required test fails.
# error are processed server-side
def required?
false
end
end
 
def wysiwyg_input(method, options = {})
wysiwyg_type = options.delete(:controls)
 
case wysiwyg_type
when :brief
text_input(method, options.merge(:input_html => {:class => "rnzckeditor-brief"}) )
else
text_input(method, options.merge(:input_html => {:class => "rnzckeditor"}) )
end
end
 
class DatepickerInput < Formtastic::Inputs::StringInput
def input_html_options
super.merge(:class => "#{super[:class]} datepicker")
end
end
 
def datepicker_input(method, options = {})
string_input(method, options.merge(:input_html => {:class => "datepicker"}) )
end
 
class TimepickerInput < Formtastic::Inputs::StringInput
def input_html_options
super.merge(:class => "#{super[:class]} timepicker")
end
end
 
def timepicker_input(method, options = {})
string_input(method, options.merge(:input_html => {:class => "timepicker"}) )
end
 
class ProgrammeSelectInput < Formtastic::Inputs::SelectInput
def collection
station = input_options.delete(:station)
programmes = station ? Programme.station(station) : Programme.all
programmes.map{|s| [s.name, s.id]}
end
end
 
def programme_select_input(method, options = {})
station = options.delete(:station)
programmes = station ? Programme.station(station) : Programme.all
select_input(method, options.merge(:collection => programmes.map{|s| [s.name, s.id]}))
end
class StatusInput < Formtastic::Inputs::RadioInput
def hint_text
if object.published_at_was
text = Formtastic::Util.html_safe("Published at #{object.published_at.to_s(:standard_with_time)}")
template.content_tag(:p, text, :class => builder.default_hint_class)
end
end
def collection
{'Under construction' => false, 'Live' => true }
end
end
 
def status_input(method, options = {})
if @object.published_at_was
text = Formtastic::Util.html_safe("Published at #{@object.published_at.to_s(:standard_with_time)}")
hint = template.content_tag(:p, text, :class => default_hint_class)
end
 
collection = {'Under construction' => false, 'Live' => true }
html_options = strip_formtastic_options(options).merge(options.delete(:input_html) || {})
 
input_name = generate_association_input_name(method)
value_as_class = options.delete(:value_as_class)
input_ids = []
 
list_item_content = collection.map do |c|
label = c.is_a?(Array) ? c.first : c
value = c.is_a?(Array) ? c.last : c
input_id = generate_html_id(input_name, value.to_s.gsub(/\s/, '_').gsub(/\W/, '').downcase)
input_ids << input_id
 
html_options[:id] = input_id
 
li_content = template.content_tag(:label,
Formtastic::Util.html_safe("#{radio_button(input_name, value, html_options)} #{escape_html_entities(label)}"),
:for => input_id
)
 
li_options = value_as_class ? { :class => [method.to_s.singularize, value.to_s.downcase].join('_') } : {}
template.content_tag(:li, Formtastic::Util.html_safe(li_content), li_options)
end
 
template.content_tag(:fieldset,
legend_tag(method, options) << template.content_tag(:ol, Formtastic::Util.html_safe(list_item_content.join))
) << hint
end
 
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.