Created
February 15, 2019 02:43
-
-
Save RichardJordan/cad1513c121796259bcfe097bf269b22 to your computer and use it in GitHub Desktop.
Use Cases pattern spike - part 3
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
# 3. This would enable a really focused TransactionsController | |
# that just handled sending off a use_case to the handler and | |
# dealing with renders or redirects on the way back in. | |
# | |
# In this example we have a context that is per action - but | |
# maybe we just pass in a broad context of request params, | |
# any authorization/authentication object and self as a listener | |
# in to the use_cases handler as a context. This could be a | |
# method in application controller to clean this up even | |
# further. | |
class TransactionsController < ApplicationController | |
attr_writer :new_transaction_form | |
def create | |
use_cases.execute :create, | |
context: new_transaction_form, | |
on_success: :create_succeeded, | |
on_failure: :create_failed | |
end | |
def new; end | |
def show | |
use_cases.execute :show, | |
context: show_context, | |
on_success: :transaction_found, | |
on_failure: :transaction_not_found | |
end | |
# Callbacks | |
def create_failed(form_with_errors) | |
self.new_transaction_form = form_with_errors | |
render new | |
end | |
def create_succeeded(_new_transaction_form) | |
flash[:notice] = "Transaction successfully created." | |
redirect_to transactions_path | |
end | |
def transaction_found(transaction) | |
render transaction | |
end | |
def transaction_not_found(_return_value) | |
render file: "#{Rails.root}/public/404", status: :not_found | |
end | |
private | |
def create_params | |
params.permit(:to_wallet_id, :from_wallet_id, :amount) | |
end | |
def new_transaction_form | |
@new_transaction_form ||= NewTransactionForm.new(create_params) | |
end | |
def show_context | |
params.permit(:id) | |
end | |
def use_cases | |
UseCases::TransactionsUseCases.new(self) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment