Skip to content

Instantly share code, notes, and snippets.

@pwanderson
Forked from rhulse/formtastic_extentions.rb
Created January 5, 2012 22:34
Show Gist options
  • Save pwanderson/1567707 to your computer and use it in GitHub Desktop.
Save pwanderson/1567707 to your computer and use it in GitHub Desktop.
Formtastic 2.0 custom input upgrade examples
# 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment