|
| 1 | +require 'abstract_unit' |
| 2 | + |
| 3 | +class CommentsController < ActionController::Base |
| 4 | + def index |
| 5 | + head :ok |
| 6 | + end |
| 7 | +end |
| 8 | + |
| 9 | +class ImageAttachmentsController < ActionController::Base |
| 10 | + def index |
| 11 | + head :ok |
| 12 | + end |
| 13 | +end |
| 14 | + |
| 15 | +class RoutingConcernsTest < ActionDispatch::IntegrationTest |
| 16 | + Routes = ActionDispatch::Routing::RouteSet.new.tap do |app| |
| 17 | + app.draw do |
| 18 | + concern :commentable do |
| 19 | + resources :comments |
| 20 | + end |
| 21 | + |
| 22 | + concern :image_attachable do |
| 23 | + resources :image_attachments, only: :index |
| 24 | + end |
| 25 | + |
| 26 | + resources :posts, concerns: [:commentable, :image_attachable] do |
| 27 | + resource :video, concerns: :commentable |
| 28 | + end |
| 29 | + |
| 30 | + resource :picture, concerns: :commentable do |
| 31 | + resources :posts, concerns: :commentable |
| 32 | + end |
| 33 | + |
| 34 | + scope "/videos" do |
| 35 | + concerns :commentable |
| 36 | + end |
| 37 | + end |
| 38 | + end |
| 39 | + |
| 40 | + include Routes.url_helpers |
| 41 | + def app; Routes end |
| 42 | + |
| 43 | + def test_accessing_concern_from_resources |
| 44 | + get "/posts/1/comments" |
| 45 | + assert_equal "200", @response.code |
| 46 | + assert_equal "/posts/1/comments", post_comments_path(post_id: 1) |
| 47 | + end |
| 48 | + |
| 49 | + def test_accessing_concern_from_resource |
| 50 | + get "/picture/comments" |
| 51 | + assert_equal "200", @response.code |
| 52 | + assert_equal "/picture/comments", picture_comments_path |
| 53 | + end |
| 54 | + |
| 55 | + def test_accessing_concern_from_nested_resource |
| 56 | + get "/posts/1/video/comments" |
| 57 | + assert_equal "200", @response.code |
| 58 | + assert_equal "/posts/1/video/comments", post_video_comments_path(post_id: 1) |
| 59 | + end |
| 60 | + |
| 61 | + def test_accessing_concern_from_nested_resources |
| 62 | + get "/picture/posts/1/comments" |
| 63 | + assert_equal "200", @response.code |
| 64 | + assert_equal "/picture/posts/1/comments", picture_post_comments_path(post_id: 1) |
| 65 | + end |
| 66 | + |
| 67 | + def test_accessing_concern_from_resources_with_more_than_one_concern |
| 68 | + get "/posts/1/image_attachments" |
| 69 | + assert_equal "200", @response.code |
| 70 | + assert_equal "/posts/1/image_attachments", post_image_attachments_path(post_id: 1) |
| 71 | + end |
| 72 | + |
| 73 | + def test_accessing_concern_from_resources_using_only_option |
| 74 | + get "/posts/1/image_attachment/1" |
| 75 | + assert_equal "404", @response.code |
| 76 | + end |
| 77 | + |
| 78 | + def test_accessing_concern_from_a_scope |
| 79 | + get "/videos/comments" |
| 80 | + assert_equal "200", @response.code |
| 81 | + end |
| 82 | + |
| 83 | + def test_with_an_invalid_concern_name |
| 84 | + e = assert_raise ArgumentError do |
| 85 | + ActionDispatch::Routing::RouteSet.new.tap do |app| |
| 86 | + app.draw do |
| 87 | + resources :posts, concerns: :foo |
| 88 | + end |
| 89 | + end |
| 90 | + end |
| 91 | + |
| 92 | + assert_equal "No concern named foo was found!", e.message |
| 93 | + end |
| 94 | +end |
0 commit comments