Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 118 lines (108 sloc) 5.6 kB
dbf20c2 @wycats Initial AMo Lint implementation
wycats authored
1 module ActiveModel
2 module Lint
c9a88a2 minor edits in AM documentation [ci skip]
Francesco Rodriguez authored
3 # == Active \Model \Lint \Tests
b4ec6f0 @radar Move ActiveModel::Lint::Tests documentation to be above module declar…
radar authored
4 #
c9a88a2 minor edits in AM documentation [ci skip]
Francesco Rodriguez authored
5 # You can test whether an object is compliant with the Active \Model API by
a17d047 @josevalim Trim down Active Model API by removing valid? and errors.full_messages
josevalim authored
6 # including <tt>ActiveModel::Lint::Tests</tt> in your TestCase. It will
7 # include tests that tell you whether your object is fully compliant,
8 # or if not, which aspects of the API are not implemented.
9 #
10 # Note an object is not required to implement all APIs in order to work
11 # with Action Pack. This module only intends to provide guidance in case
12 # you want all features out of the box.
b4ec6f0 @radar Move ActiveModel::Lint::Tests documentation to be above module declar…
radar authored
13 #
14 # These tests do not attempt to determine the semantic correctness of the
62a1309 update ActiveModel::Lint documentation [ci skip]
Francesco Rodriguez authored
15 # returned values. For instance, you could implement <tt>valid?</tt> to
6e2d35d @robin850 Fix few typos and improve markup at some levels
robin850 authored
16 # always return +true+, and the tests would pass. It is up to you to ensure
62a1309 update ActiveModel::Lint documentation [ci skip]
Francesco Rodriguez authored
17 # that the values are semantically meaningful.
b4ec6f0 @radar Move ActiveModel::Lint::Tests documentation to be above module declar…
radar authored
18 #
62a1309 update ActiveModel::Lint documentation [ci skip]
Francesco Rodriguez authored
19 # Objects you pass in are expected to return a compliant object from a call
20 # to <tt>to_model</tt>. It is perfectly fine for <tt>to_model</tt> to return
21 # +self+.
ff56f3d @josh Rewrite ActiveModel::Lint as a simple TU mixin
josh authored
22 module Tests
9acd686 @snusnu Adds #key and #to_param to the AMo interface
snusnu authored
23
76dc58b @claudiob Better docs for AM::Lint::Tests
claudiob authored
24 # Passes if the object's model responds to <tt>to_key</tt> and if calling
25 # this method returns +nil+ when the object is not persisted.
26 # Fails otherwise.
9acd686 @snusnu Adds #key and #to_param to the AMo interface
snusnu authored
27 #
76dc58b @claudiob Better docs for AM::Lint::Tests
claudiob authored
28 # <tt>to_key</tt> returns an Enumerable of all (primary) key attributes
29 # of the model, and is used to a generate unique DOM id for the object.
f81c6bc @snusnu AMo #key is now #to_key and CI is probably happy
snusnu authored
30 def test_to_key
31 assert model.respond_to?(:to_key), "The model should respond to to_key"
250c809 @josevalim Require persisted? in ActiveModel::Lint and remove new_record? and de…
josevalim authored
32 def model.persisted?() false end
7bd70dc @norman Add messages to plain assertions.
norman authored
33 assert model.to_key.nil?, "to_key should return nil when `persisted?` returns false"
9acd686 @snusnu Adds #key and #to_param to the AMo interface
snusnu authored
34 end
35
76dc58b @claudiob Better docs for AM::Lint::Tests
claudiob authored
36 # Passes if the object's model responds to <tt>to_param</tt> and if
37 # calling this method returns +nil+ when the object is not persisted.
38 # Fails otherwise.
f81c6bc @snusnu AMo #key is now #to_key and CI is probably happy
snusnu authored
39 #
76dc58b @claudiob Better docs for AM::Lint::Tests
claudiob authored
40 # <tt>to_param</tt> is used to represent the object's key in URLs.
62a1309 update ActiveModel::Lint documentation [ci skip]
Francesco Rodriguez authored
41 # Implementers can decide to either raise an exception or provide a
42 # default in case the record uses a composite primary key. There are no
43 # tests for this behavior in lint because it doesn't make sense to force
44 # any of the possible implementation strategies on the implementer.
9acd686 @snusnu Adds #key and #to_param to the AMo interface
snusnu authored
45 def test_to_param
46 assert model.respond_to?(:to_param), "The model should respond to to_param"
d0cf212 @josevalim to_key should return all exists keys (if any exists), regardless if t…
josevalim authored
47 def model.to_key() [1] end
250c809 @josevalim Require persisted? in ActiveModel::Lint and remove new_record? and de…
josevalim authored
48 def model.persisted?() false end
e8458d3 @pixeltrix Fix typo in assertion message
pixeltrix authored
49 assert model.to_param.nil?, "to_param should return nil when `persisted?` returns false"
9acd686 @snusnu Adds #key and #to_param to the AMo interface
snusnu authored
50 end
51
76dc58b @claudiob Better docs for AM::Lint::Tests
claudiob authored
52 # Passes if the object's model responds to <tt>to_partial_path</tt> and if
53 # calling this method returns a string. Fails otherwise.
bf81207 Let ActiveModel instances define partial paths.
Grant Hutchins & Peter Jaros authored
54 #
76dc58b @claudiob Better docs for AM::Lint::Tests
claudiob authored
55 # <tt>to_partial_path</tt> is used for looking up partials. For example,
56 # a BlogPost model might return "blog_posts/blog_post".
dc8773b @josevalim Rename new method to_path to to_partial_path to avoid conflicts with …
josevalim authored
57 def test_to_partial_path
58 assert model.respond_to?(:to_partial_path), "The model should respond to to_partial_path"
59 assert_kind_of String, model.to_partial_path
bf81207 Let ActiveModel instances define partial paths.
Grant Hutchins & Peter Jaros authored
60 end
61
76dc58b @claudiob Better docs for AM::Lint::Tests
claudiob authored
62 # Passes if the object's model responds to <tt>persisted?</tt> and if
63 # calling this method returns either +true+ or +false+. Fails otherwise.
dbf20c2 @wycats Initial AMo Lint implementation
wycats authored
64 #
76dc58b @claudiob Better docs for AM::Lint::Tests
claudiob authored
65 # <tt>persisted?</tt> is used when calculating the URL for an object.
66 # If the object is not persisted, a form for that object, for instance,
67 # will route to the create action. If it is persisted, a form for the
68 # object will route to the update action.
250c809 @josevalim Require persisted? in ActiveModel::Lint and remove new_record? and de…
josevalim authored
69 def test_persisted?
70 assert model.respond_to?(:persisted?), "The model should respond to persisted?"
71 assert_boolean model.persisted?, "persisted?"
dbf20c2 @wycats Initial AMo Lint implementation
wycats authored
72 end
73
76dc58b @claudiob Better docs for AM::Lint::Tests
claudiob authored
74 # Passes if the object's model responds to <tt>model_name</tt> both as
75 # an instance method and as a class method, and if calling this method
76 # returns a string with some convenience methods: <tt>:human</tt>,
77 # <tt>:singular</tt> and <tt>:plural</tt>.
653fa4c @josevalim Add naming to AMo::Lint
josevalim authored
78 #
76dc58b @claudiob Better docs for AM::Lint::Tests
claudiob authored
79 # Check ActiveModel::Naming for more information.
653fa4c @josevalim Add naming to AMo::Lint
josevalim authored
80 def test_model_naming
6b0e834 @amiel Use #model_name on instances instead of classes
amiel authored
81 assert model.class.respond_to?(:model_name), "The model class should respond to model_name"
653fa4c @josevalim Add naming to AMo::Lint
josevalim authored
82 model_name = model.class.model_name
c82fd8f @drogus Allow ActiveModel::Name to duck type
drogus authored
83 assert model_name.respond_to?(:to_str)
84 assert model_name.human.respond_to?(:to_str)
85 assert model_name.singular.respond_to?(:to_str)
86 assert model_name.plural.respond_to?(:to_str)
6b0e834 @amiel Use #model_name on instances instead of classes
amiel authored
87
88 assert model.respond_to?(:model_name), "The model instance should respond to model_name"
89 assert_equal model.model_name, model.class.model_name
653fa4c @josevalim Add naming to AMo::Lint
josevalim authored
90 end
91
76dc58b @claudiob Better docs for AM::Lint::Tests
claudiob authored
92 # Passes if the object's model responds to <tt>errors</tt> and if calling
93 # <tt>[](attribute)</tt> on the result of this method returns an array.
94 # Fails otherwise.
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored
95 #
76dc58b @claudiob Better docs for AM::Lint::Tests
claudiob authored
96 # <tt>errors[attribute]</tt> is used to retrieve the errors of a model
97 # for a given attribute. If errors are present, the method should return
98 # an array of strings that are the errors for the attribute in question.
99 # If localization is used, the strings should be localized for the current
100 # locale. If no error is present, the method should return an empty array.
ff56f3d @josh Rewrite ActiveModel::Lint as a simple TU mixin
josh authored
101 def test_errors_aref
102 assert model.respond_to?(:errors), "The model should respond to errors"
103 assert model.errors[:hello].is_a?(Array), "errors#[] should return an Array"
dbf20c2 @wycats Initial AMo Lint implementation
wycats authored
104 end
105
ff56f3d @josh Rewrite ActiveModel::Lint as a simple TU mixin
josh authored
106 private
107 def model
19c6c8f @fxn respond_to -> respond to in a message from AM::Lint
fxn authored
108 assert @model.respond_to?(:to_model), "The object should respond to to_model"
ff56f3d @josh Rewrite ActiveModel::Lint as a simple TU mixin
josh authored
109 @model.to_model
dbf20c2 @wycats Initial AMo Lint implementation
wycats authored
110 end
111
ff56f3d @josh Rewrite ActiveModel::Lint as a simple TU mixin
josh authored
112 def assert_boolean(result, name)
113 assert result == true || result == false, "#{name} should be a boolean"
114 end
dbf20c2 @wycats Initial AMo Lint implementation
wycats authored
115 end
116 end
e2d0b0e @laktek fixed ActiveModel::Lint typos [#3236 state:resolved]
laktek authored
117 end
Something went wrong with that request. Please try again.