Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Respond with 404 when there is no matching FC class #24

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/focused_controller.rb
Expand Up @@ -2,3 +2,4 @@
require 'focused_controller/route'
require 'focused_controller/mixin'
require 'focused_controller/route_mapper'
require 'focused_controller/railtie' if defined?(Rails)
13 changes: 13 additions & 0 deletions lib/focused_controller/railtie.rb
@@ -0,0 +1,13 @@
module FocusedController
class Railtie < Rails::Railtie
if config.action_dispatch.rescue_responses
config.action_dispatch.rescue_responses['FocusedController::NotFound'] = :not_found
config.action_dispatch.rescue_templates['FocusedController::NotFound'] = 'unknown_action'
else
initializer 'focused_controller.not_found' do
ActionDispatch::ShowExceptions.rescue_responses['FocusedController::NotFound'] = :not_found
ActionDispatch::ShowExceptions.rescue_templates['FocusedController::NotFound'] = 'unknown_action'
end
end
end
end
12 changes: 11 additions & 1 deletion lib/focused_controller/route.rb
@@ -1,4 +1,6 @@
module FocusedController
class NotFound < RuntimeError; end

class Route
attr_reader :name

Expand All @@ -7,9 +9,17 @@ def initialize(name)
end

def call(env)
name.constantize.call(env)
action_class.call(env)
end

alias to_s name

private

def action_class
name.constantize
rescue NameError => e
raise NotFound, e.to_s
end
end
end
9 changes: 8 additions & 1 deletion test/acceptance/app_test.rb
Expand Up @@ -66,7 +66,7 @@ def read_output(stream)
# actually run the test.
def start_server
within_test_app do
IO.popen("bundle exec rails s -p #{FocusedController::Test.port} 2>&1") do |out|
IO.popen("rails s -p #{FocusedController::Test.port} 2>&1") do |out|
start = Time.now
started = false
output = ""
Expand Down Expand Up @@ -130,6 +130,13 @@ def start_server
end
end

it 'renders a 404 error when the requested action is missing' do
start_server do
s.visit '/posts/missing'
s.status_code.must_equal 404
end
end

it 'runs a functional test' do
run_command "ruby -Itest test/functional/posts_controller_test.rb"
end
Expand Down
6 changes: 5 additions & 1 deletion test/app/config/routes.rb
@@ -1,6 +1,10 @@
App::Application.routes.draw do
focused_controller_routes do
resources :posts
resources :posts do
collection do
get :missing
end
end
end

# The priority is based upon order of creation:
Expand Down