Froze rails gems
[depot.git] / vendor / rails / actionpack / test / controller / record_identifier_test.rb
1 require 'abstract_unit'
2
3 class Comment
4 attr_reader :id
5 def save; @id = 1 end
6 def new_record?; @id.nil? end
7 def name
8 @id.nil? ? 'new comment' : "comment ##{@id}"
9 end
10 end
11
12 class Comment::Nested < Comment; end
13
14 class Test::Unit::TestCase
15 protected
16 def comments_url
17 'http://www.example.com/comments'
18 end
19
20 def comment_url(comment)
21 "http://www.example.com/comments/#{comment.id}"
22 end
23 end
24
25
26 class RecordIdentifierTest < Test::Unit::TestCase
27 include ActionController::RecordIdentifier
28
29 def setup
30 @klass = Comment
31 @record = @klass.new
32 @singular = 'comment'
33 @plural = 'comments'
34 end
35
36 def test_dom_id_with_new_record
37 assert_equal "new_#{@singular}", dom_id(@record)
38 end
39
40 def test_dom_id_with_new_record_and_prefix
41 assert_equal "custom_prefix_#{@singular}", dom_id(@record, :custom_prefix)
42 end
43
44 def test_dom_id_with_saved_record
45 @record.save
46 assert_equal "#{@singular}_1", dom_id(@record)
47 end
48
49 def test_dom_id_with_prefix
50 @record.save
51 assert_equal "edit_#{@singular}_1", dom_id(@record, :edit)
52 end
53
54 def test_partial_path
55 expected = "#{@plural}/#{@singular}"
56 assert_equal expected, partial_path(@record)
57 assert_equal expected, partial_path(Comment)
58 end
59
60 def test_partial_path_with_namespaced_controller_path
61 expected = "admin/#{@plural}/#{@singular}"
62 assert_equal expected, partial_path(@record, "admin/posts")
63 assert_equal expected, partial_path(@klass, "admin/posts")
64 end
65
66 def test_partial_path_with_not_namespaced_controller_path
67 expected = "#{@plural}/#{@singular}"
68 assert_equal expected, partial_path(@record, "posts")
69 assert_equal expected, partial_path(@klass, "posts")
70 end
71
72 def test_dom_class
73 assert_equal @singular, dom_class(@record)
74 end
75
76 def test_dom_class_with_prefix
77 assert_equal "custom_prefix_#{@singular}", dom_class(@record, :custom_prefix)
78 end
79
80 def test_singular_class_name
81 assert_equal @singular, singular_class_name(@record)
82 end
83
84 def test_singular_class_name_for_class
85 assert_equal @singular, singular_class_name(@klass)
86 end
87
88 def test_plural_class_name
89 assert_equal @plural, plural_class_name(@record)
90 end
91
92 def test_plural_class_name_for_class
93 assert_equal @plural, plural_class_name(@klass)
94 end
95
96 private
97 def method_missing(method, *args)
98 RecordIdentifier.send(method, *args)
99 end
100 end
101
102 class NestedRecordIdentifierTest < RecordIdentifierTest
103 def setup
104 @klass = Comment::Nested
105 @record = @klass.new
106 @singular = 'comment_nested'
107 @plural = 'comment_nesteds'
108 end
109
110 def test_partial_path
111 expected = "comment/nesteds/nested"
112 assert_equal expected, partial_path(@record)
113 assert_equal expected, partial_path(Comment::Nested)
114 end
115
116 def test_partial_path_with_namespaced_controller_path
117 expected = "admin/comment/nesteds/nested"
118 assert_equal expected, partial_path(@record, "admin/posts")
119 assert_equal expected, partial_path(@klass, "admin/posts")
120 end
121
122 def test_partial_path_with_deeper_namespaced_controller_path
123 expected = "deeper/admin/comment/nesteds/nested"
124 assert_equal expected, partial_path(@record, "deeper/admin/posts")
125 assert_equal expected, partial_path(@klass, "deeper/admin/posts")
126 end
127
128 def test_partial_path_with_even_deeper_namespaced_controller_path
129 expected = "even/more/deeper/admin/comment/nesteds/nested"
130 assert_equal expected, partial_path(@record, "even/more/deeper/admin/posts")
131 assert_equal expected, partial_path(@klass, "even/more/deeper/admin/posts")
132 end
133
134 def test_partial_path_with_not_namespaced_controller_path
135 expected = "comment/nesteds/nested"
136 assert_equal expected, partial_path(@record, "posts")
137 assert_equal expected, partial_path(@klass, "posts")
138 end
139 end