Guest User

Untitled

a guest
Feb 16th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. # 3. This would enable a really focused TransactionsController
  2. # that just handled sending off a use_case to the handler and
  3. # dealing with renders or redirects on the way back in.
  4. #
  5. # In this example we have a context that is per action - but
  6. # maybe we just pass in a broad context of request params,
  7. # any authorization/authentication object and self as a listener
  8. # in to the use_cases handler as a context. This could be a
  9. # method in application controller to clean this up even
  10. # further.
  11.  
  12. class TransactionsController < ApplicationController
  13. attr_writer :new_transaction_form
  14.  
  15. def create
  16. use_cases.execute :create,
  17. context: new_transaction_form,
  18. on_success: :create_succeeded,
  19. on_failure: :create_failed
  20. end
  21.  
  22. def new; end
  23.  
  24. def show
  25. use_cases.execute :show,
  26. context: show_context,
  27. on_success: :transaction_found,
  28. on_failure: :transaction_not_found
  29. end
  30.  
  31. # Callbacks
  32.  
  33. def create_failed(form_with_errors)
  34. self.new_transaction_form = form_with_errors
  35. render new
  36. end
  37.  
  38. def create_succeeded(_new_transaction_form)
  39. flash[:notice] = "Transaction successfully created."
  40. redirect_to transactions_path
  41. end
  42.  
  43. def transaction_found(transaction)
  44. render transaction
  45. end
  46.  
  47. def transaction_not_found(_return_value)
  48. render file: "#{Rails.root}/public/404", status: :not_found
  49. end
  50.  
  51.  
  52. private
  53.  
  54. def create_params
  55. params.permit(:to_wallet_id, :from_wallet_id, :amount)
  56. end
  57.  
  58. def new_transaction_form
  59. @new_transaction_form ||= NewTransactionForm.new(create_params)
  60. end
  61.  
  62. def show_context
  63. params.permit(:id)
  64. end
  65.  
  66. def use_cases
  67. UseCases::TransactionsUseCases.new(self)
  68. end
  69.  
  70. end
Add Comment
Please, Sign In to add comment