| 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_dom_class |
|---|
| 61 |
assert_equal @singular, dom_class(@record) |
|---|
| 62 |
end |
|---|
| 63 |
|
|---|
| 64 |
def test_dom_class_with_prefix |
|---|
| 65 |
assert_equal "custom_prefix_#{@singular}", dom_class(@record, :custom_prefix) |
|---|
| 66 |
end |
|---|
| 67 |
|
|---|
| 68 |
def test_singular_class_name |
|---|
| 69 |
assert_equal @singular, singular_class_name(@record) |
|---|
| 70 |
end |
|---|
| 71 |
|
|---|
| 72 |
def test_singular_class_name_for_class |
|---|
| 73 |
assert_equal @singular, singular_class_name(@klass) |
|---|
| 74 |
end |
|---|
| 75 |
|
|---|
| 76 |
def test_plural_class_name |
|---|
| 77 |
assert_equal @plural, plural_class_name(@record) |
|---|
| 78 |
end |
|---|
| 79 |
|
|---|
| 80 |
def test_plural_class_name_for_class |
|---|
| 81 |
assert_equal @plural, plural_class_name(@klass) |
|---|
| 82 |
end |
|---|
| 83 |
|
|---|
| 84 |
private |
|---|
| 85 |
def method_missing(method, *args) |
|---|
| 86 |
RecordIdentifier.send(method, *args) |
|---|
| 87 |
end |
|---|
| 88 |
end |
|---|
| 89 |
|
|---|
| 90 |
class NestedRecordIdentifierTest < RecordIdentifierTest |
|---|
| 91 |
def setup |
|---|
| 92 |
@klass = Comment::Nested |
|---|
| 93 |
@record = @klass.new |
|---|
| 94 |
@singular = 'comment_nested' |
|---|
| 95 |
@plural = 'comment_nesteds' |
|---|
| 96 |
end |
|---|
| 97 |
|
|---|
| 98 |
def test_partial_path |
|---|
| 99 |
expected = "comment/nesteds/nested" |
|---|
| 100 |
assert_equal expected, partial_path(@record) |
|---|
| 101 |
assert_equal expected, partial_path(Comment::Nested) |
|---|
| 102 |
end |
|---|
| 103 |
end |
|---|