rory 0.3.13 → 0.3.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -9,6 +9,26 @@ module Rory
9
9
 
10
10
  attr_accessor :locals
11
11
 
12
+ class << self
13
+ def before_actions
14
+ @before_actions ||= []
15
+ end
16
+
17
+ def after_actions
18
+ @after_actions ||= []
19
+ end
20
+
21
+ # Register a method to run before the action method.
22
+ def before_action(method_name)
23
+ before_actions << method_name
24
+ end
25
+
26
+ # Register a method to run after the action method.
27
+ def after_action(method_name)
28
+ after_actions << method_name
29
+ end
30
+ end
31
+
12
32
  def initialize(request, routing, app = nil)
13
33
  @request = request
14
34
  @dispatcher = routing[:dispatcher]
@@ -64,22 +84,10 @@ module Rory
64
84
  @response = @dispatcher.render_not_found
65
85
  end
66
86
 
67
- # This method is called before the action method.
68
- def before_action
69
- #noop
70
- end
71
-
72
- # This method is called after the action method.
73
- def after_action
74
- #noop
75
- end
76
-
77
87
  def present
78
- # if a method exists on the controller for the requested action, call it.
79
- action = @route.action
80
- before_action
81
- self.send(action) if self.respond_to?(action)
82
- after_action
88
+ # Call all before and after filters, and if a method exists on the
89
+ # controller for the requested action, call it in between.
90
+ call_filtered_action(@route.action)
83
91
 
84
92
  if @response
85
93
  # that method may have resulted in a response already being generated
@@ -95,5 +103,24 @@ module Rory
95
103
  [200, {'Content-type' => 'text/html', 'charset' => 'UTF-8'}, [@body]]
96
104
  end
97
105
  end
106
+
107
+ private
108
+
109
+ def call_filter_set(which_set, opts = {})
110
+ opts[:break_if_response] ||= true
111
+ filters = self.class.send(which_set)
112
+ filters.each do |filter|
113
+ self.send(filter)
114
+ break if @response && opts[:break_if_response]
115
+ end
116
+ end
117
+
118
+ def call_filtered_action(action)
119
+ call_filter_set(:before_actions)
120
+ unless @response
121
+ self.send(action) if self.respond_to?(action)
122
+ call_filter_set(:after_actions, :break_if_response => false)
123
+ end
124
+ end
98
125
  end
99
126
  end
data/lib/rory/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Rory
2
- VERSION = '0.3.13'
2
+ VERSION = '0.3.14'
3
3
  end
@@ -0,0 +1,5 @@
1
+ class FilteredController < Rory::Controller
2
+ before_action :pickle_something
3
+ before_action :make_it_tasty
4
+ after_action :rub_tummy
5
+ end
@@ -88,21 +88,48 @@ describe Rory::Controller do
88
88
 
89
89
  describe "#present" do
90
90
  it "calls filters and action from route if exists on controller" do
91
- controller = Rory::Controller.new(@request, @routing)
92
- expect(controller).to receive('before_action').ordered
93
- expect(controller).to receive('letsgo').ordered
94
- expect(controller).to receive('after_action').ordered
95
- expect(controller).to receive('render').ordered
91
+ controller = FilteredController.new(@request, @routing)
92
+ expect(controller).to receive(:pickle_something).ordered
93
+ expect(controller).to receive(:make_it_tasty).ordered
94
+ expect(controller).to receive(:letsgo).ordered
95
+ expect(controller).to receive(:rub_tummy).ordered
96
+ expect(controller).to receive(:render).ordered
97
+ controller.present
98
+ end
99
+
100
+ it "short circuits if a before_action generates a response" do
101
+ controller = FilteredController.new(@request, @routing)
102
+ def controller.pickle_something
103
+ @response = 'stuff'
104
+ end
105
+ expect(controller).to receive(:make_it_tasty).never
106
+ expect(controller).to receive(:letsgo).never
107
+ expect(controller).to receive(:rub_tummy).never
108
+ expect(controller).to receive(:render).never
109
+ controller.present
110
+ end
111
+
112
+ it "does not short circuit after_actions if action generates response" do
113
+ controller = FilteredController.new(@request, @routing)
114
+ def controller.letsgo
115
+ @response = 'stuff'
116
+ end
117
+ expect(controller).to receive(:pickle_something).ordered
118
+ expect(controller).to receive(:make_it_tasty).ordered
119
+ expect(controller).to receive(:letsgo).ordered.and_call_original
120
+ expect(controller).to receive(:rub_tummy).ordered
121
+ expect(controller).to receive(:render).never
96
122
  controller.present
97
123
  end
98
124
 
99
125
  it "doesn't try to call action from route if nonexistent on controller" do
100
- controller = Rory::Controller.new(@request, @routing)
126
+ controller = FilteredController.new(@request, @routing)
101
127
  allow(controller).to receive(:respond_to?).with('letsgo').and_return(false)
102
- expect(controller).to receive('before_action').ordered
103
- expect(controller).to receive('letsgo').never
104
- expect(controller).to receive('after_action').ordered
105
- expect(controller).to receive('render').ordered
128
+ expect(controller).to receive(:pickle_something).ordered
129
+ expect(controller).to receive(:make_it_tasty).ordered
130
+ expect(controller).to receive(:letsgo).never
131
+ expect(controller).to receive(:rub_tummy).ordered
132
+ expect(controller).to receive(:render).ordered
106
133
  controller.present
107
134
  end
108
135
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rory
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.13
4
+ version: 0.3.14
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-03-30 00:00:00.000000000 Z
12
+ date: 2014-03-31 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rack
@@ -205,6 +205,7 @@ files:
205
205
  - lib/tasks/rory.rake
206
206
  - spec/fixture_app/config/application.rb
207
207
  - spec/fixture_app/config/routes.rb
208
+ - spec/fixture_app/controllers/filtered_controller.rb
208
209
  - spec/fixture_app/controllers/for_reals_controller.rb
209
210
  - spec/fixture_app/controllers/goose/lumpies_controller.rb
210
211
  - spec/fixture_app/controllers/goose/wombat/rabbits_controller.rb