aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/test/controller/polymorphic_routes_test.rb
blob: 3e72063e9e160a88737c818ee6fca7f76c682579 (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
require File.dirname(__FILE__) + '/../abstract_unit'

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

class Comment
  attr_reader :id
  def post_id; 1 end
  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 articles_url
    'http://www.example.com/articles'
  end
  alias_method :new_article_url, :articles_url
  
  def article_url(article)
    "http://www.example.com/articles/#{article.id}"
  end

  def article_comments_url(article)
    "http://www.example.com/articles/#{article.id}/comments"
  end
  
  def article_comment_url(article, comment)
    "http://www.example.com/articles/#{article.id}/comments/#{comment.id}"
  end
  
  def admin_articles_url
    "http://www.example.com/admin/articles"
  end
  alias_method :new_admin_article_url, :admin_articles_url
  
  def admin_article_url(article)
    "http://www.example.com/admin/articles/#{article.id}"
  end
  
  def admin_article_comments_url(article)
    "http://www.example.com/admin/articles/#{article.id}/comments"
  end
  
  def admin_article_comment_url(article, comment)
    "http://www.example.com/admin/test/articles/#{article.id}/comments/#{comment.id}"
  end
end


class PolymorphicRoutesTest < Test::Unit::TestCase
  include ActionController::PolymorphicRoutes

  def setup
    @article = Article.new
    @comment = Comment.new
  end
  
  def test_with_record
    assert_equal(articles_url, polymorphic_url(@article, :action => 'new'))
    assert_equal(articles_url, polymorphic_url(@article))
    @article.save
    assert_equal(article_url(@article), polymorphic_url(@article))
  end
  
  # TODO: Needs to be updated to correctly know about whether the object is in a hash or not
  def xtest_with_hash
    @article.save
    assert_equal(article_url(@article), polymorphic_url(:id => @article))
  end

  def test_with_array
    assert_equal(article_comments_url(@article), polymorphic_url([@article, @comment]))
    @comment.save
    assert_equal(article_comment_url(@article, @comment), polymorphic_url([@article, @comment]))
  end  
  
  def test_with_array_and_namespace
    assert_equal(admin_articles_url, polymorphic_url([:admin, @article], :action => 'new'))
    assert_equal(admin_articles_url, polymorphic_url([:admin, @article]))
    @article.save
    assert_equal(admin_article_url(@article), polymorphic_url([:admin, @article]))
    assert_equal(admin_article_comments_url(@article), polymorphic_url([:admin, @article, @comment]))
    @comment.save
    assert_equal(admin_article_comment_url(@article, @comment), polymorphic_url([:admin, @article, @comment]))
  end
end