Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
eliasjpr committed Nov 7, 2017
1 parent 77373cf commit 451c9c5
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 34 deletions.
2 changes: 1 addition & 1 deletion spec/amber/controller/helpers/redirect_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ module Amber::Controller::Helpers

it "redirects to full controller name as symbol" do
controller = build_controller
redirector = Redirector.from_controller_action(:RedirectController, :show, params: {"id" => "5"})
redirector = Redirector.from_controller_action(RedirectController, :show, params: {"id" => "5"})
redirector.redirect(controller)
assert_expected_response?(controller, "/redirect/5", 302)
end
Expand Down
18 changes: 0 additions & 18 deletions spec/amber/router/route_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,6 @@ module Amber
end
end

describe "#match?" do
it "matches by controller and action" do
subject.match?(:fake, :action).should be_truthy
end

it "does not match with invalid action" do
subject.match?(:fake, :invalid).should be_falsey
end

it "does not match with invalid controller" do
subject.match?(:invalid, :action).should be_falsey
end

it "does not match with invalid controller and invalid action" do
subject.match?(:invalid, :invalid).should be_falsey
end
end

describe "#call" do
context "before action" do
it "does not execute action" do
Expand Down
8 changes: 4 additions & 4 deletions spec/amber/router/router_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -137,19 +137,19 @@ module Amber
router.add routeD

it "matches route by controller and action" do
router.match_by_controller_action(:fake, :index).should eq routeA
router.match_by_controller_action(:fakecontroller, :index).should eq routeA
end

it "matches controller new action" do
router.match_by_controller_action(:fake, :new).should eq routeB
router.match_by_controller_action(:fakecontroller, :new).should eq routeB
end

it "matches controller show action" do
router.match_by_controller_action(:fake, :show).should eq routeC
router.match_by_controller_action(:fakecontroller, :show).should eq routeC
end

it "matches controller another action" do
router.match_by_controller_action(:fake, :another).should eq routeD
router.match_by_controller_action(:fakecontroller, :another).should eq routeD
end
end

Expand Down
13 changes: 11 additions & 2 deletions src/amber/controller/helpers/redirect.cr
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,17 @@ module Amber::Controller::Helpers
@params : Hash(String, String)? = nil
@flash : Hash(String, String)? = nil

def self.from_controller_action(controller : Symbol | Class, action : Symbol, **options)
route = Amber::Server.router.match_by_controller_action(controller, action)
def self.from_controller_action(controller : Class, action : Symbol, **options)
route = Amber::Server.router.match_by_controller_action(controller.to_s.downcase, action)
redirect(route, **options)
end

def self.from_controller_action(controller : Symbol, action : Symbol, **options)
route = Amber::Server.router.match_by_controller_action("#{controller}controller", action)
redirect(route, **options)
end

def self.redirect(route, **options)
params = options[:params]?
location, params = route.not_nil!.substitute_keys_in_path(params)
status = options[:status]? || DEFAULT_STATUS_CODE
Expand Down
8 changes: 0 additions & 8 deletions src/amber/router/route.cr
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,5 @@ module Amber
end
{result, params}
end

def match?(controller : Class, action : Symbol)
self.controller.downcase == controller.to_s.downcase && self.action == action
end

def match?(controller : Symbol, action : Symbol)
self.controller.downcase == "#{controller}controller" && self.action == action
end
end
end
2 changes: 1 addition & 1 deletion src/amber/router/router.cr
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ module Amber
end

def match_by_controller_action(controller, action)
@routes_hash["#{controller}controller##{action}"]
@routes_hash["#{controller}##{action}"]
end

def all
Expand Down

0 comments on commit 451c9c5

Please sign in to comment.