Skip to content

Instantly share code, notes, and snippets.

@synth
Last active March 29, 2016 02:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save synth/505ad1c7b4841bb59855 to your computer and use it in GitHub Desktop.
Save synth/505ad1c7b4841bb59855 to your computer and use it in GitHub Desktop.
Ad hoc simple form calendar input
module ApplicationHelper
def mmddyyyy_date_input_tag(scope, attribute, value)
object = Struct.new(attribute, :class).new(value, Hashie::Mash.new(model_name: "Date"))
builder = SimpleForm::FormBuilder.new(scope, object, self, {}, nil)
input = DateMmddyyyyInput.new(builder, attribute, nil, nil)
input.input
end
end
class DateBaseInput < SimpleForm::Inputs::Base
def input
options[:hint] = hint_text unless options.has_key?(:hint)
input_html_options[:class] << date_picker_class
input_html_options[:class] << options[:class]
template.content_tag(:div, class: 'input-append') do
template.safe_join([
@builder.text_field(attribute_name, input_html_options),
wrapped_calendar_icon
], '')
end
end
private
# Wrap the icon in a <label> so clicking on it activates the datepicker
def wrapped_calendar_icon
date_input_id = "#{@builder.object.class.model_name.underscore}_#{attribute_name}"
template.content_tag(:span, class: 'add-on') do
template.content_tag(:label, template.icon_datepicker, for: date_input_id)
end
end
end
class DateMmddyyyyInput < DateBaseInput
private
def hint_text
'(MM/DD/YYYY)'
end
def date_picker_class
'datepicker'
end
end
@synth
Copy link
Author

synth commented Mar 29, 2016

The idea here is sometimes you don't necessarily need a "simple_form" builder object, such as perhaps having a search form. Sometimes you have a simple action that searches via some parameters such as "to" and "from" date parameters and dont need to wrap everything in a form or search object.

But you still want to leverage SimpleForms inputs. Et Voila. Above is one approach to hacking together the input generation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment