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,44 @@
1
+ require 'aldous/service/result/base'
2
+
3
+ module Aldous
4
+ class Service
5
+ module Result
6
+ class Base < ::Aldous::SimpleDto
7
+ module PredicateMethodsForInheritance
8
+ # For every child class, create a predicate method on the base named
9
+ # after the child that returns false and override the same predicate
10
+ # method on the child to return true. So if the child class is called
11
+ # Failure then we'll have a predicate method called failure? which will
12
+ # return false on the base class and true on the actual child class.
13
+ def inherited(child)
14
+ return if child.name == nil # unnamed class
15
+ child_class_name_as_predicate = "#{underscore(child.name.split("::").last)}?"
16
+
17
+ add_predicate_method_to_class(Aldous::Service::Result::Base, child_class_name_as_predicate, false)
18
+ add_predicate_method_to_class(child, child_class_name_as_predicate, true)
19
+ end
20
+
21
+ private
22
+
23
+ def add_predicate_method_to_class(klass, method_name, method_value)
24
+ unless klass.instance_methods(false).include?(method_name)
25
+ klass.class_eval do
26
+ define_method method_name do
27
+ method_value
28
+ end
29
+ end
30
+ end
31
+ end
32
+
33
+ def underscore(string)
34
+ string.gsub(/::/, '/').
35
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
36
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
37
+ tr("-", "_").
38
+ downcase
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,13 @@
1
+ require 'aldous/simple_dto'
2
+ require 'aldous/service/result/base/predicate_methods_for_inheritance'
3
+
4
+ module Aldous
5
+ class Service
6
+ module Result
7
+ class Base < ::Aldous::SimpleDto
8
+ extend PredicateMethodsForInheritance
9
+ end
10
+ end
11
+ end
12
+ end
13
+
@@ -0,0 +1,11 @@
1
+ require 'aldous/service/result/base'
2
+
3
+ module Aldous
4
+ class Service
5
+ module Result
6
+ class Failure < ::Aldous::Service::Result::Base
7
+ end
8
+ end
9
+ end
10
+ end
11
+
@@ -0,0 +1,11 @@
1
+ require 'aldous/service/result/base'
2
+
3
+ module Aldous
4
+ class Service
5
+ module Result
6
+ class Success < ::Aldous::Service::Result::Base
7
+ end
8
+ end
9
+ end
10
+ end
11
+
@@ -0,0 +1,48 @@
1
+ require 'aldous/logging_wrapper'
2
+ require 'aldous/service/result/base'
3
+ require 'aldous/service/result/failure'
4
+
5
+ module Aldous
6
+ class Service
7
+ class Wrapper
8
+ attr :service
9
+
10
+ def initialize(service)
11
+ @service = service
12
+ end
13
+
14
+ def raisable_error
15
+ service.raisable_error
16
+ end
17
+
18
+ def default_result_data
19
+ service.default_result_data
20
+ end
21
+
22
+ def perform!
23
+ result = service.perform
24
+
25
+ unless result.kind_of?(::Aldous::Service::Result::Base)
26
+ raise "Return value of #perform must be a type of #{::Aldous::Service::Result::Base}"
27
+ end
28
+
29
+ build_result_with_default_options(result)
30
+ rescue => e
31
+ raise raisable_error.new(e.message)
32
+ end
33
+
34
+ def perform
35
+ perform!
36
+ rescue => e
37
+ Aldous::LoggingWrapper.log(e.cause || e)
38
+ Aldous::Service::Result::Failure.new(default_result_data.merge(errors: [e]))
39
+ end
40
+
41
+ private
42
+
43
+ def build_result_with_default_options(result)
44
+ result.class.new(default_result_data.merge(result._data))
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,34 @@
1
+ require 'aldous/service/wrapper'
2
+ require 'aldous/errors/user_error'
3
+
4
+ module Aldous
5
+ class Service
6
+ include Aldous
7
+
8
+ class << self
9
+ def build(*args)
10
+ Aldous::Service::Wrapper.new(new(*args))
11
+ end
12
+
13
+ def perform(*args)
14
+ build(*args).perform
15
+ end
16
+
17
+ def perform!(*args)
18
+ build(*args).perform!
19
+ end
20
+ end
21
+
22
+ def perform
23
+ raise NotImplementedError.new("#{self.class.name} must implement method #perform")
24
+ end
25
+
26
+ def raisable_error
27
+ Aldous::Errors::UserError
28
+ end
29
+
30
+ def default_result_data
31
+ {}
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,47 @@
1
+ module Aldous
2
+ class SimpleDto
3
+ attr_reader :_data
4
+
5
+ def initialize(data = {})
6
+ @_data = data
7
+
8
+ handle_errors_and_messages
9
+ define_accessors
10
+ end
11
+
12
+ def errors
13
+ @errors ||= []
14
+ end
15
+
16
+ def messages
17
+ @messages ||= []
18
+ end
19
+
20
+ private
21
+
22
+ def handle_errors_and_messages
23
+ # ensure that any errors or messages end up in the appropriate array
24
+ _data.each_pair do |key, value|
25
+ if key.to_s == 'errors'
26
+ @errors = [value].flatten.compact
27
+ elsif key.to_s == 'messages'
28
+ @messages = [value].flatten.compact
29
+ end
30
+ end
31
+ end
32
+
33
+ def define_accessors
34
+ self.class.class_exec(_data) do |_data|
35
+ _data.each_key do |key|
36
+ next if key.to_s == 'errors' || key.to_s == 'messages'
37
+ # do nothing if method is already defined
38
+ next if instance_methods(false).include?(key.to_sym)
39
+
40
+ define_method key do
41
+ @_data[key]
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,9 @@
1
+ module Aldous
2
+ class StdoutLogger
3
+ class << self
4
+ def info(message)
5
+ $stdout.puts message
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module Aldous
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,12 @@
1
+ require 'aldous/respondable/renderable'
2
+
3
+ module Aldous
4
+ module View
5
+ module Blank
6
+ class AtomView < Respondable::Renderable
7
+ def template
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,16 @@
1
+ require 'aldous/respondable/renderable'
2
+
3
+ module Aldous
4
+ module View
5
+ module Blank
6
+ class HtmlView < Respondable::Renderable
7
+ def template
8
+ {
9
+ html: "", layout: true
10
+ }
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
16
+
@@ -0,0 +1,16 @@
1
+ require 'aldous/respondable/renderable'
2
+
3
+ module Aldous
4
+ module View
5
+ module Blank
6
+ class JsonView < Respondable::Renderable
7
+ def template
8
+ {
9
+ json: {}
10
+ }
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
16
+
data/lib/aldous.rb ADDED
@@ -0,0 +1,40 @@
1
+ require 'aldous/version'
2
+ require 'aldous/configuration'
3
+
4
+ require 'aldous/errors/user_error'
5
+
6
+ require 'aldous/respondable/base'
7
+ require 'aldous/respondable/headable'
8
+ require 'aldous/respondable/redirectable'
9
+ require 'aldous/respondable/renderable'
10
+ require 'aldous/respondable/send_data'
11
+ require 'aldous/respondable/request_http_basic_authentication'
12
+
13
+ require 'aldous/service/result/failure'
14
+ require 'aldous/service/result/success'
15
+
16
+ require 'aldous/service'
17
+
18
+ require 'aldous/controller'
19
+ require 'aldous/controller_action'
20
+ require 'aldous/controller/action/precondition'
21
+ require 'aldous/params'
22
+
23
+ require 'aldous/view/blank/atom_view'
24
+ require 'aldous/view/blank/json_view'
25
+ require 'aldous/view/blank/html_view'
26
+
27
+ module Aldous
28
+ class << self
29
+ def configuration(&block)
30
+ @configuration ||= Aldous::Configuration.new
31
+ if block_given?
32
+ block.call(@configuration)
33
+ else
34
+ @configuration
35
+ end
36
+ end
37
+ alias :config :configuration # can use either config or configuration
38
+ end
39
+ end
40
+
@@ -0,0 +1,48 @@
1
+ RSpec.describe Aldous::BuildRespondableService do
2
+ let(:service) do
3
+ described_class.new(
4
+ view_context: view_context,
5
+ default_view_data: default_view_data,
6
+ respondable_class: respondable_class,
7
+ status: status,
8
+ extra_data: extra_data
9
+ )
10
+ end
11
+
12
+ let(:view_context) { double "view_context" }
13
+ let(:default_view_data) { {hello: 1} }
14
+ let(:respondable_class) { double "respondable_class"}
15
+ let(:status) { :foo }
16
+ let(:extra_data) { {world: 2} }
17
+
18
+ let(:simple_dto) {instance_double(Aldous::SimpleDto)}
19
+ let(:respondable_instance) {double "respondable_instance"}
20
+
21
+ describe "#perform" do
22
+ let(:view_data) { {hello: 1, world: 2} }
23
+
24
+ before do
25
+ allow(Aldous::SimpleDto).to receive(:new).and_return(simple_dto)
26
+ allow(respondable_class).to receive(:new).and_return(respondable_instance)
27
+ end
28
+
29
+ context "when status is part of the extra_data" do
30
+ let(:extra_data) { {world: 2, status: status} }
31
+
32
+ it "creates a dto with the correct data" do
33
+ expect(Aldous::SimpleDto).to receive(:new).with(view_data).and_return(simple_dto)
34
+ service.perform
35
+ end
36
+ end
37
+
38
+ it "creates a dto with the correct data" do
39
+ expect(Aldous::SimpleDto).to receive(:new).with(view_data).and_return(simple_dto)
40
+ service.perform
41
+ end
42
+
43
+ it "returns a respondable instance" do
44
+ expect(respondable_class).to receive(:new).with(status, simple_dto, view_context)
45
+ service.perform
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,15 @@
1
+ RSpec.describe Aldous::Configuration do
2
+ subject(:config) {described_class.new}
3
+
4
+ describe "#error_reporter" do
5
+ it "is configured to use the dummy error reporter by default" do
6
+ expect(config.error_reporter).to eq ::Aldous::DummyErrorReporter
7
+ end
8
+ end
9
+
10
+ describe "#logger" do
11
+ it "is configured to use the stdout logger by default" do
12
+ expect(config.logger).to eq ::Aldous::StdoutLogger
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,48 @@
1
+ RSpec.describe Aldous::Controller::Action::Precondition::Wrapper do
2
+ let(:wrapper) { described_class.new precondition }
3
+
4
+ let(:precondition) {double 'precondition',
5
+ action: controller_action,
6
+ perform: nil,
7
+ build_view: nil }
8
+
9
+ let(:controller_action) { double 'controller action',
10
+ default_view_data: default_view_data,
11
+ default_error_respondable: default_error_respondable,
12
+ perform: nil,
13
+ build_view: nil }
14
+
15
+ let(:default_view_data) { {default_view_data: true} }
16
+ let(:default_error_respondable) {double 'default_error_respondable'}
17
+
18
+ before do
19
+ allow(Aldous::LoggingWrapper).to receive(:log)
20
+ end
21
+
22
+ describe '#perform' do
23
+ subject(:perform) { wrapper.perform }
24
+
25
+ it "calls perform on the precondition" do
26
+ expect(precondition).to receive(:perform)
27
+ perform
28
+ end
29
+
30
+ context 'when precondition throws an exception' do
31
+ let(:e) { StandardError.new 'message' }
32
+
33
+ before do
34
+ allow(precondition).to receive(:perform).and_raise(e)
35
+ end
36
+
37
+ it "builds a default error view with errors" do
38
+ expect(precondition).to receive(:build_view).with(default_error_respondable, errors: [e])
39
+ perform
40
+ end
41
+
42
+ it 'reports the error' do
43
+ expect(Aldous::LoggingWrapper).to receive(:log).with(e)
44
+ perform
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,81 @@
1
+ RSpec.describe Aldous::Controller::Action::Precondition do
2
+ before do
3
+ class ExamplePrecondition < Aldous::Controller::Action::Precondition
4
+ end
5
+ end
6
+
7
+ after do
8
+ Object.send :remove_const, 'ExamplePrecondition'
9
+ end
10
+
11
+ let(:precondition) {ExamplePrecondition.new(action)}
12
+
13
+ let(:action) {double 'action', controller: controller, default_view_data: default_view_data}
14
+ let(:controller) {double 'controller', view_context: view_context}
15
+ let(:view_context) {double "view_context"}
16
+ let(:default_view_data) {double "default_view_data"}
17
+
18
+ describe "::build" do
19
+ before do
20
+ allow(ExamplePrecondition).to receive(:new).and_return(precondition)
21
+ allow(Aldous::Controller::Action::Precondition::Wrapper).to receive(:new)
22
+ end
23
+
24
+ it "wraps a controller action instance" do
25
+ expect(Aldous::Controller::Action::Precondition::Wrapper).to receive(:new).with(precondition)
26
+ ExamplePrecondition.build(action)
27
+ end
28
+ end
29
+
30
+ describe "::perform" do
31
+ let(:wrapper) {double "wrapper", perform: nil}
32
+
33
+ before do
34
+ allow(ExamplePrecondition).to receive(:new).and_return(action)
35
+ allow(Aldous::Controller::Action::Precondition::Wrapper).to receive(:new).and_return(wrapper)
36
+ end
37
+
38
+ it "calls perform on the wrapper" do
39
+ expect(wrapper).to receive(:perform)
40
+ ExamplePrecondition.perform(action)
41
+ end
42
+ end
43
+
44
+ describe "::inherited" do
45
+ context "a precondition instance" do
46
+ Aldous.configuration.controller_methods_exposed_to_action.each do |method_name|
47
+ it "responds to #{method_name}" do
48
+ expect(precondition).to respond_to method_name
49
+ end
50
+ end
51
+ end
52
+ end
53
+
54
+ describe "#perform" do
55
+ it "raises an error since it should be overridden" do
56
+ expect{precondition.perform}.to raise_error
57
+ end
58
+ end
59
+
60
+ describe "#build_view" do
61
+ before do
62
+ allow(Aldous::BuildRespondableService).to receive(:new).with(
63
+ view_context: view_context,
64
+ default_view_data: default_view_data,
65
+ respondable_class: respondable_class,
66
+ status: nil,
67
+ extra_data: extra_data
68
+ ).and_return(build_respondable_service)
69
+ end
70
+
71
+ let(:respondable_class) {double "respondable_class"}
72
+ let(:extra_data) { {hello: 1} }
73
+
74
+ let(:build_respondable_service) {double "build_respondable_service", perform: nil}
75
+
76
+ it "executes the BuildRespondableService" do
77
+ expect(build_respondable_service).to receive(:perform)
78
+ precondition.build_view(respondable_class, extra_data)
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,43 @@
1
+ RSpec.describe Aldous::Controller::Action::ResultExecutionService do
2
+ let(:result_execution_service) {described_class.new(controller, respondable, default_view_data)}
3
+
4
+ let(:controller) {double "controller"}
5
+ let(:respondable) {double "respondable", action: action}
6
+ let(:default_view_data) {'hello'}
7
+
8
+ let(:action) {double 'action', execute: nil}
9
+
10
+ describe "::perform" do
11
+ subject(:perform) {described_class.perform(controller, respondable, default_view_data)}
12
+
13
+ let(:result_execution_service) {double "result_execution_service", perform: nil}
14
+
15
+ before do
16
+ allow(described_class).to receive(:new).and_return(result_execution_service)
17
+ end
18
+
19
+ it "instantiates the result execution service" do
20
+ expect(described_class).to receive(:new).with(controller, respondable, default_view_data)
21
+ perform
22
+ end
23
+
24
+ it "performs the result execution service" do
25
+ expect(result_execution_service).to receive(:perform)
26
+ perform
27
+ end
28
+ end
29
+
30
+ describe "#perform" do
31
+ subject(:perform) {result_execution_service.perform}
32
+
33
+ it "fetches the action from the respondable" do
34
+ expect(respondable).to receive(:action).with(controller)
35
+ perform
36
+ end
37
+
38
+ it "executes the respondable action" do
39
+ expect(action).to receive(:execute)
40
+ perform
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,46 @@
1
+ RSpec.describe Aldous::Controller::Action::Wrapper do
2
+ let(:wrapper) { described_class.new controller_action }
3
+
4
+ let(:controller_action) { double 'controller action',
5
+ default_view_data: default_view_data,
6
+ preconditions: preconditions,
7
+ default_error_respondable: default_error_respondable,
8
+ perform: nil,
9
+ build_view: nil }
10
+
11
+ let(:default_view_data) { {default_view_data: true} }
12
+ let(:preconditions) { double 'preconditions' }
13
+ let(:default_error_respondable) {double 'default_error_respondable'}
14
+
15
+ before do
16
+ allow(Aldous::LoggingWrapper).to receive(:log)
17
+ end
18
+
19
+ describe '#perform' do
20
+ subject(:perform) { wrapper.perform }
21
+
22
+ it "calls perform on the controller action" do
23
+ expect(controller_action).to receive(:perform)
24
+ perform
25
+ end
26
+
27
+ context 'when controller service throws an exception' do
28
+ let(:e) { StandardError.new 'message' }
29
+
30
+ before do
31
+ allow(controller_action).to receive(:perform).and_raise(e)
32
+ end
33
+
34
+ it "builds a default error view with errors" do
35
+ expect(controller_action).to receive(:build_view).with(default_error_respondable, errors: [e])
36
+ perform
37
+ end
38
+
39
+ it 'reports the error' do
40
+ expect(Aldous::LoggingWrapper).to receive(:log).with(e)
41
+ perform
42
+ end
43
+ end
44
+ end
45
+ end
46
+
@@ -0,0 +1,79 @@
1
+ RSpec.describe Aldous::Controller::ActionExecutionService do
2
+ let(:action_execution_service) {described_class.new(controller, controller_action_class)}
3
+
4
+ let(:controller) {double "controller", head: nil}
5
+ let(:controller_action_class) {double "controller_action_class", build: action}
6
+
7
+ let(:action) {double 'action', perform: action_result, default_view_data: default_view_data}
8
+ let(:action_result) {'foobar'}
9
+ let(:default_view_data) {'hello'}
10
+
11
+ describe "::perform" do
12
+ subject(:perform) {described_class.perform(controller, controller_action_class)}
13
+
14
+ let(:action_execution_service) {double "action_execution_service", perform: nil}
15
+
16
+ before do
17
+ allow(described_class).to receive(:new).and_return(action_execution_service)
18
+ end
19
+
20
+ it "instantiates the action execution service" do
21
+ expect(described_class).to receive(:new).with(controller, controller_action_class)
22
+ perform
23
+ end
24
+
25
+ it "performs the action execution service" do
26
+ expect(action_execution_service).to receive(:perform)
27
+ perform
28
+ end
29
+ end
30
+
31
+ describe "#perform" do
32
+ subject(:perform) {action_execution_service.perform}
33
+
34
+ let(:preconditions_execution_service) {double "preconditions_execution_service", perform: preconditions_result}
35
+ let(:preconditions_result) {[nil, nil]}
36
+
37
+ before do
38
+ allow(Aldous::Controller::PreconditionsExecutionService).to receive(:new).and_return(preconditions_execution_service)
39
+ allow(Aldous::Controller::Action::ResultExecutionService).to receive(:perform)
40
+ end
41
+
42
+ context "when preconditions did not halt execution" do
43
+ it "executes the result with action_result" do
44
+ expect(Aldous::Controller::Action::ResultExecutionService).to receive(:perform).with(controller, action_result, default_view_data)
45
+ perform
46
+ end
47
+ end
48
+
49
+ context "when preconditions did halt execution" do
50
+ let(:preconditions_result) {[precondition, precondition_result]}
51
+
52
+ let(:precondition) {double 'precondition', perform: precondition_result, default_view_data: default_view_data}
53
+ let(:precondition_result) {'preconditionfoobar'}
54
+ let(:default_view_data) {'precondtionhello'}
55
+
56
+ it "executes the result with precondition_result" do
57
+ expect(Aldous::Controller::Action::ResultExecutionService).to receive(:perform).with(controller, precondition_result, default_view_data)
58
+ perform
59
+ end
60
+ end
61
+
62
+ context "when error occurs" do
63
+ before do
64
+ allow(Aldous::LoggingWrapper).to receive(:log)
65
+ allow(Aldous::Controller::PreconditionsExecutionService).to receive(:new).and_raise
66
+ end
67
+
68
+ it "logs the error" do
69
+ expect(Aldous::LoggingWrapper).to receive(:log)
70
+ perform
71
+ end
72
+
73
+ it "returns a 500 via controller" do
74
+ expect(controller).to receive(:head).with(:internal_server_error)
75
+ perform
76
+ end
77
+ end
78
+ end
79
+ end