aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/test/controller/record_identifier_test.rb
blob: 12c1eaea6997574f19a67e19c3c10f46e55be265 (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
require 'abstract_unit'

class Comment
  attr_reader :id
  def save; @id = 1 end
  def new_record?; @id.nil? end
  def name
    @id.nil? ? 'new comment' : "comment ##{@id}"
  end
end

class Comment::Nested < Comment; end

class Test::Unit::TestCase
  protected
    def comments_url
      'http://www.example.com/comments'
    end
    
    def comment_url(comment)
      "http://www.example.com/comments/#{comment.id}"
    end
end


class RecordIdentifierTest < Test::Unit::TestCase
  include ActionController::RecordIdentifier

  def setup
    @klass  = Comment
    @record = @klass.new
    @singular = 'comment'
    @plural = 'comments'
  end

  def test_dom_id_with_new_record
    assert_equal "new_#{@singular}", dom_id(@record)
  end

  def test_dom_id_with_new_record_and_prefix
    assert_equal "custom_prefix_#{@singular}", dom_id(@record, :custom_prefix)
  end

  def test_dom_id_with_saved_record
    @record.save
    assert_equal "#{@singular}_1", dom_id(@record)
  end

  def test_dom_id_with_prefix
    @record.save
    assert_equal "edit_#{@singular}_1", dom_id(@record, :edit)
  end

  def test_partial_path
    expected = "#{@plural}/#{@singular}"
    assert_equal expected, partial_path(@record)
    assert_equal expected, partial_path(Comment)
  end

  def test_partial_path_with_namespaced_controller_path
    expected = "admin/#{@plural}/#{@singular}"
    assert_equal expected, partial_path(@record, "admin/posts")
    assert_equal expected, partial_path(@klass, "admin/posts")
  end

  def test_partial_path_with_not_namespaced_controller_path
    expected = "#{@plural}/#{@singular}"
    assert_equal expected, partial_path(@record, "posts")
    assert_equal expected, partial_path(@klass, "posts")
  end

  def test_dom_class
    assert_equal @singular, dom_class(@record)
  end
  
  def test_dom_class_with_prefix
    assert_equal "custom_prefix_#{@singular}", dom_class(@record, :custom_prefix)
  end

  def test_singular_class_name
    assert_equal @singular, singular_class_name(@record)
  end

  def test_singular_class_name_for_class
    assert_equal @singular, singular_class_name(@klass)
  end

  def test_plural_class_name
    assert_equal @plural, plural_class_name(@record)
  end

  def test_plural_class_name_for_class
    assert_equal @plural, plural_class_name(@klass)
  end

  private
    def method_missing(method, *args)
      RecordIdentifier.send(method, *args)
    end
end

class NestedRecordIdentifierTest < RecordIdentifierTest
  def setup
    @klass  = Comment::Nested
    @record = @klass.new
    @singular = 'comment_nested'
    @plural = 'comment_nesteds'
  end

  def test_partial_path
    expected = "comment/nesteds/nested"
    assert_equal expected, partial_path(@record)
    assert_equal expected, partial_path(Comment::Nested)
  end

  def test_partial_path_with_namespaced_controller_path
    expected = "admin/comment/nesteds/nested"
    assert_equal expected, partial_path(@record, "admin/posts")
    assert_equal expected, partial_path(@klass, "admin/posts")
  end

  def test_partial_path_with_deeper_namespaced_controller_path
    expected = "deeper/admin/comment/nesteds/nested"
    assert_equal expected, partial_path(@record, "deeper/admin/posts")
    assert_equal expected, partial_path(@klass, "deeper/admin/posts")
  end

  def test_partial_path_with_even_deeper_namespaced_controller_path
    expected = "even/more/deeper/admin/comment/nesteds/nested"
    assert_equal expected, partial_path(@record, "even/more/deeper/admin/posts")
    assert_equal expected, partial_path(@klass, "even/more/deeper/admin/posts")
  end

  def test_partial_path_with_not_namespaced_controller_path
    expected = "comment/nesteds/nested"
    assert_equal expected, partial_path(@record, "posts")
    assert_equal expected, partial_path(@klass, "posts")
  end
end