aldous 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cbd9c9ba816e2ef37e0528487e6f203a2f30c821
4
- data.tar.gz: 8b1ef09ccbd2e525f814255bcbe61425056edae2
3
+ metadata.gz: e396af4df13c405822eb290056947943e502c97b
4
+ data.tar.gz: 62e3f8739b0a35e9f0c4911347f088763e003944
5
5
  SHA512:
6
- metadata.gz: 5ceae3ac65b7eb859192b9311c7ad8502426f59c25d5a2dfdabf0d7029d992c1525c785b94f61cc100f4492b4ccc1dee217b5052ce653a40ae34d8f8fecb83ca
7
- data.tar.gz: 759d8191ad6b4c4d0e663f5dd268040924481419f93dbaf1d40dae727fbb3f77f13fa39ad8fdab32376670c55738b56f1bed964ca3e7fcce3d225f83995b7958
6
+ metadata.gz: 8ab212817cec483cff3f3ae186eb74ca4f8b3dc499634e5c5ce8af68b92bf17a3fba96e3a043819527100d5c4e3fcdb472cf9e55621a933bf1dad3245e95f004
7
+ data.tar.gz: 409b37981511c069505dcce476fde2bfa8d3eb9d723c06f9f8fec34f65c32f34ef93cad3224a26040f023d69c1c615ba2ebe98466510d18660b6dd72f4eab793
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Aldous
2
2
 
3
- The basic idea behind this gem is that [Rails](https://github.com/rails/rails) is missing a few key things that make our lives especially painful when our applications start gettging bigger and more complex. With Aldous we attempt to address some of these shortcomings, namely:
3
+ The basic idea behind this gem is that [Rails](https://github.com/rails/rails) is missing a few key things that make our lives especially painful when our applications start getting bigger and more complex. With Aldous we attempt to address some of these shortcomings, namely:
4
4
 
5
5
  1) Bloated models that trample all over the single responsibility principle. God models that tend to appear despite our best intentions. Not to mention the complexity of testing objects that are so big an unwieldy.
6
6
 
@@ -113,8 +113,8 @@ or
113
113
 
114
114
  ```ruby
115
115
  hash = {}
116
- result = CreateUserService.perform!(hash)
117
116
  begin
117
+ result = CreateUserService.perform!(hash)
118
118
  if result.success?
119
119
  # do success stuff
120
120
  else #result.failure?
@@ -191,11 +191,11 @@ The first thing to do is to configure a folder for the controller actions to liv
191
191
 
192
192
  ```ruby
193
193
  config.autoload_paths += %W(
194
- #{config.root}/app/controller_action
194
+ #{config.root}/app/controller_actions
195
195
  )
196
196
 
197
197
  config.eager_load_paths += %W(
198
- #{config.root}/app/controller_action
198
+ #{config.root}/app/controller_actions
199
199
  )
200
200
  ```
201
201
 
@@ -250,7 +250,7 @@ class BaseAction < ::Aldous::ControllerAction
250
250
  [Shared::EnsureUserNotDisabledPrecondition]
251
251
  end
252
252
 
253
- def default_error_respondable
253
+ def default_error_handler(error)
254
254
  Defaults::ServerErrorView
255
255
  end
256
256
 
@@ -268,11 +268,11 @@ As you can see if inherits from `Aldous::ControllerAction`. The methods you shou
268
268
 
269
269
  - `default_view_data` - this hash of data will be available to all the aldous view objects
270
270
  - `preconditions` - this is used as a replacement for `before_actions`
271
- - `default_error_respondable` - this view will be rendered if an unhandled error gets raised in the action code
271
+ - `default_error_handler` - this view will be rendered if an unhandled error gets raised in the action code
272
272
 
273
273
  Similar to services, for actions you implement the `perform` method (might as well keep things consistent). All action classes have the same constructor signature, they take a controller object and you have access to this controller object in your action classes. Also a few methods from the controller get exposed directly to your action (params, session, cookies, request, response) for convenience. You can expose others via configuration in an initializer, or you can grab them from the controller instance you have access to.
274
274
 
275
- Controller action are automatically error free, so any error that gets raised in the perform method, automatically get caught and a view you specify in `default_error_respondable` will get rendered to handle that error.
275
+ Controller action are automatically error free, so any error that gets raised in the perform method, automatically get caught and a view you specify in `default_error_handler` will get rendered to handle that error.
276
276
 
277
277
  Lets look at a slightly bigger controller action:
278
278
 
@@ -10,7 +10,7 @@ class BaseAction < ::Aldous::ControllerAction
10
10
  [Shared::EnsureUserNotDisabledPrecondition]
11
11
  end
12
12
 
13
- def default_error_respondable
13
+ def default_error_handler(error)
14
14
  Defaults::ServerErrorView
15
15
  end
16
16
 
@@ -1,7 +1,6 @@
1
1
  class HomeController::Show < BaseAction
2
2
  def perform
3
3
  return build_view(Todos::IndexRedirect) if current_user
4
-
5
4
  build_view(Home::ShowView)
6
5
  end
7
6
  end
@@ -0,0 +1,11 @@
1
+ class Shared::NoAldousViewPrecondition < BasePrecondition
2
+ delegate :current_user, :current_ability, to: :action
3
+
4
+ def perform
5
+ controller.render(
6
+ template: 'defaults/forbidden',
7
+ status: :unprocessable_entity,
8
+ locals: {error: 'Foobar'}
9
+ )
10
+ end
11
+ end
@@ -37,6 +37,10 @@ module Aldous
37
37
  raise NotImplementedError.new("#{self.class.name} must implement method #perform")
38
38
  end
39
39
 
40
+ def controller
41
+ action.controller
42
+ end
43
+
40
44
  def build_view(respondable_class, extra_data = {})
41
45
  ::Aldous::BuildRespondableService.new(
42
46
  view_context: action.controller.view_context,
@@ -1,4 +1,5 @@
1
1
  require 'aldous/logging_wrapper'
2
+ require 'aldous/respondable/base'
2
3
 
3
4
  module Aldous
4
5
  module Controller
@@ -18,15 +19,25 @@ module Aldous
18
19
  controller_action.default_view_data
19
20
  end
20
21
 
21
- def default_error_respondable
22
- controller_action.default_error_respondable
22
+ def default_error_handler(error)
23
+ controller_action.default_error_handler(error)
24
+ end
25
+
26
+ def controller
27
+ controller_action.controller
23
28
  end
24
29
 
25
30
  def perform
26
31
  controller_action.perform
27
32
  rescue => e
28
33
  ::Aldous::LoggingWrapper.log(e)
29
- controller_action.build_view(default_error_respondable, errors: [e])
34
+
35
+ error_handler = default_error_handler(e)
36
+
37
+ if error_handler.kind_of?(Class) &&
38
+ error_handler.ancestors.include?(Aldous::Respondable::Base)
39
+ controller_action.build_view(error_handler, errors: [e])
40
+ end
30
41
  end
31
42
  end
32
43
  end
@@ -23,6 +23,9 @@ module Aldous
23
23
 
24
24
  precondition, precondition_result = PreconditionsExecutionService.new(action, controller).perform
25
25
 
26
+ # a precondition executed a render or a redirect
27
+ return if controller.performed?
28
+
26
29
  action_result = nil
27
30
  if precondition_result
28
31
  action = precondition
@@ -31,6 +34,9 @@ module Aldous
31
34
  action_result = action.perform
32
35
  end
33
36
 
37
+ # the action executed render or a redirect
38
+ return if controller.performed?
39
+
34
40
  Action::ResultExecutionService.perform(controller, action_result, action.default_view_data)
35
41
  rescue => e
36
42
  LoggingWrapper.log(e)
@@ -46,7 +46,7 @@ module Aldous
46
46
  []
47
47
  end
48
48
 
49
- def default_error_respondable
49
+ def default_error_handler(error)
50
50
  ::Aldous::View::Blank::HtmlView
51
51
  end
52
52
 
@@ -1,3 +1,3 @@
1
1
  module Aldous
2
- VERSION = "1.0.0"
2
+ VERSION = "1.0.1"
3
3
  end
@@ -4,13 +4,13 @@ RSpec.describe Aldous::Controller::Action::Wrapper do
4
4
  let(:controller_action) { double 'controller action',
5
5
  default_view_data: default_view_data,
6
6
  preconditions: preconditions,
7
- default_error_respondable: default_error_respondable,
7
+ default_error_handler: default_error_handler,
8
8
  perform: nil,
9
9
  build_view: nil }
10
10
 
11
11
  let(:default_view_data) { {default_view_data: true} }
12
12
  let(:preconditions) { double 'preconditions' }
13
- let(:default_error_respondable) {double 'default_error_respondable'}
13
+ let(:default_error_handler) {double 'default_error_handler'}
14
14
 
15
15
  before do
16
16
  allow(Aldous::LoggingWrapper).to receive(:log)
@@ -31,15 +31,26 @@ RSpec.describe Aldous::Controller::Action::Wrapper do
31
31
  allow(controller_action).to receive(:perform).and_raise(e)
32
32
  end
33
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
34
  it 'reports the error' do
40
35
  expect(Aldous::LoggingWrapper).to receive(:log).with(e)
41
36
  perform
42
37
  end
38
+
39
+ context "and the default error handler is a respondable" do
40
+ let(:default_error_handler) {Aldous::Respondable::Renderable}
41
+
42
+ it "builds a default error view with errors" do
43
+ expect(controller_action).to receive(:build_view).with(default_error_handler, errors: [e])
44
+ perform
45
+ end
46
+ end
47
+
48
+ context "and the default error handler is not a respondable" do
49
+ it "doesn't need to do anything" do
50
+ expect(controller_action).to_not receive(:build_view)
51
+ perform
52
+ end
53
+ end
43
54
  end
44
55
  end
45
56
  end
@@ -1,7 +1,7 @@
1
1
  RSpec.describe Aldous::Controller::ActionExecutionService do
2
2
  let(:action_execution_service) {described_class.new(controller, controller_action_class)}
3
3
 
4
- let(:controller) {double "controller", head: nil}
4
+ let(:controller) {double "controller", head: nil, performed?: false}
5
5
  let(:controller_action_class) {double "controller_action_class", build: action}
6
6
 
7
7
  let(:action) {double 'action', perform: action_result, default_view_data: default_view_data}
@@ -44,6 +44,21 @@ RSpec.describe Aldous::Controller::ActionExecutionService do
44
44
  expect(Aldous::Controller::Action::ResultExecutionService).to receive(:perform).with(controller, action_result, default_view_data)
45
45
  perform
46
46
  end
47
+
48
+ context "but action perform halted execution due to render" do
49
+ before do
50
+ allow(controller).to receive(:performed?).and_return(false, true)
51
+ end
52
+
53
+ it "returns nil" do
54
+ expect(perform).to be_nil
55
+ end
56
+
57
+ it "does not execute the result" do
58
+ expect(Aldous::Controller::Action::ResultExecutionService).to_not receive(:perform)
59
+ perform
60
+ end
61
+ end
47
62
  end
48
63
 
49
64
  context "when preconditions did halt execution" do
@@ -57,6 +72,19 @@ RSpec.describe Aldous::Controller::ActionExecutionService do
57
72
  expect(Aldous::Controller::Action::ResultExecutionService).to receive(:perform).with(controller, precondition_result, default_view_data)
58
73
  perform
59
74
  end
75
+
76
+ context "via a render on the controller object" do
77
+ let(:controller) {double "controller", head: nil, performed?: true}
78
+
79
+ it "returns nil" do
80
+ expect(perform).to be_nil
81
+ end
82
+
83
+ it "does not execute the result" do
84
+ expect(Aldous::Controller::Action::ResultExecutionService).to_not receive(:perform)
85
+ perform
86
+ end
87
+ end
60
88
  end
61
89
 
62
90
  context "when error occurs" do
@@ -67,9 +67,9 @@ RSpec.describe Aldous::ControllerAction do
67
67
  end
68
68
  end
69
69
 
70
- describe "#default_error_respondable" do
70
+ describe "#default_error_handler" do
71
71
  it "is a blank html view by default" do
72
- expect(action.default_error_respondable).to eq Aldous::View::Blank::HtmlView
72
+ expect(action.default_error_handler('foo')).to eq Aldous::View::Blank::HtmlView
73
73
  end
74
74
  end
75
75
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aldous
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alan Skorkin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-20 00:00:00.000000000 Z
11
+ date: 2015-04-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -83,6 +83,7 @@ files:
83
83
  - examples/basic_todo/app/controller_actions/base_precondition.rb
84
84
  - examples/basic_todo/app/controller_actions/home_controller/show.rb
85
85
  - examples/basic_todo/app/controller_actions/shared/ensure_user_not_disabled_precondition.rb
86
+ - examples/basic_todo/app/controller_actions/shared/no_aldous_view_precondition.rb
86
87
  - examples/basic_todo/app/controller_actions/sign_ins_controller/create.rb
87
88
  - examples/basic_todo/app/controller_actions/sign_ins_controller/destroy.rb
88
89
  - examples/basic_todo/app/controller_actions/sign_ins_controller/new.rb