aldous 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (212) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +19 -0
  3. data/.irbrc +3 -0
  4. data/.rspec +3 -0
  5. data/.ruby-version +1 -0
  6. data/Gemfile +4 -0
  7. data/LICENSE.txt +22 -0
  8. data/README.md +591 -0
  9. data/Rakefile +1 -0
  10. data/aldous.gemspec +24 -0
  11. data/examples/basic_todo/.foreman +1 -0
  12. data/examples/basic_todo/.gitignore +16 -0
  13. data/examples/basic_todo/.rspec +3 -0
  14. data/examples/basic_todo/.ruby-version +2 -0
  15. data/examples/basic_todo/Gemfile +52 -0
  16. data/examples/basic_todo/Procfile +1 -0
  17. data/examples/basic_todo/README.rdoc +28 -0
  18. data/examples/basic_todo/Rakefile +6 -0
  19. data/examples/basic_todo/app/assets/images/.keep +0 -0
  20. data/examples/basic_todo/app/assets/javascripts/application.js +13 -0
  21. data/examples/basic_todo/app/assets/stylesheets/application.css +15 -0
  22. data/examples/basic_todo/app/controller_actions/base_action.rb +24 -0
  23. data/examples/basic_todo/app/controller_actions/base_precondition.rb +2 -0
  24. data/examples/basic_todo/app/controller_actions/home_controller/show.rb +8 -0
  25. data/examples/basic_todo/app/controller_actions/shared/ensure_user_not_disabled_precondition.rb +9 -0
  26. data/examples/basic_todo/app/controller_actions/sign_ins_controller/create.rb +24 -0
  27. data/examples/basic_todo/app/controller_actions/sign_ins_controller/destroy.rb +9 -0
  28. data/examples/basic_todo/app/controller_actions/sign_ins_controller/new.rb +7 -0
  29. data/examples/basic_todo/app/controller_actions/sign_ins_controller/user_params.rb +9 -0
  30. data/examples/basic_todo/app/controller_actions/sign_ups_controller/create.rb +23 -0
  31. data/examples/basic_todo/app/controller_actions/sign_ups_controller/new.rb +7 -0
  32. data/examples/basic_todo/app/controller_actions/sign_ups_controller/user_params.rb +9 -0
  33. data/examples/basic_todo/app/controller_actions/todos/all_completed_controller/destroy.rb +17 -0
  34. data/examples/basic_todo/app/controller_actions/todos/completed_controller/create.rb +29 -0
  35. data/examples/basic_todo/app/controller_actions/todos_controller/create.rb +26 -0
  36. data/examples/basic_todo/app/controller_actions/todos_controller/destroy.rb +21 -0
  37. data/examples/basic_todo/app/controller_actions/todos_controller/edit.rb +19 -0
  38. data/examples/basic_todo/app/controller_actions/todos_controller/index.rb +19 -0
  39. data/examples/basic_todo/app/controller_actions/todos_controller/new.rb +17 -0
  40. data/examples/basic_todo/app/controller_actions/todos_controller/todo_params.rb +9 -0
  41. data/examples/basic_todo/app/controller_actions/todos_controller/update.rb +28 -0
  42. data/examples/basic_todo/app/controller_actions/users_controller/index.rb +19 -0
  43. data/examples/basic_todo/app/controllers/application_controller.rb +9 -0
  44. data/examples/basic_todo/app/controllers/home_controller.rb +5 -0
  45. data/examples/basic_todo/app/controllers/sign_ins_controller.rb +5 -0
  46. data/examples/basic_todo/app/controllers/sign_ups_controller.rb +5 -0
  47. data/examples/basic_todo/app/controllers/todos/all_completed_controller.rb +5 -0
  48. data/examples/basic_todo/app/controllers/todos/completed_controller.rb +5 -0
  49. data/examples/basic_todo/app/controllers/todos_controller.rb +5 -0
  50. data/examples/basic_todo/app/controllers/users_controller.rb +5 -0
  51. data/examples/basic_todo/app/helpers/application_helper.rb +2 -0
  52. data/examples/basic_todo/app/mailers/.keep +0 -0
  53. data/examples/basic_todo/app/models/ability.rb +27 -0
  54. data/examples/basic_todo/app/models/role.rb +5 -0
  55. data/examples/basic_todo/app/models/todo.rb +5 -0
  56. data/examples/basic_todo/app/models/user.rb +12 -0
  57. data/examples/basic_todo/app/models/user_role.rb +5 -0
  58. data/examples/basic_todo/app/services/create_user_service.rb +26 -0
  59. data/examples/basic_todo/app/services/find_current_user_service.rb +29 -0
  60. data/examples/basic_todo/app/services/sign_in_service.rb +13 -0
  61. data/examples/basic_todo/app/services/sign_out_service.rb +12 -0
  62. data/examples/basic_todo/app/views/base_view.rb +18 -0
  63. data/examples/basic_todo/app/views/defaults/bad_request.html.slim +12 -0
  64. data/examples/basic_todo/app/views/defaults/bad_request_view.rb +15 -0
  65. data/examples/basic_todo/app/views/defaults/forbidden.html.slim +6 -0
  66. data/examples/basic_todo/app/views/defaults/forbidden_view.rb +14 -0
  67. data/examples/basic_todo/app/views/defaults/server_error.html.slim +12 -0
  68. data/examples/basic_todo/app/views/defaults/server_error_view.rb +14 -0
  69. data/examples/basic_todo/app/views/home/show.html.slim +5 -0
  70. data/examples/basic_todo/app/views/home/show_redirect.rb +5 -0
  71. data/examples/basic_todo/app/views/home/show_view.rb +7 -0
  72. data/examples/basic_todo/app/views/layouts/application.html.slim +18 -0
  73. data/examples/basic_todo/app/views/modules/_header.html.slim +13 -0
  74. data/examples/basic_todo/app/views/modules/header_view.rb +7 -0
  75. data/examples/basic_todo/app/views/sign_ins/new.html.slim +14 -0
  76. data/examples/basic_todo/app/views/sign_ins/new_view.rb +10 -0
  77. data/examples/basic_todo/app/views/sign_ups/new.html.slim +13 -0
  78. data/examples/basic_todo/app/views/sign_ups/new_view.rb +10 -0
  79. data/examples/basic_todo/app/views/todos/edit.html.slim +14 -0
  80. data/examples/basic_todo/app/views/todos/edit_view.rb +10 -0
  81. data/examples/basic_todo/app/views/todos/index.html.slim +12 -0
  82. data/examples/basic_todo/app/views/todos/index_redirect.rb +5 -0
  83. data/examples/basic_todo/app/views/todos/index_view/_todo.html.slim +8 -0
  84. data/examples/basic_todo/app/views/todos/index_view/todo_view.rb +28 -0
  85. data/examples/basic_todo/app/views/todos/index_view.rb +26 -0
  86. data/examples/basic_todo/app/views/todos/new.html.slim +14 -0
  87. data/examples/basic_todo/app/views/todos/new_view.rb +10 -0
  88. data/examples/basic_todo/app/views/todos/not_found.html.slim +6 -0
  89. data/examples/basic_todo/app/views/todos/not_found_view.rb +15 -0
  90. data/examples/basic_todo/app/views/users/index.html.slim +7 -0
  91. data/examples/basic_todo/app/views/users/index_redirect.rb +5 -0
  92. data/examples/basic_todo/app/views/users/index_view/_user.html.slim +2 -0
  93. data/examples/basic_todo/app/views/users/index_view/user_view.rb +22 -0
  94. data/examples/basic_todo/app/views/users/index_view.rb +26 -0
  95. data/examples/basic_todo/bin/bundle +3 -0
  96. data/examples/basic_todo/bin/rails +8 -0
  97. data/examples/basic_todo/bin/rake +8 -0
  98. data/examples/basic_todo/bin/rspec +7 -0
  99. data/examples/basic_todo/bin/spring +15 -0
  100. data/examples/basic_todo/config/application.rb +41 -0
  101. data/examples/basic_todo/config/boot.rb +4 -0
  102. data/examples/basic_todo/config/database.yml +25 -0
  103. data/examples/basic_todo/config/environment.rb +5 -0
  104. data/examples/basic_todo/config/environments/development.rb +37 -0
  105. data/examples/basic_todo/config/environments/production.rb +78 -0
  106. data/examples/basic_todo/config/environments/test.rb +39 -0
  107. data/examples/basic_todo/config/initializers/aldous.rb +3 -0
  108. data/examples/basic_todo/config/initializers/assets.rb +8 -0
  109. data/examples/basic_todo/config/initializers/backtrace_silencers.rb +7 -0
  110. data/examples/basic_todo/config/initializers/cookies_serializer.rb +3 -0
  111. data/examples/basic_todo/config/initializers/filter_parameter_logging.rb +4 -0
  112. data/examples/basic_todo/config/initializers/inflections.rb +16 -0
  113. data/examples/basic_todo/config/initializers/mime_types.rb +4 -0
  114. data/examples/basic_todo/config/initializers/session_store.rb +3 -0
  115. data/examples/basic_todo/config/initializers/wrap_parameters.rb +14 -0
  116. data/examples/basic_todo/config/locales/en.yml +23 -0
  117. data/examples/basic_todo/config/routes.rb +18 -0
  118. data/examples/basic_todo/config/secrets.yml +22 -0
  119. data/examples/basic_todo/config.ru +4 -0
  120. data/examples/basic_todo/db/migrate/20150226035524_create_user.rb +10 -0
  121. data/examples/basic_todo/db/migrate/20150227004411_create_todo.rb +11 -0
  122. data/examples/basic_todo/db/migrate/20150301110126_roles.rb +22 -0
  123. data/examples/basic_todo/db/migrate/20150301121923_add_user_disabled_column.rb +5 -0
  124. data/examples/basic_todo/db/schema.rb +45 -0
  125. data/examples/basic_todo/db/seeds.rb +7 -0
  126. data/examples/basic_todo/lib/assets/.keep +0 -0
  127. data/examples/basic_todo/lib/tasks/.keep +0 -0
  128. data/examples/basic_todo/log/.keep +0 -0
  129. data/examples/basic_todo/public/404.html +67 -0
  130. data/examples/basic_todo/public/422.html +67 -0
  131. data/examples/basic_todo/public/500.html +66 -0
  132. data/examples/basic_todo/public/favicon.ico +0 -0
  133. data/examples/basic_todo/public/robots.txt +5 -0
  134. data/examples/basic_todo/test/controllers/.keep +0 -0
  135. data/examples/basic_todo/test/fixtures/.keep +0 -0
  136. data/examples/basic_todo/test/helpers/.keep +0 -0
  137. data/examples/basic_todo/test/integration/.keep +0 -0
  138. data/examples/basic_todo/test/mailers/.keep +0 -0
  139. data/examples/basic_todo/test/models/.keep +0 -0
  140. data/examples/basic_todo/test/test_helper.rb +10 -0
  141. data/examples/basic_todo/vendor/assets/javascripts/.keep +0 -0
  142. data/examples/basic_todo/vendor/assets/stylesheets/.keep +0 -0
  143. data/lib/aldous/build_respondable_service.rb +23 -0
  144. data/lib/aldous/configuration.rb +18 -0
  145. data/lib/aldous/controller/action/precondition/wrapper.rb +32 -0
  146. data/lib/aldous/controller/action/precondition.rb +52 -0
  147. data/lib/aldous/controller/action/result_execution_service.rb +27 -0
  148. data/lib/aldous/controller/action/wrapper.rb +34 -0
  149. data/lib/aldous/controller/action_execution_service.rb +42 -0
  150. data/lib/aldous/controller/preconditions_execution_service.rb +32 -0
  151. data/lib/aldous/controller.rb +21 -0
  152. data/lib/aldous/controller_action.rb +63 -0
  153. data/lib/aldous/dummy_error_reporter.rb +9 -0
  154. data/lib/aldous/dummy_logger.rb +8 -0
  155. data/lib/aldous/errors/user_error.rb +6 -0
  156. data/lib/aldous/logging_wrapper.rb +16 -0
  157. data/lib/aldous/params.rb +34 -0
  158. data/lib/aldous/respondable/base.rb +32 -0
  159. data/lib/aldous/respondable/headable.rb +30 -0
  160. data/lib/aldous/respondable/redirectable.rb +38 -0
  161. data/lib/aldous/respondable/renderable.rb +50 -0
  162. data/lib/aldous/respondable/request_http_basic_authentication.rb +23 -0
  163. data/lib/aldous/respondable/send_data.rb +36 -0
  164. data/lib/aldous/respondable/shared/flash.rb +24 -0
  165. data/lib/aldous/service/result/base/predicate_methods_for_inheritance.rb +44 -0
  166. data/lib/aldous/service/result/base.rb +13 -0
  167. data/lib/aldous/service/result/failure.rb +11 -0
  168. data/lib/aldous/service/result/success.rb +11 -0
  169. data/lib/aldous/service/wrapper.rb +48 -0
  170. data/lib/aldous/service.rb +34 -0
  171. data/lib/aldous/simple_dto.rb +47 -0
  172. data/lib/aldous/stdout_logger.rb +9 -0
  173. data/lib/aldous/version.rb +3 -0
  174. data/lib/aldous/view/blank/atom_view.rb +12 -0
  175. data/lib/aldous/view/blank/html_view.rb +16 -0
  176. data/lib/aldous/view/blank/json_view.rb +16 -0
  177. data/lib/aldous.rb +40 -0
  178. data/spec/aldous/build_respondable_service_spec.rb +48 -0
  179. data/spec/aldous/configuration_spec.rb +15 -0
  180. data/spec/aldous/controller/action/precondition/wrapper_spec.rb +48 -0
  181. data/spec/aldous/controller/action/precondition_spec.rb +81 -0
  182. data/spec/aldous/controller/action/result_execution_service_spec.rb +43 -0
  183. data/spec/aldous/controller/action/wrapper_spec.rb +46 -0
  184. data/spec/aldous/controller/action_execution_service_spec.rb +79 -0
  185. data/spec/aldous/controller/preconditions_execution_service_spec.rb +45 -0
  186. data/spec/aldous/controller_action_spec.rb +97 -0
  187. data/spec/aldous/controller_spec.rb +25 -0
  188. data/spec/aldous/dummy_error_reporter_spec.rb +10 -0
  189. data/spec/aldous/dummy_logger_spec.rb +7 -0
  190. data/spec/aldous/logging_wrapper_spec.rb +55 -0
  191. data/spec/aldous/params_spec.rb +39 -0
  192. data/spec/aldous/respondable/base_spec.rb +11 -0
  193. data/spec/aldous/respondable/headable/head_action_spec.rb +17 -0
  194. data/spec/aldous/respondable/headable_spec.rb +20 -0
  195. data/spec/aldous/respondable/redirectable/redirect_action_spec.rb +34 -0
  196. data/spec/aldous/respondable/redirectable_spec.rb +26 -0
  197. data/spec/aldous/respondable/renderable/render_action_spec.rb +34 -0
  198. data/spec/aldous/respondable/renderable_spec.rb +46 -0
  199. data/spec/aldous/respondable/request_http_basic_authentication_spec.rb +0 -0
  200. data/spec/aldous/respondable/send_data/send_data_action_spec.rb +15 -0
  201. data/spec/aldous/respondable/send_data_spec.rb +30 -0
  202. data/spec/aldous/respondable/shared/flash_spec.rb +30 -0
  203. data/spec/aldous/service/result/failure_spec.rb +11 -0
  204. data/spec/aldous/service/result/success_spec.rb +11 -0
  205. data/spec/aldous/service/wrapper_spec.rb +110 -0
  206. data/spec/aldous/service_spec.rb +101 -0
  207. data/spec/aldous/simple_dto_spec.rb +40 -0
  208. data/spec/aldous/view/blank/atom_view_spec.rb +15 -0
  209. data/spec/aldous/view/blank/html_view_spec.rb +15 -0
  210. data/spec/aldous/view/blank/json_view_spec.rb +15 -0
  211. data/spec/spec_helper.rb +26 -0
  212. metadata +330 -0
@@ -0,0 +1,23 @@
1
+ require 'aldous/simple_dto'
2
+
3
+ module Aldous
4
+ class BuildRespondableService
5
+ attr_reader :view_context, :default_view_data
6
+ attr_reader :respondable_class, :status, :extra_data
7
+
8
+ def initialize(view_context:, default_view_data:, respondable_class:, status: nil, extra_data: {})
9
+ @view_context = view_context
10
+ @default_view_data = default_view_data
11
+ @respondable_class = respondable_class
12
+ @status = status
13
+ @extra_data = extra_data
14
+ end
15
+
16
+ def perform
17
+ # we don't need the status as a local
18
+ actual_extra_data = extra_data.reject{|k, v| k == :status}
19
+ view_data = SimpleDto.new(default_view_data.merge(actual_extra_data))
20
+ respondable_class.new(status, view_data, view_context)
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,18 @@
1
+ require 'aldous/dummy_error_reporter'
2
+ require 'aldous/dummy_logger'
3
+ require 'aldous/stdout_logger'
4
+
5
+ module Aldous
6
+ class Configuration
7
+ attr_accessor :error_reporter,
8
+ :logger,
9
+ :controller_methods_exposed_to_action
10
+
11
+ def initialize
12
+ @error_reporter = ::Aldous::DummyErrorReporter
13
+ @logger = ::Aldous::StdoutLogger
14
+
15
+ @controller_methods_exposed_to_action = [:params, :session, :cookies, :request, :response]
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,32 @@
1
+ require 'aldous/logging_wrapper'
2
+
3
+ module Aldous
4
+ module Controller
5
+ module Action
6
+ class Precondition
7
+ class Wrapper
8
+ attr :precondition
9
+
10
+ def initialize(precondition)
11
+ @precondition = precondition
12
+ end
13
+
14
+ def default_view_data
15
+ precondition.action.default_view_data
16
+ end
17
+
18
+ def default_error_respondable
19
+ precondition.action.default_error_respondable
20
+ end
21
+
22
+ def perform
23
+ precondition.perform
24
+ rescue => e
25
+ ::Aldous::LoggingWrapper.log(e)
26
+ precondition.build_view(default_error_respondable, errors: [e])
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,52 @@
1
+ require 'aldous/controller/action/precondition/wrapper'
2
+ require 'aldous/build_respondable_service'
3
+
4
+ module Aldous
5
+ module Controller
6
+ module Action
7
+ class Precondition
8
+ include Aldous
9
+
10
+ class << self
11
+ def build(action)
12
+ Aldous::Controller::Action::Precondition::Wrapper.new(new(action))
13
+ end
14
+
15
+ def perform(action)
16
+ build(action).perform
17
+ end
18
+
19
+ def inherited(klass)
20
+ ::Aldous.configuration.controller_methods_exposed_to_action.each do |method_name|
21
+ unless klass.method_defined?(method_name)
22
+ define_method method_name do
23
+ action.controller.send(method_name)
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+
30
+ attr_reader :action
31
+
32
+ def initialize(action)
33
+ @action = action
34
+ end
35
+
36
+ def perform
37
+ raise NotImplementedError.new("#{self.class.name} must implement method #perform")
38
+ end
39
+
40
+ def build_view(respondable_class, extra_data = {})
41
+ ::Aldous::BuildRespondableService.new(
42
+ view_context: action.controller.view_context,
43
+ default_view_data: action.default_view_data,
44
+ respondable_class: respondable_class,
45
+ status: extra_data[:status],
46
+ extra_data: extra_data
47
+ ).perform
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,27 @@
1
+ require 'aldous/logging_wrapper'
2
+
3
+ module Aldous
4
+ module Controller
5
+ module Action
6
+ class ResultExecutionService
7
+ class << self
8
+ def perform(controller, respondable, default_view_data)
9
+ self.new(controller, respondable, default_view_data).perform
10
+ end
11
+ end
12
+
13
+ attr_reader :controller, :respondable, :default_view_data
14
+
15
+ def initialize(controller, respondable, default_view_data)
16
+ @controller = controller
17
+ @respondable = respondable
18
+ @default_view_data = default_view_data
19
+ end
20
+
21
+ def perform
22
+ respondable.action(controller).execute
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,34 @@
1
+ require 'aldous/logging_wrapper'
2
+
3
+ module Aldous
4
+ module Controller
5
+ module Action
6
+ class Wrapper
7
+ attr :controller_action
8
+
9
+ def initialize(controller_action)
10
+ @controller_action = controller_action
11
+ end
12
+
13
+ def preconditions
14
+ controller_action.preconditions
15
+ end
16
+
17
+ def default_view_data
18
+ controller_action.default_view_data
19
+ end
20
+
21
+ def default_error_respondable
22
+ controller_action.default_error_respondable
23
+ end
24
+
25
+ def perform
26
+ controller_action.perform
27
+ rescue => e
28
+ ::Aldous::LoggingWrapper.log(e)
29
+ controller_action.build_view(default_error_respondable, errors: [e])
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,42 @@
1
+ require 'aldous/controller/preconditions_execution_service'
2
+ require 'aldous/controller/action/result_execution_service'
3
+ require 'aldous/logging_wrapper'
4
+
5
+ module Aldous
6
+ module Controller
7
+ class ActionExecutionService
8
+ class << self
9
+ def perform(controller, controller_action_class)
10
+ self.new(controller, controller_action_class).perform
11
+ end
12
+ end
13
+
14
+ attr_reader :controller, :controller_action_class
15
+
16
+ def initialize(controller, controller_action_class)
17
+ @controller = controller
18
+ @controller_action_class = controller_action_class
19
+ end
20
+
21
+ def perform
22
+ action = controller_action_class.build(controller)
23
+
24
+ precondition, precondition_result = PreconditionsExecutionService.new(action, controller).perform
25
+
26
+ action_result = nil
27
+ if precondition_result
28
+ action = precondition
29
+ action_result = precondition_result
30
+ else
31
+ action_result = action.perform
32
+ end
33
+
34
+ Action::ResultExecutionService.perform(controller, action_result, action.default_view_data)
35
+ rescue => e
36
+ LoggingWrapper.log(e)
37
+ controller.head :internal_server_error
38
+ end
39
+ end
40
+ end
41
+ end
42
+
@@ -0,0 +1,32 @@
1
+ require 'aldous/respondable/base'
2
+ require 'aldous/controller/action/precondition/wrapper'
3
+
4
+ module Aldous
5
+ module Controller
6
+ class PreconditionsExecutionService
7
+ attr_reader :action, :controller
8
+
9
+ def initialize(action, controller)
10
+ @action = action
11
+ @controller = controller
12
+ end
13
+
14
+ def perform
15
+ if action.respond_to?(:preconditions) && !action.preconditions.empty?
16
+ action.preconditions.each do |precondition_class|
17
+ # action here is actually an action wrapper hence the
18
+ # action.controller_action below
19
+ precondition = precondition_class.build(action.controller_action)
20
+ precondition_result = precondition.perform
21
+
22
+ if precondition_result.kind_of?(::Aldous::Respondable::Base)
23
+ return [precondition, precondition_result]
24
+ end
25
+ end
26
+ end
27
+ [nil, nil]
28
+ end
29
+ end
30
+ end
31
+ end
32
+
@@ -0,0 +1,21 @@
1
+ require 'aldous/controller/action_execution_service'
2
+
3
+ module Aldous
4
+ module Controller
5
+ include Aldous
6
+
7
+ def self.included(base)
8
+ base.extend ClassMethods
9
+ end
10
+
11
+ module ClassMethods
12
+ def controller_actions(*actions)
13
+ actions.each do |action_name|
14
+ define_method action_name do
15
+ ::Aldous::Controller::ActionExecutionService.perform(self, self.class.const_get(action_name.to_s.classify))
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,63 @@
1
+ require 'aldous/controller/action/wrapper'
2
+ require 'aldous/view/blank/html_view'
3
+ require 'aldous/simple_dto'
4
+ require 'aldous/build_respondable_service'
5
+
6
+ module Aldous
7
+ class ControllerAction
8
+ include Aldous
9
+
10
+ class << self
11
+ def build(controller)
12
+ Aldous::Controller::Action::Wrapper.new(new(controller))
13
+ end
14
+
15
+ def perform(controller)
16
+ build(controller).perform
17
+ end
18
+
19
+ def inherited(klass)
20
+ # expose methods from controller to the service, according to configuration
21
+ ::Aldous.configuration.controller_methods_exposed_to_action.each do |method_name|
22
+ unless klass.method_defined?(method_name)
23
+ define_method method_name do
24
+ controller.send(method_name)
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+
31
+ attr_reader :controller
32
+
33
+ def initialize(controller)
34
+ @controller = controller
35
+ end
36
+
37
+ def perform
38
+ raise NotImplementedError.new("#{self.class.name} must implement method #perform")
39
+ end
40
+
41
+ def default_view_data
42
+ {}
43
+ end
44
+
45
+ def preconditions
46
+ []
47
+ end
48
+
49
+ def default_error_respondable
50
+ ::Aldous::View::Blank::HtmlView
51
+ end
52
+
53
+ def build_view(respondable_class, extra_data = {})
54
+ ::Aldous::BuildRespondableService.new(
55
+ view_context: controller.view_context,
56
+ default_view_data: default_view_data,
57
+ respondable_class: respondable_class,
58
+ status: extra_data[:status],
59
+ extra_data: extra_data
60
+ ).perform
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,9 @@
1
+ module Aldous
2
+ class DummyErrorReporter
3
+ class << self
4
+ def report(e, data = {})
5
+ nil
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,8 @@
1
+ module Aldous
2
+ class DummyLogger
3
+ class << self
4
+ def info(message)
5
+ end
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,6 @@
1
+ module Aldous
2
+ module Errors
3
+ class UserError < StandardError
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,16 @@
1
+ module Aldous
2
+ class LoggingWrapper
3
+ class << self
4
+ def log(error)
5
+ if error.kind_of?(String)
6
+ ::Aldous.config.error_reporter.report(error)
7
+ ::Aldous.config.logger.info(error)
8
+ else # it's an error object
9
+ ::Aldous.config.error_reporter.report(error)
10
+ ::Aldous.config.logger.info(error.message)
11
+ ::Aldous.config.logger.info(error.backtrace.join("\n"))
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,34 @@
1
+ require 'aldous/logging_wrapper'
2
+
3
+ module Aldous
4
+ class Params
5
+ include Aldous
6
+
7
+ class << self
8
+ def build(*args)
9
+ new(*args)
10
+ end
11
+ end
12
+
13
+ attr_reader :params
14
+
15
+ def initialize(params)
16
+ @params = params
17
+ end
18
+
19
+ def fetch
20
+ permitted_params
21
+ rescue => e
22
+ Aldous::LoggingWrapper.log(e)
23
+ nil
24
+ end
25
+
26
+ def permitted_params
27
+ {}
28
+ end
29
+
30
+ def error_message
31
+ 'Missing param'
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,32 @@
1
+ require 'aldous/simple_dto'
2
+
3
+ module Aldous
4
+ module Respondable
5
+ class Base
6
+ attr_reader :view_data, :view_context
7
+
8
+ def initialize(status, view_data, view_context)
9
+ @status = status
10
+ @view_data = view_data
11
+ @view_context = view_context
12
+ end
13
+
14
+ def action(controller)
15
+ raise Errors::UserError.new("Respondables must define an 'action' method")
16
+ end
17
+
18
+ def status
19
+ @status || default_status
20
+ end
21
+
22
+ def default_status
23
+ :ok
24
+ end
25
+
26
+ def build_view(klass, extra_data = {})
27
+ dto = Aldous::SimpleDto.new(view_data._data.merge(extra_data))
28
+ klass.new(status, dto, view_context)
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,30 @@
1
+ require 'aldous/respondable/base'
2
+
3
+ module Aldous
4
+ module Respondable
5
+ class Headable < Base
6
+ def action(controller)
7
+ HeadAction.new(controller, status)
8
+ end
9
+
10
+ def default_status
11
+ :ok
12
+ end
13
+
14
+ private
15
+
16
+ class HeadAction
17
+ attr_reader :controller, :status
18
+
19
+ def initialize(controller, status)
20
+ @controller = controller
21
+ @status = status
22
+ end
23
+
24
+ def execute
25
+ controller.head status
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,38 @@
1
+ require 'aldous/respondable/base'
2
+ require 'aldous/respondable/shared/flash'
3
+
4
+ module Aldous
5
+ module Respondable
6
+ class Redirectable < Base
7
+ def action(controller)
8
+ RedirectAction.new(location, controller, view_data, status)
9
+ end
10
+
11
+ def location
12
+ raise Errors::UserError.new("Redirectable objects must define a 'location' method")
13
+ end
14
+
15
+ def default_status
16
+ :found
17
+ end
18
+
19
+ private
20
+
21
+ class RedirectAction
22
+ attr_reader :controller, :view_data, :location, :status
23
+
24
+ def initialize(location, controller, view_data, status)
25
+ @location = location
26
+ @controller = controller
27
+ @view_data = view_data
28
+ @status = status
29
+ end
30
+
31
+ def execute
32
+ Shared::Flash.new(view_data, controller.flash).set_error
33
+ controller.redirect_to location, {status: status}
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,50 @@
1
+ require 'aldous/respondable/base'
2
+ require 'aldous/respondable/shared/flash'
3
+
4
+ module Aldous
5
+ module Respondable
6
+ class Renderable < Base
7
+ def action(controller)
8
+ RenderAction.new(template, status, controller, view_data)
9
+ end
10
+
11
+ def default_status
12
+ :ok
13
+ end
14
+
15
+ def default_template_locals
16
+ {}
17
+ end
18
+
19
+ def template_data
20
+ {}
21
+ end
22
+
23
+ def template(extra_locals = {})
24
+ template_locals = template_data[:locals] || {}
25
+ locals = default_template_locals.merge(template_locals)
26
+ locals = locals.merge(extra_locals || {})
27
+ template_hash = template_data.merge(locals: locals)
28
+ template_hash
29
+ end
30
+
31
+ private
32
+
33
+ class RenderAction
34
+ attr_reader :template, :controller, :view_data, :status
35
+
36
+ def initialize(template, status, controller, view_data)
37
+ @status = status
38
+ @template = template
39
+ @controller = controller
40
+ @view_data = view_data
41
+ end
42
+
43
+ def execute
44
+ Shared::Flash.new(view_data, controller.flash.now).set_error
45
+ controller.render template.merge(status: status)
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,23 @@
1
+ module Aldous
2
+ module Respondable
3
+ class RequestHttpBasicAuthentication < Base
4
+ def action(controller)
5
+ RequestHttpBasicAuthenticationAction.new(controller)
6
+ end
7
+
8
+ private
9
+
10
+ class RequestHttpBasicAuthenticationAction
11
+ attr_reader :controller
12
+
13
+ def initialize(controller)
14
+ @controller = controller
15
+ end
16
+
17
+ def execute
18
+ controller.request_http_basic_authentication
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,36 @@
1
+ require 'aldous/respondable/base'
2
+
3
+ module Aldous
4
+ module Respondable
5
+ class SendData < Base
6
+ def action(controller)
7
+ SendDataAction.new(data, options, controller, view_data)
8
+ end
9
+
10
+ def data
11
+ raise Errors::UserError.new("SendData objects must define a 'data' method")
12
+ end
13
+
14
+ def options
15
+ raise Errors::UserError.new("SendData objects must define an 'options' method")
16
+ end
17
+
18
+ private
19
+
20
+ class SendDataAction
21
+ attr_reader :controller, :view_data, :data, :options
22
+
23
+ def initialize(data, options, controller, view_data)
24
+ @controller = controller
25
+ @view_data = view_data
26
+ @data = data
27
+ @options = options
28
+ end
29
+
30
+ def execute
31
+ controller.send_data data, options
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,24 @@
1
+ module Aldous
2
+ module Respondable
3
+ module Shared
4
+ class Flash
5
+ attr_reader :result, :flash_container
6
+
7
+ def initialize(result, flash_container)
8
+ @result = result
9
+ @flash_container = flash_container
10
+ end
11
+
12
+ def set_error
13
+ flash_container[:error] = error if error
14
+ end
15
+
16
+ private
17
+
18
+ def error
19
+ result.errors.first
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end