2012-08-09 18:12:43 -04:00
|
|
|
require 'abstract_unit'
|
|
|
|
|
|
|
|
class RoutingConcernsTest < ActionDispatch::IntegrationTest
|
2012-08-22 10:27:42 -04:00
|
|
|
class Reviewable
|
2012-08-26 15:37:23 -04:00
|
|
|
def self.call(mapper, options = {})
|
|
|
|
mapper.resources :reviews, options
|
2012-08-22 10:27:42 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-08-09 18:12:43 -04:00
|
|
|
Routes = ActionDispatch::Routing::RouteSet.new.tap do |app|
|
|
|
|
app.draw do
|
2012-08-26 15:37:23 -04:00
|
|
|
concern :commentable do |options|
|
|
|
|
resources :comments, options
|
2012-08-09 18:12:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
concern :image_attachable do
|
2012-08-13 18:02:00 -04:00
|
|
|
resources :images, only: :index
|
2012-08-09 18:12:43 -04:00
|
|
|
end
|
|
|
|
|
2012-08-22 10:27:42 -04:00
|
|
|
concern :reviewable, Reviewable
|
|
|
|
|
|
|
|
resources :posts, concerns: [:commentable, :image_attachable, :reviewable] do
|
2012-08-26 15:37:23 -04:00
|
|
|
resource :video, concerns: :commentable do
|
|
|
|
concerns :reviewable, as: :video_reviews
|
|
|
|
end
|
2012-08-09 18:12:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
resource :picture, concerns: :commentable do
|
|
|
|
resources :posts, concerns: :commentable
|
|
|
|
end
|
|
|
|
|
|
|
|
scope "/videos" do
|
2012-08-26 15:37:23 -04:00
|
|
|
concerns :commentable, except: :destroy
|
2012-08-09 18:12:43 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
include Routes.url_helpers
|
2014-07-07 13:21:57 -04:00
|
|
|
APP = RoutedRackApp.new Routes
|
|
|
|
def app; APP end
|
2012-08-09 18:12:43 -04:00
|
|
|
|
|
|
|
def test_accessing_concern_from_resources
|
|
|
|
get "/posts/1/comments"
|
|
|
|
assert_equal "200", @response.code
|
|
|
|
assert_equal "/posts/1/comments", post_comments_path(post_id: 1)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_accessing_concern_from_resource
|
|
|
|
get "/picture/comments"
|
|
|
|
assert_equal "200", @response.code
|
|
|
|
assert_equal "/picture/comments", picture_comments_path
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_accessing_concern_from_nested_resource
|
|
|
|
get "/posts/1/video/comments"
|
|
|
|
assert_equal "200", @response.code
|
|
|
|
assert_equal "/posts/1/video/comments", post_video_comments_path(post_id: 1)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_accessing_concern_from_nested_resources
|
|
|
|
get "/picture/posts/1/comments"
|
|
|
|
assert_equal "200", @response.code
|
|
|
|
assert_equal "/picture/posts/1/comments", picture_post_comments_path(post_id: 1)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_accessing_concern_from_resources_with_more_than_one_concern
|
2012-08-13 18:02:00 -04:00
|
|
|
get "/posts/1/images"
|
2012-08-09 18:12:43 -04:00
|
|
|
assert_equal "200", @response.code
|
2012-08-13 18:02:00 -04:00
|
|
|
assert_equal "/posts/1/images", post_images_path(post_id: 1)
|
2012-08-09 18:12:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_accessing_concern_from_resources_using_only_option
|
2012-08-13 18:02:00 -04:00
|
|
|
get "/posts/1/image/1"
|
2012-08-09 18:12:43 -04:00
|
|
|
assert_equal "404", @response.code
|
|
|
|
end
|
|
|
|
|
2012-08-26 15:37:23 -04:00
|
|
|
def test_accessing_callable_concern_
|
2012-08-22 10:27:42 -04:00
|
|
|
get "/posts/1/reviews/1"
|
|
|
|
assert_equal "200", @response.code
|
|
|
|
assert_equal "/posts/1/reviews/1", post_review_path(post_id: 1, id: 1)
|
|
|
|
end
|
|
|
|
|
2012-08-26 15:37:23 -04:00
|
|
|
def test_callable_concerns_accept_options
|
2012-08-22 10:27:42 -04:00
|
|
|
get "/posts/1/video/reviews/1"
|
|
|
|
assert_equal "200", @response.code
|
|
|
|
assert_equal "/posts/1/video/reviews/1", post_video_video_review_path(post_id: 1, id: 1)
|
|
|
|
end
|
|
|
|
|
2012-08-09 18:12:43 -04:00
|
|
|
def test_accessing_concern_from_a_scope
|
|
|
|
get "/videos/comments"
|
|
|
|
assert_equal "200", @response.code
|
|
|
|
end
|
|
|
|
|
2012-08-26 15:37:23 -04:00
|
|
|
def test_concerns_accept_options
|
|
|
|
delete "/videos/comments/1"
|
|
|
|
assert_equal "404", @response.code
|
|
|
|
end
|
|
|
|
|
2012-08-09 18:12:43 -04:00
|
|
|
def test_with_an_invalid_concern_name
|
|
|
|
e = assert_raise ArgumentError do
|
|
|
|
ActionDispatch::Routing::RouteSet.new.tap do |app|
|
|
|
|
app.draw do
|
|
|
|
resources :posts, concerns: :foo
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal "No concern named foo was found!", e.message
|
|
|
|
end
|
2012-08-23 09:02:37 -04:00
|
|
|
|
|
|
|
def test_concerns_executes_block_in_context_of_current_mapper
|
|
|
|
mapper = ActionDispatch::Routing::Mapper.new(ActionDispatch::Routing::RouteSet.new)
|
|
|
|
mapper.concern :test_concern do
|
|
|
|
resources :things
|
|
|
|
return self
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal mapper, mapper.concerns(:test_concern)
|
|
|
|
end
|
2012-08-09 18:12:43 -04:00
|
|
|
end
|