From 5acae26097a5c3183a86197727cc0ec003088ca5 Mon Sep 17 00:00:00 2001 From: Ngan Pham Date: Fri, 22 Nov 2013 22:32:24 -0800 Subject: [PATCH] Make deleted_at just return the raw value --- lib/rails_soft_deletable.rb | 11 +++++---- lib/rails_soft_deletable/version.rb | 2 +- spec/rails_soft_deletable_spec.rb | 36 +++++++++++++++++------------ 3 files changed, 28 insertions(+), 21 deletions(-) diff --git a/lib/rails_soft_deletable.rb b/lib/rails_soft_deletable.rb index d42a473..107ec73 100644 --- a/lib/rails_soft_deletable.rb +++ b/lib/rails_soft_deletable.rb @@ -48,12 +48,12 @@ def self.extended(base) end end - def deleted_at - val = super - if val.zero? || val.nil? + def soft_delete_time + value = send(soft_deletable_column) + if value.zero? || value.nil? nil else - Time.at(val).in_time_zone + Time.at(value).in_time_zone end end @@ -95,7 +95,8 @@ def restore! alias :restore :restore! def destroyed? - !!send(soft_deletable_column) + value = send(soft_deletable_column) + !value || value != 0 end def persisted? diff --git a/lib/rails_soft_deletable/version.rb b/lib/rails_soft_deletable/version.rb index 3def22f..6504bac 100644 --- a/lib/rails_soft_deletable/version.rb +++ b/lib/rails_soft_deletable/version.rb @@ -1,3 +1,3 @@ module RailsSoftDeletable - VERSION = "0.0.4" + VERSION = "0.0.5" end diff --git a/spec/rails_soft_deletable_spec.rb b/spec/rails_soft_deletable_spec.rb index a6075d2..e8b936f 100644 --- a/spec/rails_soft_deletable_spec.rb +++ b/spec/rails_soft_deletable_spec.rb @@ -11,11 +11,11 @@ decimal_model.destroy integer_model.destroy - decimal_deleted_at = DecimalModel.connection.select_value("SELECT deleted_at FROM #{DecimalModel.quoted_table_name} WHERE #{DecimalModel.primary_key} = #{decimal_model.id}") - integer_deleted_at = IntegerModel.connection.select_value("SELECT deleted_at FROM #{IntegerModel.quoted_table_name} WHERE #{IntegerModel.primary_key} = #{integer_model.id}") + raw_decimal_deleted_at = DecimalModel.connection.select_value("SELECT deleted_at FROM #{DecimalModel.quoted_table_name} WHERE #{DecimalModel.primary_key} = #{decimal_model.id}") + raw_integer_deleted_at = IntegerModel.connection.select_value("SELECT deleted_at FROM #{IntegerModel.quoted_table_name} WHERE #{IntegerModel.primary_key} = #{integer_model.id}") - expect(decimal_deleted_at).to eq(("%0.6f" % Time.now.to_f).to_f) - expect(integer_deleted_at.to_i).to eq(Time.now.to_i) + expect(raw_decimal_deleted_at).to eq(("%0.6f" % Time.now.to_f).to_f) + expect(raw_integer_deleted_at.to_i).to eq(Time.now.to_i) end end @@ -24,8 +24,11 @@ decimal_model.destroy integer_model.destroy - expect(decimal_model.deleted_at).to eq(Time.now) - expect(integer_model.deleted_at.to_i).to eq(Time.now.to_i) + expect(decimal_model.deleted_at).to eq(("%0.6f" % Time.now.to_f).to_f) + expect(integer_model.deleted_at).to eq(Time.now.to_i) + + expect(decimal_model.soft_delete_time).to eq(Time.now) + expect(integer_model.soft_delete_time.to_i).to eq(Time.now.to_i) end end @@ -107,11 +110,11 @@ decimal_model.delete integer_model.delete - decimal_deleted_at = DecimalModel.connection.select_value("SELECT deleted_at FROM #{DecimalModel.quoted_table_name} WHERE #{DecimalModel.primary_key} = #{decimal_model.id}") - integer_deleted_at = IntegerModel.connection.select_value("SELECT deleted_at FROM #{IntegerModel.quoted_table_name} WHERE #{IntegerModel.primary_key} = #{integer_model.id}") + raw_decimal_deleted_at = DecimalModel.connection.select_value("SELECT deleted_at FROM #{DecimalModel.quoted_table_name} WHERE #{DecimalModel.primary_key} = #{decimal_model.id}") + raw_integer_deleted_at = IntegerModel.connection.select_value("SELECT deleted_at FROM #{IntegerModel.quoted_table_name} WHERE #{IntegerModel.primary_key} = #{integer_model.id}") - expect(decimal_deleted_at).to eq(("%0.6f" % Time.now.to_f).to_f) - expect(integer_deleted_at.to_i).to eq(Time.now.to_i) + expect(raw_decimal_deleted_at).to eq(("%0.6f" % Time.now.to_f).to_f) + expect(raw_integer_deleted_at.to_i).to eq(Time.now.to_i) end end @@ -120,8 +123,11 @@ decimal_model.delete integer_model.delete - expect(decimal_model.deleted_at).to eq(Time.now) + expect(decimal_model.deleted_at).to eq(("%0.6f" % Time.now.to_f).to_f) expect(integer_model.deleted_at.to_i).to eq(Time.now.to_i) + + expect(decimal_model.soft_delete_time).to eq(Time.now) + expect(integer_model.soft_delete_time.to_i).to eq(Time.now.to_i) end end @@ -206,7 +212,7 @@ model.restore! expect(model).to be_persisted - expect(model.deleted_at).to be_nil + expect(model.soft_delete_time).to be_nil expect(model).to_not be_deleted_at_changed expect(model).to_not be_destroyed expect(model).to_not be_new_record @@ -233,10 +239,10 @@ end end - context "#deleted_at" do + context "#soft_delete_time" do context "when record has not been soft deleted" do it "returns nil" do - expect(model.deleted_at).to be_nil + expect(model.soft_delete_time).to be_nil end end @@ -246,7 +252,7 @@ end it "returns a Time object" do - expect(model.deleted_at).to be_kind_of(Time) + expect(model.soft_delete_time).to be_kind_of(Time) end end end