aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/test/cases/naming_test.rb
blob: d5cb1a62bc1662d37570c8de22a8bcd159fbcb06 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
require "cases/helper"
require "models/contact"
require "models/sheep"
require "models/track_back"
require "models/blog_post"

class NamingTest < ActiveModel::TestCase
  def setup
    @model_name = ActiveModel::Name.new(Post::TrackBack)
  end

  def test_singular
    assert_equal "post_track_back", @model_name.singular
  end

  def test_plural
    assert_equal "post_track_backs", @model_name.plural
  end

  def test_element
    assert_equal "track_back", @model_name.element
  end

  def test_collection
    assert_equal "post/track_backs", @model_name.collection
  end

  def test_human
    assert_equal "Track back", @model_name.human
  end

  def test_route_key
    assert_equal "post_track_backs", @model_name.route_key
  end

  def test_param_key
    assert_equal "post_track_back", @model_name.param_key
  end

  def test_i18n_key
    assert_equal :"post/track_back", @model_name.i18n_key
  end
end

class NamingWithNamespacedModelInIsolatedNamespaceTest < ActiveModel::TestCase
  def setup
    @model_name = ActiveModel::Name.new(Blog::Post, Blog)
  end

  def test_singular
    assert_equal "blog_post", @model_name.singular
  end

  def test_plural
    assert_equal "blog_posts", @model_name.plural
  end

  def test_element
    assert_equal "post", @model_name.element
  end

  def test_collection
    assert_equal "blog/posts", @model_name.collection
  end

  def test_human
    assert_equal "Post", @model_name.human
  end

  def test_route_key
    assert_equal "posts", @model_name.route_key
  end

  def test_param_key
    assert_equal "post", @model_name.param_key
  end

  def test_i18n_key
    assert_equal :"blog/post", @model_name.i18n_key
  end
end

class NamingWithNamespacedModelInSharedNamespaceTest < ActiveModel::TestCase
  def setup
    @model_name = ActiveModel::Name.new(Blog::Post)
  end

  def test_singular
    assert_equal "blog_post", @model_name.singular
  end

  def test_plural
    assert_equal "blog_posts", @model_name.plural
  end

  def test_element
    assert_equal "post", @model_name.element
  end

  def test_collection
    assert_equal "blog/posts", @model_name.collection
  end

  def test_human
    assert_equal "Post", @model_name.human
  end

  def test_route_key
    assert_equal "blog_posts", @model_name.route_key
  end

  def test_param_key
    assert_equal "blog_post", @model_name.param_key
  end

  def test_i18n_key
    assert_equal :"blog/post", @model_name.i18n_key
  end
end

class NamingWithSuppliedModelNameTest < ActiveModel::TestCase
  def setup
    @model_name = ActiveModel::Name.new(Blog::Post, nil, "Article")
  end

  def test_singular
    assert_equal "article", @model_name.singular
  end

  def test_plural
    assert_equal "articles", @model_name.plural
  end

  def test_element
    assert_equal "article", @model_name.element
  end

  def test_collection
    assert_equal "articles", @model_name.collection
  end

  def test_human
    assert_equal "Article", @model_name.human
  end

  def test_route_key
    assert_equal "articles", @model_name.route_key
  end

  def test_param_key
    assert_equal "article", @model_name.param_key
  end

  def test_i18n_key
    assert_equal :"article", @model_name.i18n_key
  end
end

class NamingUsingRelativeModelNameTest < ActiveModel::TestCase
  def setup
    @model_name = Blog::Post.model_name
  end

  def test_singular
    assert_equal "blog_post", @model_name.singular
  end

  def test_plural
    assert_equal "blog_posts", @model_name.plural
  end

  def test_element
    assert_equal "post", @model_name.element
  end

  def test_collection
    assert_equal "blog/posts", @model_name.collection
  end

  def test_human
    assert_equal "Post", @model_name.human
  end

  def test_route_key
    assert_equal "posts", @model_name.route_key
  end

  def test_param_key
    assert_equal "post", @model_name.param_key
  end

  def test_i18n_key
    assert_equal :"blog/post", @model_name.i18n_key
  end
end

class NamingHelpersTest < ActiveModel::TestCase
  def setup
    @klass  = Contact
    @record = @klass.new
    @singular = "contact"
    @plural = "contacts"
    @uncountable = Sheep
    @singular_route_key = "contact"
    @route_key = "contacts"
    @param_key = "contact"
  end

  def test_to_model_called_on_record
    assert_equal "post_named_track_backs", plural(Post::TrackBack.new)
  end

  def test_singular
    assert_equal @singular, singular(@record)
  end

  def test_singular_for_class
    assert_equal @singular, singular(@klass)
  end

  def test_plural
    assert_equal @plural, plural(@record)
  end

  def test_plural_for_class
    assert_equal @plural, plural(@klass)
  end

  def test_route_key
    assert_equal @route_key, route_key(@record)
    assert_equal @singular_route_key, singular_route_key(@record)
  end

  def test_route_key_for_class
    assert_equal @route_key, route_key(@klass)
    assert_equal @singular_route_key, singular_route_key(@klass)
  end

  def test_param_key
    assert_equal @param_key, param_key(@record)
  end

  def test_param_key_for_class
    assert_equal @param_key, param_key(@klass)
  end

  def test_uncountable
    assert uncountable?(@uncountable), "Expected 'sheep' to be uncountable"
    assert !uncountable?(@klass), "Expected 'contact' to be countable"
  end

  def test_uncountable_route_key
    assert_equal "sheep", singular_route_key(@uncountable)
    assert_equal "sheep_index", route_key(@uncountable)
  end

  private
    def method_missing(method, *args)
      ActiveModel::Naming.send(method, *args)
    end
end

class NameWithAnonymousClassTest < ActiveModel::TestCase
  def test_anonymous_class_without_name_argument
    assert_raises(ArgumentError) do
      ActiveModel::Name.new(Class.new)
    end
  end

  def test_anonymous_class_with_name_argument
    model_name = ActiveModel::Name.new(Class.new, nil, "Anonymous")
    assert_equal "Anonymous", model_name
  end
end

class NamingMethodDelegationTest < ActiveModel::TestCase
  def test_model_name
    assert_equal Blog::Post.model_name, Blog::Post.new.model_name
  end
end