Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions lib/modular_routes/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ class Builder

attr_reader :routes

def initialize(api_only:)
def initialize(api_only:, controller_method:)
@api_only = api_only
@controller_method = controller_method
@scopes = []
@routes = []
end
Expand Down Expand Up @@ -77,11 +78,11 @@ def resource(resource_name, **options, &block)
:standalone
end

Routable.for(routable, method, action, options)
Routable.for(routable, method, action, options.merge(controller_method: @controller_method))
end

private def build_scopable(type, name, options)
Scopable.for(type, name, options.merge(api_only: @api_only))
Scopable.for(type, name, options.merge(api_only: @api_only, controller_method: @controller_method))
end

private def apply_scopable(type, name, options, &block)
Expand Down
3 changes: 2 additions & 1 deletion lib/modular_routes/extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ module ModularRoutes
module Extension
def modular_routes(**options, &block)
api_only = options.fetch(:api_only, api_only?)
controller_method = options.fetch(:controller_method, 'call')

route_builder = Builder.new(api_only: api_only)
route_builder = Builder.new(api_only: api_only, controller_method: controller_method)
route_builder.instance_eval(&block)
route_builder.routes.each { |route| route.apply(self) }
end
Expand Down
5 changes: 3 additions & 2 deletions lib/modular_routes/routable/non_restful.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ class NonRestful
def initialize(http_method, action, options)
@http_method = http_method
@action = action
@options = options
@options = options.except(:controller_method)
@controller_method = options.fetch(:controller_method)
end

def apply(mapper)
Expand All @@ -15,7 +16,7 @@ def apply(mapper)

private def options
mutable_options = {
to: "#{@action}#call",
to: "#{@action}##{@controller_method}",
}

mutable_options.merge(@options)
Expand Down
5 changes: 3 additions & 2 deletions lib/modular_routes/routable/restful.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class Restful
def initialize(action, resource)
@action = action
@resource = resource
@controller_method = resource.options.fetch(:controller_method)
end

def apply(mapper)
Expand All @@ -21,14 +22,14 @@ def apply(mapper)
end

private def resource_options
@resource.options
@resource.options.except(:controller_method)
end

private def options
immutable = {
controller: @action,
only: @action,
action: :call,
action: @controller_method,
}

resource_options.merge(immutable)
Expand Down
5 changes: 3 additions & 2 deletions lib/modular_routes/routable/standalone.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ class Standalone
def initialize(http_method, action, options)
@http_method = http_method
@action = action
@options = options
@options = options.except(:controller_method)
@controller_method = options.fetch(:controller_method)
end

def apply(mapper)
Expand All @@ -18,7 +19,7 @@ def apply(mapper)

if namespace_controller_pattern?(to)
namespace, controller = to.split("#")
@options[:to] = "#{namespace}/#{controller}#call"
@options[:to] = "#{namespace}/#{controller}##{@controller_method}"
end

@options
Expand Down
2 changes: 1 addition & 1 deletion lib/modular_routes/scopable/namespace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Scopable
class Namespace
def initialize(name, options)
@name = name
@options = options.except(:api_only)
@options = options.except(:api_only, :controller_method)

@children = []
end
Expand Down
2 changes: 1 addition & 1 deletion lib/modular_routes/scopable/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def add(route_or_scope)
end

def apply(mapper)
mapper.public_send(resource_type, @name, @options) do
mapper.public_send(resource_type, @name, @options.except(:controller_method)) do
@children.each { |route_or_scope| route_or_scope.apply(mapper) }

apply_concerns(mapper)
Expand Down
2 changes: 1 addition & 1 deletion lib/modular_routes/scopable/scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Scopable
class Scope
def initialize(args, options)
@args = args
@options = options.except(:api_only)
@options = options.except(:api_only, :controller_method)

@children = []
end
Expand Down
4 changes: 4 additions & 0 deletions spec/internal/app/controllers/recipes/create_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module Recipes
class CreateController < ApplicationController
end
end
4 changes: 4 additions & 0 deletions spec/internal/app/controllers/recipes/destroy_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module Recipes
class DestroyController < ApplicationController
end
end
4 changes: 4 additions & 0 deletions spec/internal/app/controllers/recipes/edit_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module Recipes
class EditController < ApplicationController
end
end
4 changes: 4 additions & 0 deletions spec/internal/app/controllers/recipes/index_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module Recipes
class IndexController < ApplicationController
end
end
4 changes: 4 additions & 0 deletions spec/internal/app/controllers/recipes/new_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module Recipes
class NewController < ApplicationController
end
end
4 changes: 4 additions & 0 deletions spec/internal/app/controllers/recipes/show_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module Recipes
class ShowController < ApplicationController
end
end
4 changes: 4 additions & 0 deletions spec/internal/app/controllers/recipes/update_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module Recipes
class UpdateController < ApplicationController
end
end
5 changes: 5 additions & 0 deletions spec/internal/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,9 @@

resource :book
end

# controller method
modular_routes controller_method: :execute do
resources :recipes
end
end
2 changes: 1 addition & 1 deletion spec/modular_routes/builder_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

RSpec.describe ModularRoutes::Builder do
subject(:builder) { described_class.new(api_only: false) }
subject(:builder) { described_class.new(api_only: false, controller_method: "call") }

describe "#concerns" do
it "raises SyntaxError" do
Expand Down
10 changes: 10 additions & 0 deletions spec/route_mapping_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,14 @@
expect(put: "/articles/1/activate").to route_to(controller: "articles/activate", action: "call", id: "1")
end
end

context "with controller_method" do
it "maps_resources with controller_method execute" do
expect(get: "/recipes").to route_to(controller: "recipes/index", action: "execute")
expect(post: "/recipes").to route_to(controller: "recipes/create", action: "execute")
expect(put: "/recipes/1").to route_to(controller: "recipes/update", action: "execute", id: "1")
expect(get: "/recipes/1").to route_to(controller: "recipes/show", action: "execute", id: "1")
expect(delete: "/recipes/1").to route_to(controller: "recipes/destroy", action: "execute", id: "1")
end
end
end