date_time_attribute icon indicating copy to clipboard operation
date_time_attribute copied to clipboard

Using the Chronic parser results in mutating of attributes

Open donaldpiret opened this issue 10 years ago • 2 comments

Ran into this interesting issue today where if you're using the Chronic parser as described in the readme, your accessors will result in times changing each time they are called.

Example:

irb(main):001:0> e = Event.find(64)
  Event Load (6.5ms)  SELECT "events".* FROM "events" WHERE "events"."deleted_at" IS NULL AND "events"."id" = $1 LIMIT 1  [["id", 64]]
=> #<Event id: 64, title: "Saturday Bootcamp", description: "Come out and join me at Memorial Park every Saturda...", max_attendees: 50, user_id: 458, created_at: "2014-04-28 01:49:05", updated_at: "2014-05-20 06:58:11", state: "active", start: "2014-05-03 17:00:00", end: "2014-05-03 18:00:00", schedule_end: nil, time_zone: "America/Chicago">
irb(main):002:0> e.start_time
=> Sat, 03 May 2014 07:00:00 CDT -05:00
irb(main):003:0> e.start_time
=> Fri, 02 May 2014 21:00:00 CDT -05:00
irb(main):004:0> e.start_time
=> Fri, 02 May 2014 11:00:00 CDT -05:00
irb(main):005:0> e.start_time
=> Fri, 02 May 2014 01:00:00 CDT -05:00

Haven't figured out exactly what is causing this yet...

donaldpiret avatar May 20 '14 08:05 donaldpiret

@donaldpiret hrm, is there anything special in your model?

I am trying to reproduce in specs. Can't make them fail. Am I doing it in a correct way?

diff --git i/Gemfile w/Gemfile
index f2f1d44..f79710a 100644
--- i/Gemfile
+++ w/Gemfile
@@ -7,4 +7,5 @@ group :test do
   gem 'coveralls', require: false
   gem 'rake'
   gem 'sqlite3'
+  gem 'chronic'
 end
diff --git i/spec/active_record_spec.rb w/spec/active_record_spec.rb
index a75d7b6..bf5af43 100644
--- i/spec/active_record_spec.rb
+++ w/spec/active_record_spec.rb
@@ -1,4 +1,5 @@
 require 'spec_helper'
+require 'chronic'

 describe DateTimeAttribute, ActiveRecord::Base, use_active_record: true do
   before do
@@ -104,5 +105,17 @@ describe DateTimeAttribute, ActiveRecord::Base, use_active_record: true do
   context "loaded from db" do
     subject { Model.find(Model.create!(created_at: '2014-01-01 12:00:00').id) }
     its(:created_at) { should == DateTime.new(2014, 01, 01, 12, 0, 0) }
+
+    before do
+      DateTimeAttribute.parser = Chronic
+      subject.reload
+    end
+
+    it 'works with chronic' do
+      time = Chronic.parse('2014-01-01 12:00:00')
+      subject.created_at_time.should eq(time)
+      subject.created_at_time.should eq(time)
+      subject.created_at_time.should eq(time)
+    end
   end
 end

einzige avatar May 22 '14 10:05 einzige

Sorry haven't had time to really dive into this much deeper yet. Will have some more time next week and try to reproduce the behaviour I'm getting in a small example.

donaldpiret avatar May 30 '14 09:05 donaldpiret