zpaulovics / datetimepicker-rails

A date and time picker for Twitter Bootstrap in Rails using Simple Form
MIT License
174 stars 80 forks source link

Passing options from SimpleForm? #48

Closed olegkio closed 9 years ago

olegkio commented 9 years ago

I was wondering if there is a way to pass options from SimpleForm?

I see that I can add default options in pickers.js (var default_picker_options=) but I think this will apply the same options every time I use :as => :date_picker

It would be handy if different options could be passed from SimpleForm depending on the field. For example "defaultDate" can be useful for "published_on" field but not for a "birthdate" field...

zpaulovics commented 9 years ago

Hi,

One way to implement your request would be as follows:

    <%= f.input :date_field, as: :date_picker, :label => 'Date field',
                input_html: {data: {date_options: {defaultDate: '2012/12/25'}}} %>

We should modify the input definition to handle the above SimpleForm attribute as follows:

class DatePickerInput < SimpleForm::Inputs::StringInput
  def input(wrapper_options)
    value = object.send(attribute_name) if object.respond_to? attribute_name
    display_pattern = I18n.t('datepicker.dformat', :default => '%d/%m/%Y')
    input_html_options[:value] ||= I18n.localize(value, :format => display_pattern) if value.present?

    input_html_options[:type] = 'text'
    picker_pattern = I18n.t('datepicker.pformat', :default => 'DD/MM/YYYY')
    dayViewHeaderFormat = I18n.t('dayViewHeaderFormat', :default => 'MMMM YYYY')

    input_html_options[:data] ||= {}
    date_options = input_html_options[:data][:date_options] || {}
    date_options.merge!({
        locale: I18n.locale.to_s,
        dayViewHeaderFormat: dayViewHeaderFormat,
        format: picker_pattern
    })
    input_html_options[:data].merge!({date_options: date_options })

    template.content_tag :div, class: 'input-group date datepicker' do
      input = super(wrapper_options) # leave StringInput do the real rendering
      input += template.content_tag :span, class: 'input-group-btn' do
        template.content_tag :button, class: 'btn btn-default', type: 'button' do
          template.content_tag :span, '', class: 'glyphicon glyphicon-calendar'
        end
      end
      input
    end
  end

  def input_html_classes
    super.push ''   # 'form-control'
  end
end

You can test this how it works in your local project if you replace the datepicker input definition.

Zoltan

zpaulovics commented 9 years ago

Implemented it in the version 4.6.10 (currently available in branch eonasdan)

olegkio commented 9 years ago

Zoltan,

I know it's been a long time... I meant to say "thank you" for taking care of it!

Oleg Kio IT Manager / Linux Engineer / Web Developer

On Mon, Feb 23, 2015 at 3:43 PM, Zoltan Paulovics notifications@github.com wrote:

Implemented it in the version 4.6.10 (currently available in branch eonasdan)

Reply to this email directly or view it on GitHub https://github.com/zpaulovics/datetimepicker-rails/issues/48#issuecomment-75627623 .

zpaulovics commented 9 years ago

:-)