aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel
diff options
context:
space:
mode:
Diffstat (limited to 'activemodel')
-rw-r--r--activemodel/examples/validations.rb2
-rw-r--r--activemodel/lib/active_model/conversion.rb12
-rw-r--r--activemodel/lib/active_model/lint.rb25
-rw-r--r--activemodel/test/cases/conversion_test.rb4
-rw-r--r--activemodel/test/cases/lint_test.rb3
-rw-r--r--activemodel/test/models/contact.rb6
6 files changed, 23 insertions, 29 deletions
diff --git a/activemodel/examples/validations.rb b/activemodel/examples/validations.rb
index b039897ea5..0b2706076f 100644
--- a/activemodel/examples/validations.rb
+++ b/activemodel/examples/validations.rb
@@ -16,7 +16,7 @@ class Person
@persisted = true
end
- def new_record?
+ def persisted?
@persisted
end
end
diff --git a/activemodel/lib/active_model/conversion.rb b/activemodel/lib/active_model/conversion.rb
index 78dc34197b..585c20dcdf 100644
--- a/activemodel/lib/active_model/conversion.rb
+++ b/activemodel/lib/active_model/conversion.rb
@@ -8,9 +8,9 @@ module ActiveModel
# class ContactMessage
# include ActiveModel::Conversion
#
- # # Always a new record, since it's not persisted in the DB.
- # def new_record?
- # true
+ # # ContactMessage are never persisted in the DB
+ # def persisted?
+ # false
# end
# end
#
@@ -30,13 +30,13 @@ module ActiveModel
self
end
- # Returns an Enumerable of all (primary) key attributes or nil if new_record? is true
+ # Returns an Enumerable of all (primary) key attributes or nil if persisted? is fakse
def to_key
- new_record? ? nil : [id]
+ persisted? ? [id] : nil
end
# Returns a string representing the object's key suitable for use in URLs,
- # or nil if new_record? is true
+ # or nil if persisted? is false
def to_param
to_key ? to_key.join('-') : nil
end
diff --git a/activemodel/lib/active_model/lint.rb b/activemodel/lib/active_model/lint.rb
index 98413ac9fb..0e62e131a3 100644
--- a/activemodel/lib/active_model/lint.rb
+++ b/activemodel/lib/active_model/lint.rb
@@ -17,28 +17,28 @@ module ActiveModel
# == Responds to <tt>to_key</tt>
#
# Returns an Enumerable of all (primary) key attributes
- # or nil if model.new_record? is true
+ # or nil if model.persisted? is false
def test_to_key
assert model.respond_to?(:to_key), "The model should respond to to_key"
- def model.new_record?() true end
+ def model.persisted?() false end
assert model.to_key.nil?
- def model.new_record?() false end
+ def model.persisted?() true end
assert model.to_key.respond_to?(:each)
end
# == Responds to <tt>to_param</tt>
#
# Returns a string representing the object's key suitable for use in URLs
- # or nil if model.new_record? is true.
+ # or nil if model.persisted? is false.
#
# Implementers can decide to either raise an exception or provide a default
# in case the record uses a composite primary key. There are no tests for this
# behavior in lint because it doesn't make sense to force any of the possible
# implementation strategies on the implementer. However, if the resource is
- # a new_record?, then to_param should always return nil.
+ # not persisted?, then to_param should always return nil.
def test_to_param
assert model.respond_to?(:to_param), "The model should respond to to_param"
- def model.new_record?() true end
+ def model.persisted?() false end
assert model.to_param.nil?
end
@@ -51,21 +51,16 @@ module ActiveModel
assert_boolean model.valid?, "valid?"
end
- # == Responds to <tt>new_record?</tt>
+ # == Responds to <tt>persisted?</tt>
#
# Returns a boolean that specifies whether the object has been persisted yet.
# This is used when calculating the URL for an object. If the object is
# not persisted, a form for that object, for instance, will be POSTed to the
# collection. If it is persisted, a form for the object will put PUTed to the
# URL for the object.
- def test_new_record?
- assert model.respond_to?(:new_record?), "The model should respond to new_record?"
- assert_boolean model.new_record?, "new_record?"
- end
-
- def test_destroyed?
- assert model.respond_to?(:destroyed?), "The model should respond to destroyed?"
- assert_boolean model.destroyed?, "destroyed?"
+ def test_persisted?
+ assert model.respond_to?(:persisted?), "The model should respond to persisted?"
+ assert_boolean model.persisted?, "persisted?"
end
# == Naming
diff --git a/activemodel/test/cases/conversion_test.rb b/activemodel/test/cases/conversion_test.rb
index 373424df2f..7669bf5f65 100644
--- a/activemodel/test/cases/conversion_test.rb
+++ b/activemodel/test/cases/conversion_test.rb
@@ -12,7 +12,7 @@ class ConversionTest < ActiveModel::TestCase
end
test "to_key default implementation returns the id in an array for persisted records" do
- assert_equal [1], Contact.new(:new_record => false, :id => 1).to_key
+ assert_equal [1], Contact.new(:id => 1).to_key
end
test "to_param default implementation returns nil for new records" do
@@ -20,6 +20,6 @@ class ConversionTest < ActiveModel::TestCase
end
test "to_param default implementation returns a string of ids for persisted records" do
- assert_equal "1", Contact.new(:new_record => false, :id => 1).to_param
+ assert_equal "1", Contact.new(:id => 1).to_param
end
end \ No newline at end of file
diff --git a/activemodel/test/cases/lint_test.rb b/activemodel/test/cases/lint_test.rb
index e4c069e1ab..68372160cd 100644
--- a/activemodel/test/cases/lint_test.rb
+++ b/activemodel/test/cases/lint_test.rb
@@ -8,8 +8,7 @@ class LintTest < ActiveModel::TestCase
include ActiveModel::Conversion
def valid?() true end
- def new_record?() true end
- def destroyed?() true end
+ def persisted?() false end
def errors
obj = Object.new
diff --git a/activemodel/test/models/contact.rb b/activemodel/test/models/contact.rb
index dbdb8539b7..a583b89aa1 100644
--- a/activemodel/test/models/contact.rb
+++ b/activemodel/test/models/contact.rb
@@ -1,13 +1,13 @@
class Contact
include ActiveModel::Conversion
- attr_accessor :id, :name, :age, :created_at, :awesome, :preferences, :new_record
+ attr_accessor :id, :name, :age, :created_at, :awesome, :preferences
def initialize(options = {})
options.each { |name, value| send("#{name}=", value) }
end
- def new_record?
- defined?(@new_record) ? @new_record : true
+ def persisted?
+ id.present?
end
end