Skip to content

Instantly share code, notes, and snippets.

@booch
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save booch/bbd6c40e3c984074da33 to your computer and use it in GitHub Desktop.
Save booch/bbd6c40e3c984074da33 to your computer and use it in GitHub Desktop.
Callback ideas for hexagonal Rails controllers
# From http://blog.boochtek.com/2015/02/23/hexagonal-rails-controllers
class OrderController < ApplicationController
def index
interactor.on(:display) { |orders| render orders }
interactor.list
end
def show
interactor.on(:display) { |order| render order }
interactor.on(:not_found) { |order_id| render status: 404 }
interactor.get(params[:id])
end
end
# Same thing, with callback API suggested by @adkron.
class OrderController < ApplicationController
def index
interactor.list do |on|
on.display { |orders| render orders }
end
end
def show
interactor.get(params[:id]) do |on|
on.display { |order| render order }
on.not_found { |order_id| render status: 404 }
end
end
end
# Same thing, with callbacks using stabby lambdas as arguments.
class OrderController < ApplicationController
def index
interactor.list(
display: ->(orders){ render orders }
)
end
def show
interactor.get(params[:id],
display: ->(order) { render order },
not_found: ->(order_id) { render status: 404 }
)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment