diff --git a/lib/flexible_date/base.rb b/lib/flexible_date/base.rb index 4b960d4..f683da2 100644 --- a/lib/flexible_date/base.rb +++ b/lib/flexible_date/base.rb @@ -23,7 +23,11 @@ def flexible_date(*params) attr_accessible field_with_suffix define_method "#{field_with_suffix}" do - format = I18n.t("flexible_date.configuration.format") + if self.class.columns_hash[field.to_s].type == :datetime + format = I18n.t("flexible_date.configuration.datetime_format") + else + format = I18n.t("flexible_date.configuration.format") + end date = self.send("#{field}") date.try(:strftime, format) end @@ -47,8 +51,14 @@ def flexible_date(*params) "flexible_date.messages.invalid") else begin - format = I18n.t("flexible_date.configuration.format") - self.send("#{field}=", value.blank? ? "" : Date.strptime(value, format)) + if self.class.columns_hash[field.to_s].type == :datetime + class_name = DateTime + format = I18n.t("flexible_date.configuration.datetime_format") + else + class_name = Date + format = I18n.t("flexible_date.configuration.format") + end + self.send("#{field}=", value.blank? ? "" : class_name.strptime(value, format)) rescue ArgumentError self.send("#{field}=", nil) @flexible_date_errors["#{field}".to_sym] = try_t.call( diff --git a/spec/dummy/app/models/event.rb b/spec/dummy/app/models/event.rb index 58596ab..df28589 100644 --- a/spec/dummy/app/models/event.rb +++ b/spec/dummy/app/models/event.rb @@ -1,7 +1,8 @@ class Event < ActiveRecord::Base - attr_accessible :end_date, :judgement_day, :payday, :start_date, :description + attr_accessible :end_date, :judgement_day, :payday, :start_date, :description, + :created_at - flexible_date :start_date, :end_date + flexible_date :start_date, :end_date, :created_at flexible_date :judgement_day, :suffix => 'yyz' flexible_date :payday, :if => Proc.new { |n| n.description.blank? } end diff --git a/spec/dummy/config/locales/br.yml b/spec/dummy/config/locales/br.yml index a6bc867..98c4a6b 100644 --- a/spec/dummy/config/locales/br.yml +++ b/spec/dummy/config/locales/br.yml @@ -2,6 +2,7 @@ br: flexible_date: configuration: format: "%d/%m/%Y" + datetime_format: "%d/%m/%Y %H:%M:%S" messages: with_suffix: invalid: "inválida." diff --git a/spec/dummy/config/locales/en.yml b/spec/dummy/config/locales/en.yml index 9f2e129..c7c80ed 100644 --- a/spec/dummy/config/locales/en.yml +++ b/spec/dummy/config/locales/en.yml @@ -2,5 +2,6 @@ en: flexible_date: configuration: format: "%d-%m-%Y" + datetime_format: "%d-%m-%Y %H:%M:%S" messages: invalid: "invalid." diff --git a/spec/flexible_date_spec.rb b/spec/flexible_date_spec.rb index b92eac0..b910444 100644 --- a/spec/flexible_date_spec.rb +++ b/spec/flexible_date_spec.rb @@ -9,7 +9,7 @@ it 'allows mass assignment of suffixed attributes' do expect { - Event.new(payday_flex: nil, judgement_day_yyz: nil, + Event.new(payday_flex: nil, judgement_day_yyz: nil, created_at_flex: nil, start_date_flex: nil, end_date_flex: nil) }.to_not raise_error ActiveModel::MassAssignmentSecurity::Error end @@ -24,8 +24,8 @@ it 'with empty date' do @event.payday_flex = "" @event.should_not be_valid - @event.errors[:payday_flex].should == ["inválida."] @event.errors[:payday].should == ["inválida."] + @event.errors[:payday_flex].should == ["inválida."] end it 'without empty date' do @@ -50,6 +50,7 @@ event.should respond_to(:start_date_flex=) event.should respond_to(:end_date_flex) event.should respond_to(:end_date_flex=) + event.should respond_to(:created_at_flex=) end it 'can be customized' do @@ -141,4 +142,22 @@ end end end + + context 'for datetime fields' do + before(:each) { I18n.locale = :en } + + it 'valid datetime' do + event = Event.new + event.created_at_flex = "30-04-2010 22:15:59" + event.should be_valid + end + + it 'invalid datetime' do + event = Event.new + event.created_at_flex = "30-04-2010" + event.should_not be_valid + event.errors[:created_at_flex].should == ["invalid."] + event.errors[:created_at].should == ["invalid."] + end + end end