rory 0.3.15 → 0.3.16
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.
data/lib/rory/controller.rb
CHANGED
@@ -19,13 +19,13 @@ module Rory
|
|
19
19
|
end
|
20
20
|
|
21
21
|
# Register a method to run before the action method.
|
22
|
-
def before_action(method_name)
|
23
|
-
before_actions << method_name
|
22
|
+
def before_action(method_name, opts = {})
|
23
|
+
before_actions << opts.merge(:method_name => method_name)
|
24
24
|
end
|
25
25
|
|
26
26
|
# Register a method to run after the action method.
|
27
|
-
def after_action(method_name)
|
28
|
-
after_actions << method_name
|
27
|
+
def after_action(method_name, opts = {})
|
28
|
+
after_actions << opts.merge(:method_name => method_name)
|
29
29
|
end
|
30
30
|
|
31
31
|
def ancestor_actions(action_type)
|
@@ -94,7 +94,7 @@ module Rory
|
|
94
94
|
def present
|
95
95
|
# Call all before and after filters, and if a method exists on the
|
96
96
|
# controller for the requested action, call it in between.
|
97
|
-
call_filtered_action(@route.action)
|
97
|
+
call_filtered_action(@route.action.to_sym)
|
98
98
|
|
99
99
|
if @response
|
100
100
|
# that method may have resulted in a response already being generated
|
@@ -113,20 +113,30 @@ module Rory
|
|
113
113
|
|
114
114
|
private
|
115
115
|
|
116
|
-
def
|
117
|
-
|
116
|
+
def call_filter_for_action?(filter, action)
|
117
|
+
(filter[:only].nil? || filter[:only].include?(action)) &&
|
118
|
+
(filter[:except].nil? || !filter[:except].include?(action))
|
119
|
+
end
|
120
|
+
|
121
|
+
def get_relevant_filters(which_set, action)
|
118
122
|
filters = self.class.send(which_set)
|
123
|
+
filters.select { |filter| call_filter_for_action?(filter, action) }
|
124
|
+
end
|
125
|
+
|
126
|
+
def call_filter_set(which_set, action, opts = {})
|
127
|
+
opts = { :break_if_response => true }.merge(opts)
|
128
|
+
filters = get_relevant_filters(which_set, action)
|
119
129
|
filters.each do |filter|
|
120
130
|
break if @response && opts[:break_if_response]
|
121
|
-
self.send(filter)
|
131
|
+
self.send(filter[:method_name])
|
122
132
|
end
|
123
133
|
end
|
124
134
|
|
125
135
|
def call_filtered_action(action)
|
126
|
-
call_filter_set(:before_actions)
|
136
|
+
call_filter_set(:before_actions, action)
|
127
137
|
unless @response
|
128
138
|
self.send(action) if self.respond_to?(action)
|
129
|
-
call_filter_set(:after_actions, :break_if_response => false)
|
139
|
+
call_filter_set(:after_actions, action, :break_if_response => false)
|
130
140
|
end
|
131
141
|
end
|
132
142
|
end
|
data/lib/rory/version.rb
CHANGED
@@ -2,5 +2,9 @@ require_relative 'base_filtered_controller'
|
|
2
2
|
|
3
3
|
class FilteredController < BaseFilteredController
|
4
4
|
before_action :make_it_tasty
|
5
|
-
|
5
|
+
before_action :make_it_nutritious, :only => [:eat]
|
6
|
+
after_action :sleep, :except => [:eat]
|
7
|
+
|
8
|
+
def eat
|
9
|
+
end
|
6
10
|
end
|
@@ -89,12 +89,9 @@ describe Rory::Controller do
|
|
89
89
|
describe "#present" do
|
90
90
|
it "calls filters and action from route if exists on controller" do
|
91
91
|
controller = FilteredController.new(@request, @routing)
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
expect(controller).to receive(:rub_tummy).ordered
|
96
|
-
expect(controller).to receive(:sleep).ordered
|
97
|
-
expect(controller).to receive(:render).ordered
|
92
|
+
[:pickle_something, :make_it_tasty, :letsgo, :rub_tummy, :sleep, :render].each do |m|
|
93
|
+
expect(controller).to receive(m).ordered
|
94
|
+
end
|
98
95
|
controller.present
|
99
96
|
end
|
100
97
|
|
@@ -103,11 +100,9 @@ describe Rory::Controller do
|
|
103
100
|
def controller.pickle_something
|
104
101
|
@response = 'stuff'
|
105
102
|
end
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
expect(controller).to receive(:sleep).never
|
110
|
-
expect(controller).to receive(:render).never
|
103
|
+
[:make_it_tasty, :letsgo, :rub_tummy, :sleep, :render].each do |m|
|
104
|
+
expect(controller).to receive(m).never
|
105
|
+
end
|
111
106
|
controller.present
|
112
107
|
end
|
113
108
|
|
@@ -127,12 +122,23 @@ describe Rory::Controller do
|
|
127
122
|
|
128
123
|
it "doesn't try to call action from route if nonexistent on controller" do
|
129
124
|
controller = FilteredController.new(@request, @routing)
|
130
|
-
allow(controller).to receive(:respond_to?).with(
|
131
|
-
expect(controller).to receive(:pickle_something).ordered
|
132
|
-
expect(controller).to receive(:make_it_tasty).ordered
|
125
|
+
allow(controller).to receive(:respond_to?).with(:letsgo).and_return(false)
|
133
126
|
expect(controller).to receive(:letsgo).never
|
127
|
+
[:pickle_something, :make_it_tasty, :rub_tummy, :sleep, :render].each do |m|
|
128
|
+
expect(controller).to receive(m).ordered
|
129
|
+
end
|
130
|
+
controller.present
|
131
|
+
end
|
132
|
+
|
133
|
+
it "filters before and after actions on :only and :except" do
|
134
|
+
@routing[:route] = Rory::Route.new('', :to => 'test#eat')
|
135
|
+
controller = FilteredController.new(@request, @routing)
|
136
|
+
expect(controller).to receive(:make_it_tasty).ordered
|
137
|
+
expect(controller).to receive(:make_it_nutritious).ordered
|
138
|
+
expect(controller).to receive(:eat).ordered
|
134
139
|
expect(controller).to receive(:rub_tummy).ordered
|
135
|
-
expect(controller).to receive(:
|
140
|
+
expect(controller).to receive(:smile).ordered
|
141
|
+
expect(controller).to receive(:sleep).never
|
136
142
|
expect(controller).to receive(:render).ordered
|
137
143
|
controller.present
|
138
144
|
end
|