Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # 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
Add Comment
Please, Sign In to add comment