2010-06-14 09:14:49 +00:00
|
|
|
# == Active Model Lint Tests
|
2010-06-14 09:13:06 +00:00
|
|
|
#
|
2010-06-14 21:21:53 +00:00
|
|
|
# You can test whether an object is compliant with the Active Model API by
|
2010-06-14 09:13:06 +00:00
|
|
|
# including <tt>ActiveModel::Lint::Tests</tt> in your TestCase. It will include
|
2009-10-07 14:24:51 +00:00
|
|
|
# tests that tell you whether your object is fully compliant, or if not,
|
2009-08-29 06:49:18 +00:00
|
|
|
# which aspects of the API are not implemented.
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
module ActiveModel
|
|
|
|
module Lint
|
2009-10-07 14:24:51 +00: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 07:24:10 +00:00
|
|
|
|
2010-02-21 02:05:28 +00: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 07:24:10 +00:00
|
|
|
#
|
|
|
|
# Returns an Enumerable of all (primary) key attributes
|
2010-02-21 10:09:21 +00:00
|
|
|
# or nil if model.persisted? is false
|
2010-02-21 02:05:28 +00:00
|
|
|
def test_to_key
|
|
|
|
assert model.respond_to?(:to_key), "The model should respond to to_key"
|
2010-02-21 10:09:21 +00:00
|
|
|
def model.persisted?() false end
|
2010-02-21 02:05:28 +00:00
|
|
|
assert model.to_key.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 07:24:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# == Responds to <tt>to_param</tt>
|
|
|
|
#
|
|
|
|
# Returns a string representing the object's key suitable for use in URLs
|
2010-02-21 10:09:21 +00:00
|
|
|
# or nil if model.persisted? is false.
|
2010-02-21 02:05:28 +00: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 10:09:21 +00: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 07:24:10 +00:00
|
|
|
def test_to_param
|
|
|
|
assert model.respond_to?(:to_param), "The model should respond to to_param"
|
2010-08-15 14:29:15 +00:00
|
|
|
def model.to_key() [1] end
|
2010-02-21 10:09:21 +00:00
|
|
|
def model.persisted?() false end
|
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 07:24:10 +00:00
|
|
|
assert model.to_param.nil?
|
|
|
|
end
|
|
|
|
|
2010-01-31 23:08:20 +00:00
|
|
|
# == Responds to <tt>valid?</tt>
|
2009-08-29 06:49:18 +00:00
|
|
|
#
|
|
|
|
# Returns a boolean that specifies whether the object is in a valid or invalid
|
|
|
|
# state.
|
|
|
|
def test_valid?
|
2009-10-07 14:24:51 +00:00
|
|
|
assert model.respond_to?(:valid?), "The model should respond to valid?"
|
|
|
|
assert_boolean model.valid?, "valid?"
|
2009-08-29 06:49:18 +00:00
|
|
|
end
|
|
|
|
|
2010-02-21 10:09:21 +00:00
|
|
|
# == Responds to <tt>persisted?</tt>
|
2009-08-29 06:49:18 +00: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
|
|
|
|
# not persisted, a form for that object, for instance, will be POSTed to the
|
2010-06-15 17:38:22 +00:00
|
|
|
# collection. If it is persisted, a form for the object will be PUT to the
|
2009-08-29 06:49:18 +00:00
|
|
|
# URL for the object.
|
2010-02-21 10:09:21 +00:00
|
|
|
def test_persisted?
|
|
|
|
assert model.respond_to?(:persisted?), "The model should respond to persisted?"
|
|
|
|
assert_boolean model.persisted?, "persisted?"
|
2009-08-29 06:49:18 +00:00
|
|
|
end
|
|
|
|
|
2010-01-31 23:08:20 +00:00
|
|
|
# == Naming
|
2009-12-31 13:19:30 +00:00
|
|
|
#
|
2010-06-15 17:38:22 +00:00
|
|
|
# Model.model_name must return a string with some convenience methods as
|
2009-12-31 13:19:30 +00:00
|
|
|
# :human and :partial_path. Check ActiveModel::Naming for more information.
|
|
|
|
#
|
|
|
|
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
|
|
|
|
assert_kind_of String, model_name.partial_path
|
2010-01-20 06:35:09 +00:00
|
|
|
assert_kind_of String, model_name.singular
|
|
|
|
assert_kind_of String, model_name.plural
|
2009-12-31 13:19:30 +00:00
|
|
|
end
|
|
|
|
|
2010-01-31 23:08:20 +00:00
|
|
|
# == Errors Testing
|
2010-08-14 05:13:00 +00:00
|
|
|
#
|
2009-08-29 06:49:18 +00:00
|
|
|
# Returns an object that has :[] and :full_messages defined on it. See below
|
|
|
|
# for more details.
|
2010-01-31 23:08:20 +00:00
|
|
|
#
|
2009-10-07 14:24:51 +00:00
|
|
|
# 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.
|
|
|
|
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 06:49:18 +00:00
|
|
|
end
|
|
|
|
|
2009-10-07 14:24:51 +00:00
|
|
|
# Returns an Array of all error messages for the object. Each message
|
|
|
|
# should contain information about the field, if applicable.
|
|
|
|
def test_errors_full_messages
|
|
|
|
assert model.respond_to?(:errors), "The model should respond to errors"
|
|
|
|
assert model.errors.full_messages.is_a?(Array), "errors#full_messages should return an Array"
|
|
|
|
end
|
2009-08-29 06:49:18 +00:00
|
|
|
|
2009-10-07 14:24:51 +00:00
|
|
|
private
|
|
|
|
def model
|
|
|
|
assert @model.respond_to?(:to_model), "The object should respond_to to_model"
|
|
|
|
@model.to_model
|
2009-08-29 06:49:18 +00:00
|
|
|
end
|
|
|
|
|
2009-10-07 14:24:51 +00:00
|
|
|
def assert_boolean(result, name)
|
|
|
|
assert result == true || result == false, "#{name} should be a boolean"
|
|
|
|
end
|
2009-08-29 06:49:18 +00:00
|
|
|
end
|
|
|
|
end
|
2009-09-21 14:49:43 +00:00
|
|
|
end
|