Last active
March 29, 2016 02:12
-
-
Save synth/505ad1c7b4841bb59855 to your computer and use it in GitHub Desktop.
Ad hoc simple form calendar input
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DateMmddyyyyInput < DateBaseInput | |
private | |
def hint_text | |
'(MM/DD/YYYY)' | |
end | |
def date_picker_class | |
'datepicker' | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.