Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle fields of Datetime type #13

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions lib/flexible_date/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand Down
5 changes: 3 additions & 2 deletions spec/dummy/app/models/event.rb
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions spec/dummy/config/locales/br.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down
1 change: 1 addition & 0 deletions spec/dummy/config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ en:
flexible_date:
configuration:
format: "%d-%m-%Y"
datetime_format: "%d-%m-%Y %H:%M:%S"
messages:
invalid: "invalid."
23 changes: 21 additions & 2 deletions spec/flexible_date_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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