Permalink
Browse files

Add a #destroyed? method for ActiveModel compliance.

There is currently no useful difference
between a new record and a destroyed record.

As such, #new_record? and #destroyed? are essentially synonymous.
  • Loading branch information...
1 parent eb30455 commit 09467d3179311406f5eeee197ca5a96daf6bb637 @elliotcm committed Mar 27, 2010
Showing with 35 additions and 1 deletion.
  1. +9 −1 lib/document/active_model_compliance.rb
  2. +26 −0 spec/document/active_model_compliance_spec.rb
@@ -6,10 +6,18 @@ module ActiveModelCompliance
def valid?
true
end
-
+
+ # There is currently no useful difference
+ # between a new record and a destroyed record.
+ #
+ # As such, these methods are essentially synonymous.
def new_record?
@_id.nil?
end
+
+ def destroyed?
+ @_id.nil?
+ end
end
end
@@ -46,4 +46,30 @@ class ActiveModelComplianceTest
end
end
end
+
+ describe "#destroyed?" do
+ it "responds to #destroyed?" do
+ @model.should respond_to(:destroyed?)
+ end
+
+ context "when the object has an id" do
+ before(:each) do
+ @model.instance_variable_set(:@_id, mock(:id))
+ end
+
+ it "is false" do
+ @model.destroyed?.should be_false
+ end
+ end
+
+ context "when the object has no id" do
+ before(:each) do
+ @model.instance_variable_set(:@_id, nil)
+ end
+
+ it "is true" do
+ @model.destroyed?.should be_true
+ end
+ end
+ end
end

0 comments on commit 09467d3

Please sign in to comment.