Skip to content

Commit

Permalink
Make deleted_at just return the raw value
Browse files Browse the repository at this point in the history
  • Loading branch information
ngan committed Nov 23, 2013
1 parent 648f225 commit 5acae26
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 21 deletions.
11 changes: 6 additions & 5 deletions lib/rails_soft_deletable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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?
Expand Down
2 changes: 1 addition & 1 deletion lib/rails_soft_deletable/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module RailsSoftDeletable
VERSION = "0.0.4"
VERSION = "0.0.5"
end
36 changes: 21 additions & 15 deletions spec/rails_soft_deletable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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
Expand Down

0 comments on commit 5acae26

Please sign in to comment.