Last active
August 29, 2015 14:16
-
-
Save booch/bbd6c40e3c984074da33 to your computer and use it in GitHub Desktop.
Callback ideas for hexagonal Rails controllers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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