Created
March 5, 2015 12:27
-
-
Save Szeliga/5386bbaf0b378e4910b0 to your computer and use it in GitHub Desktop.
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 DatePickerInput < SimpleForm::Inputs::StringInput | |
def display_pattern | |
I18n.t('datepicker.dformat', default: '%d/%m/%Y') | |
end | |
def picker_pattern | |
I18n.t('datepicker.pformat', default: 'dd/MM/yyyy') | |
end | |
def wrapper_classes | |
'input-append date datepicker' | |
end | |
def input_value | |
object.send(attribute_name) if object.respond_to? attribute_name | |
end | |
def set_display_pattern | |
value = input_value | |
input_html_options[:value] ||= I18n.localize(value, format: display_pattern) if value.present? | |
end | |
def set_picker_pattern | |
input_html_options[:type] = 'text' | |
input_html_options[:data] ||= {} | |
input_html_options[:data].merge!(format: picker_pattern, language: I18n.locale.to_s, | |
date_weekstart: I18n.t('datepicker.weekstart', default: 0)) | |
end | |
def input | |
set_display_pattern | |
set_picker_pattern | |
template.content_tag :div, class: wrapper_classes do | |
input = super | |
input += template.content_tag :span, class: 'add-on' do | |
template.content_tag :i, '', class: 'icon-calendar', | |
data: { 'time-icon' => 'icon-time', 'date-icon' => 'icon-calendar' } | |
end | |
input | |
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
require "spec_helper" | |
RSpec.describe DatePickerInput do | |
before { I18n.locale = I18n.default_locale } | |
# Prevent warnings about already initialized constant | |
after { Object.send(:remove_const, :Dummy) } | |
let!(:dummy) do | |
Dummy = Class.new do | |
include Mongoid::Document | |
field :date, type: Date | |
end | |
end | |
let(:foo) { dummy.new(date: Date.parse('05.03.2015')) } | |
let(:body) { Nokogiri::HTML(input_for(foo, :date, as: :date_picker)) } | |
subject { body.at_css('input#dummy_date') } | |
it 'sets data-format attribute to dd/MM/yyyy' do | |
expect(subject.attr('data-format')).to eq 'dd/MM/yyyy' | |
end | |
it 'formats the inputs value' do | |
expect(subject.attr('value')).to eq '05/03/2015' | |
end | |
it 'sets the data-language attribute to en' do | |
expect(subject.attr('data-language')).to eq 'en' | |
end | |
it 'sets the data-date-weekstart attribute to 0' do | |
expect(subject.attr('data-date-weekstart')).to eq '0' | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment