2009-08-29 02:49:18 -04:00
|
|
|
module ActiveModel
|
|
|
|
module Lint
|
2010-12-09 03:16:18 -05:00
|
|
|
# == Active Model Lint Tests
|
|
|
|
#
|
|
|
|
# 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
|
|
|
|
# returned values. For instance, you could implement valid? to always
|
|
|
|
# return true, and the tests would pass. It is up to you to ensure that
|
|
|
|
# the values are semantically meaningful.
|
|
|
|
#
|
|
|
|
# Objects you pass in are expected to return a compliant object from a
|
|
|
|
# call to to_model. It is perfectly fine for to_model to return self.
|
2009-10-07 10:24:51 -04:00
|
|
|
module Tests
|
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
|
|
|
|
2010-02-20 21:05:28 -05:00
|
|
|
# == Responds to <tt>to_key</tt>
|
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
|
|
|
#
|
|
|
|
# Returns an Enumerable of all (primary) key attributes
|
2012-02-07 17:10:14 -05:00
|
|
|
# or nil if model.persisted? is false. This is used by
|
|
|
|
# dom_id to generate unique ids 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
|
|
|
|
|
|
|
|
# == Responds to <tt>to_param</tt>
|
|
|
|
#
|
|
|
|
# Returns a string representing the object's key suitable for use in URLs
|
2010-02-21 05:09:21 -05:00
|
|
|
# or nil if model.persisted? is false.
|
2010-02-20 21:05:28 -05: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. However, if the resource is
|
2010-02-21 05:09:21 -05:00
|
|
|
# not persisted?, then to_param should always return nil.
|
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
|
|
|
|
|
2011-08-01 05:42:00 -04:00
|
|
|
# == Responds to <tt>to_partial_path</tt>
|
2011-07-08 17:54:15 -04:00
|
|
|
#
|
|
|
|
# Returns a string giving a relative path. This 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
|
|
|
|
|
2010-02-21 05:09:21 -05:00
|
|
|
# == Responds to <tt>persisted?</tt>
|
2009-08-29 02:49:18 -04:00
|
|
|
#
|
|
|
|
# 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
|
2011-05-06 17:03:55 -04:00
|
|
|
# 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 routes 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
|
|
|
|
|
2010-01-31 18:08:20 -05:00
|
|
|
# == Naming
|
2009-12-31 08:19:30 -05:00
|
|
|
#
|
2011-07-08 17:54:15 -04:00
|
|
|
# Model.model_name must return a string with some convenience methods:
|
|
|
|
# :human, :singular, and :plural. Check ActiveModel::Naming for more information.
|
2009-12-31 08:19:30 -05:00
|
|
|
#
|
|
|
|
def test_model_naming
|
|
|
|
assert model.class.respond_to?(:model_name), "The model should respond to model_name"
|
|
|
|
model_name = model.class.model_name
|
|
|
|
assert_kind_of String, model_name
|
|
|
|
assert_kind_of String, model_name.human
|
2010-01-20 01:35:09 -05:00
|
|
|
assert_kind_of String, model_name.singular
|
|
|
|
assert_kind_of String, model_name.plural
|
2009-12-31 08:19:30 -05:00
|
|
|
end
|
|
|
|
|
2010-01-31 18:08:20 -05:00
|
|
|
# == Errors Testing
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2012-02-07 17:10:14 -05:00
|
|
|
# Returns an object that implements [](attribute) defined which returns 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, this 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
|
|
|
|
assert @model.respond_to?(:to_model), "The object should respond_to to_model"
|
|
|
|
@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
|