2017-07-16 13:11:16 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2009-08-29 02:49:18 -04:00
|
|
|
module ActiveModel
|
|
|
|
module Lint
|
2012-10-21 02:26:01 -04:00
|
|
|
# == Active \Model \Lint \Tests
|
2010-12-09 03:16:18 -05:00
|
|
|
#
|
2012-10-21 02:26:01 -04:00
|
|
|
# You can test whether an object is compliant with the Active \Model API by
|
2012-02-07 17:10:14 -05:00
|
|
|
# including <tt>ActiveModel::Lint::Tests</tt> in your TestCase. It will
|
|
|
|
# include tests that tell you whether your object is fully compliant,
|
|
|
|
# or if not, which aspects of the API are not implemented.
|
|
|
|
#
|
|
|
|
# Note an object is not required to implement all APIs in order to work
|
|
|
|
# with Action Pack. This module only intends to provide guidance in case
|
|
|
|
# you want all features out of the box.
|
2010-12-09 03:16:18 -05:00
|
|
|
#
|
|
|
|
# These tests do not attempt to determine the semantic correctness of the
|
2012-06-22 19:01:12 -04:00
|
|
|
# returned values. For instance, you could implement <tt>valid?</tt> to
|
2013-12-24 06:39:41 -05:00
|
|
|
# always return +true+, and the tests would pass. It is up to you to ensure
|
2012-06-22 19:01:12 -04:00
|
|
|
# that the values are semantically meaningful.
|
2010-12-09 03:16:18 -05:00
|
|
|
#
|
2012-06-22 19:01:12 -04:00
|
|
|
# Objects you pass in are expected to return a compliant object from a call
|
|
|
|
# to <tt>to_model</tt>. It is perfectly fine for <tt>to_model</tt> to return
|
|
|
|
# +self+.
|
2009-10-07 10:24:51 -04:00
|
|
|
module Tests
|
2015-01-07 12:05:23 -05:00
|
|
|
# Passes if the object's model responds to <tt>to_key</tt> and if calling
|
|
|
|
# this method returns +nil+ when the object is not persisted.
|
|
|
|
# Fails otherwise.
|
Adds #key and #to_param to the AMo interface
This commit introduces two new methods that every
AMo compliant object must implement. Below are the
default implementations along with the implied
interface contract.
# Returns an Enumerable of all (primary) key
# attributes or nil if new_record? is true
def key
new_record? ? nil : [1]
end
# Returns a string representing the object's key
# suitable for use in URLs, or nil if new_record?
# is true
def to_param
key ? key.first.to_s : nil
end
1) The #key method
Previously rails' record_identifier code, which is
used in the #dom_id helper, relied on calling #id
on the record to provide a reasonable DOM id. Now
with rails3 being all ORM agnostic, it's not safe
anymore to assume that every record ever will have
an #id as its primary key attribute.
Having a #key method available on every AMo object
means that #dom_id can be implemented using
record.to_model.key # instead of
record.id
Using this we're able to take composite primary
keys into account (e.g. available in datamapper)
by implementing #dom_id using a newly added
record_key_for_dom_id(record)
method. The user can overwrite this method to
provide customized versions of the object's key
used in #dom_id.
Also, dealing with more complex keys that can
contain arbitrary strings, means that we need to
make sure that we only provide DOM ids that are
valid according to the spec. For this reason, this
patch sends the key provided through a newly added
sanitize_dom_id(candidate_id)
method, that makes sure we only produce valid HTML
The reason to not just add #dom_id to the AMo
interface was that it feels like providing a DOM
id should not be a model concern. Adding #dom_id
to the AMo interface would force these concern on
the model, while it's better left to be implemented
in a helper.
Now one could say the same is true for #to_param,
and actually I think that it doesn't really fit
into the model either, but it's used in AR and it's
a main part of integrating into the rails router.
This is different from #dom_id which is only used
in view helpers and can be implemented on top of a
semantically more meaningful method like #key.
2) The #to_param method
Since the rails router relies on #to_param to be
present, AR::Base implements it and returns the
id by default, allowing the user to overwrite the
method if desired.
Now with different ORMs integrating into rails,
every ORM railtie needs to implement it's own
#to_param implementation while already providing
code to be AMo compliant. Since the whole point of
AMo compliance seems to be to integrate any ORM
seamlessly into rails, it seems fair that all we
really need to do as another ORM, is to be AMo
compliant. By including #to_param into the official
interface, we can make sure that this code can be
centralized in the various AMo compliance layers,
and not be added separately by every ORM railtie.
3) All specs pass
2010-02-20 02:24:10 -05:00
|
|
|
#
|
2015-01-07 12:05:23 -05:00
|
|
|
# <tt>to_key</tt> returns an Enumerable of all (primary) key attributes
|
|
|
|
# of the model, and is used to a generate unique DOM id for the object.
|
2010-02-20 21:05:28 -05:00
|
|
|
def test_to_key
|
|
|
|
assert model.respond_to?(:to_key), "The model should respond to to_key"
|
2010-02-21 05:09:21 -05:00
|
|
|
def model.persisted?() false end
|
2011-03-15 09:06:53 -04:00
|
|
|
assert model.to_key.nil?, "to_key should return nil when `persisted?` returns false"
|
Adds #key and #to_param to the AMo interface
This commit introduces two new methods that every
AMo compliant object must implement. Below are the
default implementations along with the implied
interface contract.
# Returns an Enumerable of all (primary) key
# attributes or nil if new_record? is true
def key
new_record? ? nil : [1]
end
# Returns a string representing the object's key
# suitable for use in URLs, or nil if new_record?
# is true
def to_param
key ? key.first.to_s : nil
end
1) The #key method
Previously rails' record_identifier code, which is
used in the #dom_id helper, relied on calling #id
on the record to provide a reasonable DOM id. Now
with rails3 being all ORM agnostic, it's not safe
anymore to assume that every record ever will have
an #id as its primary key attribute.
Having a #key method available on every AMo object
means that #dom_id can be implemented using
record.to_model.key # instead of
record.id
Using this we're able to take composite primary
keys into account (e.g. available in datamapper)
by implementing #dom_id using a newly added
record_key_for_dom_id(record)
method. The user can overwrite this method to
provide customized versions of the object's key
used in #dom_id.
Also, dealing with more complex keys that can
contain arbitrary strings, means that we need to
make sure that we only provide DOM ids that are
valid according to the spec. For this reason, this
patch sends the key provided through a newly added
sanitize_dom_id(candidate_id)
method, that makes sure we only produce valid HTML
The reason to not just add #dom_id to the AMo
interface was that it feels like providing a DOM
id should not be a model concern. Adding #dom_id
to the AMo interface would force these concern on
the model, while it's better left to be implemented
in a helper.
Now one could say the same is true for #to_param,
and actually I think that it doesn't really fit
into the model either, but it's used in AR and it's
a main part of integrating into the rails router.
This is different from #dom_id which is only used
in view helpers and can be implemented on top of a
semantically more meaningful method like #key.
2) The #to_param method
Since the rails router relies on #to_param to be
present, AR::Base implements it and returns the
id by default, allowing the user to overwrite the
method if desired.
Now with different ORMs integrating into rails,
every ORM railtie needs to implement it's own
#to_param implementation while already providing
code to be AMo compliant. Since the whole point of
AMo compliance seems to be to integrate any ORM
seamlessly into rails, it seems fair that all we
really need to do as another ORM, is to be AMo
compliant. By including #to_param into the official
interface, we can make sure that this code can be
centralized in the various AMo compliance layers,
and not be added separately by every ORM railtie.
3) All specs pass
2010-02-20 02:24:10 -05:00
|
|
|
end
|
|
|
|
|
2015-01-07 12:05:23 -05:00
|
|
|
# Passes if the object's model responds to <tt>to_param</tt> and if
|
|
|
|
# calling this method returns +nil+ when the object is not persisted.
|
|
|
|
# Fails otherwise.
|
2010-02-20 21:05:28 -05:00
|
|
|
#
|
2015-01-07 12:05:23 -05:00
|
|
|
# <tt>to_param</tt> is used to represent the object's key in URLs.
|
2012-06-22 19:01:12 -04:00
|
|
|
# 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.
|
Adds #key and #to_param to the AMo interface
This commit introduces two new methods that every
AMo compliant object must implement. Below are the
default implementations along with the implied
interface contract.
# Returns an Enumerable of all (primary) key
# attributes or nil if new_record? is true
def key
new_record? ? nil : [1]
end
# Returns a string representing the object's key
# suitable for use in URLs, or nil if new_record?
# is true
def to_param
key ? key.first.to_s : nil
end
1) The #key method
Previously rails' record_identifier code, which is
used in the #dom_id helper, relied on calling #id
on the record to provide a reasonable DOM id. Now
with rails3 being all ORM agnostic, it's not safe
anymore to assume that every record ever will have
an #id as its primary key attribute.
Having a #key method available on every AMo object
means that #dom_id can be implemented using
record.to_model.key # instead of
record.id
Using this we're able to take composite primary
keys into account (e.g. available in datamapper)
by implementing #dom_id using a newly added
record_key_for_dom_id(record)
method. The user can overwrite this method to
provide customized versions of the object's key
used in #dom_id.
Also, dealing with more complex keys that can
contain arbitrary strings, means that we need to
make sure that we only provide DOM ids that are
valid according to the spec. For this reason, this
patch sends the key provided through a newly added
sanitize_dom_id(candidate_id)
method, that makes sure we only produce valid HTML
The reason to not just add #dom_id to the AMo
interface was that it feels like providing a DOM
id should not be a model concern. Adding #dom_id
to the AMo interface would force these concern on
the model, while it's better left to be implemented
in a helper.
Now one could say the same is true for #to_param,
and actually I think that it doesn't really fit
into the model either, but it's used in AR and it's
a main part of integrating into the rails router.
This is different from #dom_id which is only used
in view helpers and can be implemented on top of a
semantically more meaningful method like #key.
2) The #to_param method
Since the rails router relies on #to_param to be
present, AR::Base implements it and returns the
id by default, allowing the user to overwrite the
method if desired.
Now with different ORMs integrating into rails,
every ORM railtie needs to implement it's own
#to_param implementation while already providing
code to be AMo compliant. Since the whole point of
AMo compliance seems to be to integrate any ORM
seamlessly into rails, it seems fair that all we
really need to do as another ORM, is to be AMo
compliant. By including #to_param into the official
interface, we can make sure that this code can be
centralized in the various AMo compliance layers,
and not be added separately by every ORM railtie.
3) All specs pass
2010-02-20 02:24:10 -05:00
|
|
|
def test_to_param
|
|
|
|
assert model.respond_to?(:to_param), "The model should respond to to_param"
|
2010-08-15 10:29:15 -04:00
|
|
|
def model.to_key() [1] end
|
2010-02-21 05:09:21 -05:00
|
|
|
def model.persisted?() false end
|
2011-03-16 06:28:40 -04:00
|
|
|
assert model.to_param.nil?, "to_param should return nil when `persisted?` returns false"
|
Adds #key and #to_param to the AMo interface
This commit introduces two new methods that every
AMo compliant object must implement. Below are the
default implementations along with the implied
interface contract.
# Returns an Enumerable of all (primary) key
# attributes or nil if new_record? is true
def key
new_record? ? nil : [1]
end
# Returns a string representing the object's key
# suitable for use in URLs, or nil if new_record?
# is true
def to_param
key ? key.first.to_s : nil
end
1) The #key method
Previously rails' record_identifier code, which is
used in the #dom_id helper, relied on calling #id
on the record to provide a reasonable DOM id. Now
with rails3 being all ORM agnostic, it's not safe
anymore to assume that every record ever will have
an #id as its primary key attribute.
Having a #key method available on every AMo object
means that #dom_id can be implemented using
record.to_model.key # instead of
record.id
Using this we're able to take composite primary
keys into account (e.g. available in datamapper)
by implementing #dom_id using a newly added
record_key_for_dom_id(record)
method. The user can overwrite this method to
provide customized versions of the object's key
used in #dom_id.
Also, dealing with more complex keys that can
contain arbitrary strings, means that we need to
make sure that we only provide DOM ids that are
valid according to the spec. For this reason, this
patch sends the key provided through a newly added
sanitize_dom_id(candidate_id)
method, that makes sure we only produce valid HTML
The reason to not just add #dom_id to the AMo
interface was that it feels like providing a DOM
id should not be a model concern. Adding #dom_id
to the AMo interface would force these concern on
the model, while it's better left to be implemented
in a helper.
Now one could say the same is true for #to_param,
and actually I think that it doesn't really fit
into the model either, but it's used in AR and it's
a main part of integrating into the rails router.
This is different from #dom_id which is only used
in view helpers and can be implemented on top of a
semantically more meaningful method like #key.
2) The #to_param method
Since the rails router relies on #to_param to be
present, AR::Base implements it and returns the
id by default, allowing the user to overwrite the
method if desired.
Now with different ORMs integrating into rails,
every ORM railtie needs to implement it's own
#to_param implementation while already providing
code to be AMo compliant. Since the whole point of
AMo compliance seems to be to integrate any ORM
seamlessly into rails, it seems fair that all we
really need to do as another ORM, is to be AMo
compliant. By including #to_param into the official
interface, we can make sure that this code can be
centralized in the various AMo compliance layers,
and not be added separately by every ORM railtie.
3) All specs pass
2010-02-20 02:24:10 -05:00
|
|
|
end
|
|
|
|
|
2015-01-07 12:05:23 -05:00
|
|
|
# Passes if the object's model responds to <tt>to_partial_path</tt> and if
|
|
|
|
# calling this method returns a string. Fails otherwise.
|
2011-07-08 17:54:15 -04:00
|
|
|
#
|
2015-01-07 12:05:23 -05:00
|
|
|
# <tt>to_partial_path</tt> is used for looking up partials. For example,
|
|
|
|
# a BlogPost model might return "blog_posts/blog_post".
|
2011-08-01 05:42:00 -04:00
|
|
|
def test_to_partial_path
|
|
|
|
assert model.respond_to?(:to_partial_path), "The model should respond to to_partial_path"
|
|
|
|
assert_kind_of String, model.to_partial_path
|
2011-07-08 17:54:15 -04:00
|
|
|
end
|
|
|
|
|
2015-01-07 12:05:23 -05:00
|
|
|
# Passes if the object's model responds to <tt>persisted?</tt> and if
|
|
|
|
# calling this method returns either +true+ or +false+. Fails otherwise.
|
2009-08-29 02:49:18 -04:00
|
|
|
#
|
2015-01-07 12:05:23 -05:00
|
|
|
# <tt>persisted?</tt> is used when calculating the URL for an object.
|
|
|
|
# If the object is not persisted, a form for that object, for instance,
|
|
|
|
# will route to the create action. If it is persisted, a form for the
|
|
|
|
# object will route to the update action.
|
2010-02-21 05:09:21 -05:00
|
|
|
def test_persisted?
|
|
|
|
assert model.respond_to?(:persisted?), "The model should respond to persisted?"
|
|
|
|
assert_boolean model.persisted?, "persisted?"
|
2009-08-29 02:49:18 -04:00
|
|
|
end
|
|
|
|
|
2015-01-07 12:05:23 -05:00
|
|
|
# Passes if the object's model responds to <tt>model_name</tt> both as
|
|
|
|
# an instance method and as a class method, and if calling this method
|
|
|
|
# returns a string with some convenience methods: <tt>:human</tt>,
|
|
|
|
# <tt>:singular</tt> and <tt>:plural</tt>.
|
2009-12-31 08:19:30 -05:00
|
|
|
#
|
2015-01-07 12:05:23 -05:00
|
|
|
# Check ActiveModel::Naming for more information.
|
2009-12-31 08:19:30 -05:00
|
|
|
def test_model_naming
|
2014-06-20 18:49:27 -04:00
|
|
|
assert model.class.respond_to?(:model_name), "The model class should respond to model_name"
|
2009-12-31 08:19:30 -05:00
|
|
|
model_name = model.class.model_name
|
2012-03-28 12:25:06 -04:00
|
|
|
assert model_name.respond_to?(:to_str)
|
|
|
|
assert model_name.human.respond_to?(:to_str)
|
|
|
|
assert model_name.singular.respond_to?(:to_str)
|
|
|
|
assert model_name.plural.respond_to?(:to_str)
|
2014-06-20 18:49:27 -04:00
|
|
|
|
|
|
|
assert model.respond_to?(:model_name), "The model instance should respond to model_name"
|
|
|
|
assert_equal model.model_name, model.class.model_name
|
2009-12-31 08:19:30 -05:00
|
|
|
end
|
|
|
|
|
2015-01-07 12:05:23 -05:00
|
|
|
# Passes if the object's model responds to <tt>errors</tt> and if calling
|
|
|
|
# <tt>[](attribute)</tt> on the result of this method returns an array.
|
|
|
|
# Fails otherwise.
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2015-01-07 12:05:23 -05:00
|
|
|
# <tt>errors[attribute]</tt> is used to retrieve the errors of a model
|
|
|
|
# for a given attribute. If errors are present, the method should return
|
|
|
|
# an array of strings that are the errors for the attribute in question.
|
|
|
|
# If localization is used, the strings should be localized for the current
|
|
|
|
# locale. If no error is present, the method should return an empty array.
|
2009-10-07 10:24:51 -04:00
|
|
|
def test_errors_aref
|
|
|
|
assert model.respond_to?(:errors), "The model should respond to errors"
|
|
|
|
assert model.errors[:hello].is_a?(Array), "errors#[] should return an Array"
|
2009-08-29 02:49:18 -04:00
|
|
|
end
|
|
|
|
|
2009-10-07 10:24:51 -04:00
|
|
|
private
|
|
|
|
def model
|
2013-05-12 07:59:51 -04:00
|
|
|
assert @model.respond_to?(:to_model), "The object should respond to to_model"
|
2009-10-07 10:24:51 -04:00
|
|
|
@model.to_model
|
2009-08-29 02:49:18 -04:00
|
|
|
end
|
|
|
|
|
2009-10-07 10:24:51 -04:00
|
|
|
def assert_boolean(result, name)
|
|
|
|
assert result == true || result == false, "#{name} should be a boolean"
|
|
|
|
end
|
2009-08-29 02:49:18 -04:00
|
|
|
end
|
|
|
|
end
|
2009-09-21 10:49:43 -04:00
|
|
|
end
|