From 6eb09aa0adefbedbd71660bc86749f0179133db8 Mon Sep 17 00:00:00 2001 From: cignoir Date: Wed, 16 Mar 2016 11:01:47 +0900 Subject: [PATCH] Enable to save without updating timestamps --- lib/dynamoid/config.rb | 1 + lib/dynamoid/fields.rb | 4 ++-- spec/dynamoid/persistence_spec.rb | 29 +++++++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/lib/dynamoid/config.rb b/lib/dynamoid/config.rb index c4d14ebd..f3d3e732 100644 --- a/lib/dynamoid/config.rb +++ b/lib/dynamoid/config.rb @@ -23,6 +23,7 @@ module Config option :use_ssl, :default => true option :port, :default => '443' option :identity_map, :default => false + option :timestamps, :default => true # The default logger for Dynamoid: either the Rails logger or just stdout. # diff --git a/lib/dynamoid/fields.rb b/lib/dynamoid/fields.rb index 008e7d8a..642f24d3 100644 --- a/lib/dynamoid/fields.rb +++ b/lib/dynamoid/fields.rb @@ -137,14 +137,14 @@ def update_attribute(attribute, value) # # @since 0.2.0 def set_created_at - self.created_at = DateTime.now + self.created_at = DateTime.now if Dynamoid::Config.timestamps end # Automatically called during the save callback to set the updated_at time. # # @since 0.2.0 def set_updated_at - self.updated_at = DateTime.now + self.updated_at = DateTime.now if Dynamoid::Config.timestamps end def set_type diff --git a/spec/dynamoid/persistence_spec.rb b/spec/dynamoid/persistence_spec.rb index 6292a917..5a9a3fe2 100644 --- a/spec/dynamoid/persistence_spec.rb +++ b/spec/dynamoid/persistence_spec.rb @@ -81,6 +81,35 @@ def reload_address end end + context 'with timestamps set to false' do + def reload_address + Object.send(:remove_const, 'Address') + load 'app/models/address.rb' + end + + timestamps = Dynamoid::Config.timestamps + + before do + reload_address + Dynamoid.configure do |config| + config.timestamps = false + end + end + + after do + reload_address + Dynamoid.configure do |config| + config.timestamps = timestamps + end + end + + it 'sets nil to created_at and updated_at' do + address = Address.create + expect(address.created_at).to be_nil + expect(address.updated_at).to be_nil + end + end + it 'deletes an item completely' do @user = User.create(:name => 'Josh') @user.destroy