aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/test/cases/naming_test.rb
blob: a7dde2c4330ec02d91f72b066e73769daf88588a (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
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_partial_path
    assert_equal 'post/track_backs/track_back', @model_name.partial_path
  end

  def test_human
    assert_equal 'Track back', @model_name.human
  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_partial_path
    assert_equal 'blog/posts/post', @model_name.partial_path
  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_recognizing_namespace
    assert_equal 'Post', Blog::Post.model_name.instance_variable_get("@unnamespaced")
  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_partial_path
    assert_equal 'blog/posts/post', @model_name.partial_path
  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
end

class NamingHelpersTest < Test::Unit::TestCase
  def setup
    @klass  = Contact
    @record = @klass.new
    @singular = 'contact'
    @plural = 'contacts'
    @uncountable = Sheep
    @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)
  end

  def test_route_key_for_class
    assert_equal @route_key, 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 uncoutable"
    assert !uncountable?(@klass), "Expected 'contact' to be countable"
  end

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