rory 0.5.0 → 0.5.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/rory/controller.rb +1 -1
- data/lib/rory/support.rb +17 -0
- data/lib/rory/version.rb +1 -1
- data/spec/lib/rory/controller_spec.rb +100 -119
- data/spec/lib/rory/support_spec.rb +20 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f460afe6851a0ebbff77cd5459bf5dc2e4637c6c
|
4
|
+
data.tar.gz: f22a44a3d6141f2db251415ce18724e172fb9ca1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 101bc4e7b9de76e550b74eae963664d84f04a70e497d3016f6f117b74ef0140a9939362b6a338fadb6b82558b430682a6a677550c9513341b103723ab176325a
|
7
|
+
data.tar.gz: afc2f14d5c59c4e4ba834813b3c2e82f5f6cbec9f8518c8a7f29e90c28dc9dc135904e70aa79da71189e5765b9e9826d24c5faba2dcae2766be8f5b94cafe227
|
data/lib/rory/controller.rb
CHANGED
data/lib/rory/support.rb
CHANGED
@@ -29,5 +29,22 @@ module Rory
|
|
29
29
|
require file
|
30
30
|
end
|
31
31
|
end
|
32
|
+
|
33
|
+
def encode_as_json(object)
|
34
|
+
hashed = if object.is_a?(Array)
|
35
|
+
object.map { |o| try_to_hash(o) }
|
36
|
+
else
|
37
|
+
try_to_hash(object)
|
38
|
+
end
|
39
|
+
hashed.to_json
|
40
|
+
end
|
41
|
+
|
42
|
+
def try_to_hash(object)
|
43
|
+
if object.respond_to?(:to_hash)
|
44
|
+
object.to_hash
|
45
|
+
else
|
46
|
+
object
|
47
|
+
end
|
48
|
+
end
|
32
49
|
end
|
33
50
|
end
|
data/lib/rory/version.rb
CHANGED
@@ -1,4 +1,6 @@
|
|
1
1
|
describe Rory::Controller do
|
2
|
+
subject { Rory::Controller.new(@request, @routing) }
|
3
|
+
|
2
4
|
before :each do
|
3
5
|
@routing = {
|
4
6
|
:route => Rory::Route.new('', :to => 'test#letsgo')
|
@@ -18,15 +20,13 @@ describe Rory::Controller do
|
|
18
20
|
|
19
21
|
describe '#layout' do
|
20
22
|
it 'defaults to nil' do
|
21
|
-
|
22
|
-
expect(controller.layout).to be_nil
|
23
|
+
expect(subject.layout).to be_nil
|
23
24
|
end
|
24
25
|
end
|
25
26
|
|
26
27
|
describe '#params' do
|
27
28
|
it 'returns params from request, converted for indifferent key access' do
|
28
|
-
|
29
|
-
expect(controller.params).to eq({
|
29
|
+
expect(subject.params).to eq({
|
30
30
|
'violet' => 'invisibility',
|
31
31
|
'dash' => 'superspeed',
|
32
32
|
:violet => 'invisibility',
|
@@ -39,8 +39,7 @@ describe Rory::Controller do
|
|
39
39
|
it "delegates to dispatcher from request" do
|
40
40
|
@routing[:dispatcher] = dispatcher = double
|
41
41
|
expect(dispatcher).to receive(:redirect).with(:whatever)
|
42
|
-
|
43
|
-
controller.redirect(:whatever)
|
42
|
+
subject.redirect(:whatever)
|
44
43
|
end
|
45
44
|
end
|
46
45
|
|
@@ -48,117 +47,109 @@ describe Rory::Controller do
|
|
48
47
|
it "delegates to dispatcher from request" do
|
49
48
|
@routing[:dispatcher] = dispatcher = double
|
50
49
|
expect(dispatcher).to receive(:render_not_found)
|
51
|
-
|
52
|
-
controller.render_not_found
|
50
|
+
subject.render_not_found
|
53
51
|
end
|
54
52
|
end
|
55
53
|
|
56
54
|
describe "#base_path" do
|
57
55
|
it "returns script_name from request" do
|
58
|
-
|
59
|
-
expect(controller.base_path).to eq 'script_root'
|
56
|
+
expect(subject.base_path).to eq 'script_root'
|
60
57
|
end
|
61
58
|
end
|
62
59
|
|
63
60
|
describe "#present" do
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
61
|
+
context "with filters" do
|
62
|
+
subject { FilteredController.new(@request, @routing) }
|
63
|
+
|
64
|
+
it "calls filters and action from route if exists on controller" do
|
65
|
+
[:pickle_something, :make_it_tasty, :letsgo, :rub_tummy, :sleep, :render].each do |m|
|
66
|
+
expect(subject).to receive(m).ordered
|
67
|
+
end
|
68
|
+
subject.present
|
68
69
|
end
|
69
|
-
controller.present
|
70
|
-
end
|
71
70
|
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
71
|
+
it "short circuits if a before_action generates a response" do
|
72
|
+
def subject.pickle_something
|
73
|
+
@response = 'stuff'
|
74
|
+
end
|
75
|
+
[:make_it_tasty, :letsgo, :rub_tummy, :sleep, :render].each do |m|
|
76
|
+
expect(subject).to receive(m).never
|
77
|
+
end
|
78
|
+
subject.present
|
79
79
|
end
|
80
|
-
controller.present
|
81
|
-
end
|
82
80
|
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
81
|
+
it "does not short circuit after_actions if action generates response" do
|
82
|
+
def subject.letsgo
|
83
|
+
@response = 'stuff'
|
84
|
+
end
|
85
|
+
expect(subject).to receive(:pickle_something).ordered
|
86
|
+
expect(subject).to receive(:make_it_tasty).ordered
|
87
|
+
expect(subject).to receive(:letsgo).ordered.and_call_original
|
88
|
+
expect(subject).to receive(:rub_tummy).ordered
|
89
|
+
expect(subject).to receive(:sleep).ordered
|
90
|
+
expect(subject).to receive(:render).never
|
91
|
+
subject.present
|
87
92
|
end
|
88
|
-
expect(controller).to receive(:pickle_something).ordered
|
89
|
-
expect(controller).to receive(:make_it_tasty).ordered
|
90
|
-
expect(controller).to receive(:letsgo).ordered.and_call_original
|
91
|
-
expect(controller).to receive(:rub_tummy).ordered
|
92
|
-
expect(controller).to receive(:sleep).ordered
|
93
|
-
expect(controller).to receive(:render).never
|
94
|
-
controller.present
|
95
|
-
end
|
96
93
|
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
94
|
+
it "doesn't try to call action from route if nonexistent on controller" do
|
95
|
+
allow(@routing[:route]).to receive(:action).and_return('no worries')
|
96
|
+
[:pickle_something, :make_it_tasty, :rub_tummy, :sleep, :render].each do |m|
|
97
|
+
expect(subject).to receive(m).ordered
|
98
|
+
end
|
99
|
+
expect { subject.present }.not_to raise_error
|
102
100
|
end
|
103
|
-
expect { controller.present }.not_to raise_error
|
104
|
-
end
|
105
101
|
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
end
|
102
|
+
it "filters before and after actions on :only and :except" do
|
103
|
+
@routing[:route] = Rory::Route.new('', :to => 'test#eat')
|
104
|
+
expect(subject).to receive(:make_it_tasty).ordered
|
105
|
+
expect(subject).to receive(:make_it_nutritious).ordered
|
106
|
+
expect(subject).to receive(:eat).ordered
|
107
|
+
expect(subject).to receive(:rub_tummy).ordered
|
108
|
+
expect(subject).to receive(:smile).ordered
|
109
|
+
expect(subject).to receive(:sleep).never
|
110
|
+
expect(subject).to receive(:render).ordered
|
111
|
+
subject.present
|
112
|
+
end
|
118
113
|
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
114
|
+
it "filters before and after actions on :if and :unless" do
|
115
|
+
@routing[:route] = Rory::Route.new('', :to => 'test#eat')
|
116
|
+
@request = double('Rack::Request', {
|
117
|
+
:params => { 'horses' => 'missing' },
|
118
|
+
:script_name => 'script_root'
|
119
|
+
})
|
120
|
+
expect(subject).to receive(:make_it_tasty).never
|
121
|
+
expect(subject).to receive(:make_it_nutritious).ordered
|
122
|
+
expect(subject).to receive(:eat).ordered
|
123
|
+
expect(subject).to receive(:rub_tummy).never
|
124
|
+
expect(subject).to receive(:smile).ordered
|
125
|
+
expect(subject).to receive(:sleep).never
|
126
|
+
expect(subject).to receive(:render).ordered
|
127
|
+
subject.present
|
128
|
+
end
|
134
129
|
end
|
135
130
|
|
136
131
|
it "just returns a response if @response exists" do
|
137
|
-
|
138
|
-
|
139
|
-
expect(controller.present).to eq('Forced response')
|
132
|
+
subject.instance_variable_set(:@response, 'Forced response')
|
133
|
+
expect(subject.present).to eq('Forced response')
|
140
134
|
end
|
141
135
|
|
142
136
|
it "sends a previously set @body to render" do
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
expect(controller.present).to eq('Forced response')
|
137
|
+
subject.instance_variable_set(:@body, 'Forced body')
|
138
|
+
allow(subject).to receive(:render).with(:body => 'Forced body').and_return("Forced response")
|
139
|
+
expect(subject.present).to eq('Forced response')
|
147
140
|
end
|
148
141
|
|
149
142
|
it "returns the result of render" do
|
150
|
-
|
151
|
-
|
152
|
-
expect(controller.present).to eq('The response')
|
143
|
+
allow(subject).to receive(:render).with(:body => nil).and_return("The response")
|
144
|
+
expect(subject.present).to eq('The response')
|
153
145
|
end
|
154
146
|
end
|
155
147
|
|
156
148
|
describe "#render" do
|
157
149
|
it "returns the result of #generate_body_for_render as a rack response" do
|
158
|
-
|
159
|
-
allow(
|
160
|
-
|
161
|
-
expect(controller.render).to eq([
|
150
|
+
allow(subject).to receive(:default_content_type).and_return("a prison")
|
151
|
+
allow(subject).to receive(:generate_for_render).and_return("Valoop!")
|
152
|
+
expect(subject.render).to eq([
|
162
153
|
200,
|
163
154
|
{'Content-type' => 'a prison', 'charset' => 'UTF-8'},
|
164
155
|
["Valoop!"]
|
@@ -166,9 +157,8 @@ describe Rory::Controller do
|
|
166
157
|
end
|
167
158
|
|
168
159
|
it "returns given body as a rack response" do
|
169
|
-
|
170
|
-
|
171
|
-
expect(controller.render(:body => 'Forced body')).to eq([
|
160
|
+
allow(subject).to receive(:default_content_type).and_return("snooj/woz")
|
161
|
+
expect(subject.render(:body => 'Forced body')).to eq([
|
172
162
|
200,
|
173
163
|
{'Content-type' => 'snooj/woz', 'charset' => 'UTF-8'},
|
174
164
|
["Forced body"]
|
@@ -178,58 +168,51 @@ describe Rory::Controller do
|
|
178
168
|
|
179
169
|
describe "#json_requested?" do
|
180
170
|
it "delegates to dispatcher" do
|
181
|
-
|
182
|
-
|
183
|
-
expect(controller.json_requested?).to eq(:snakes)
|
171
|
+
allow(subject).to receive(:dispatcher).and_return(double(:json_requested? => :snakes))
|
172
|
+
expect(subject.json_requested?).to eq(:snakes)
|
184
173
|
end
|
185
174
|
end
|
186
175
|
|
187
176
|
describe "#generate_for_render" do
|
188
177
|
it "renders and returns the default template if not json" do
|
189
|
-
|
190
|
-
|
191
|
-
expect(controller.generate_for_render).to eq("Whee")
|
178
|
+
allow(subject).to receive(:generate_body_from_template).with("test/letsgo", {}).and_return("Whee")
|
179
|
+
expect(subject.generate_for_render).to eq("Whee")
|
192
180
|
end
|
193
181
|
|
194
182
|
it "renders and returns the given template if not json" do
|
195
|
-
|
196
|
-
|
197
|
-
expect(controller.generate_for_render(:template => 'engines')).to eq("Oh dear")
|
183
|
+
allow(subject).to receive(:generate_body_from_template).with("engines", {}).and_return("Oh dear")
|
184
|
+
expect(subject.generate_for_render(:template => 'engines')).to eq("Oh dear")
|
198
185
|
end
|
199
186
|
|
200
187
|
it "returns json version of given json object if json" do
|
201
|
-
|
202
|
-
|
203
|
-
expect(controller.generate_for_render(:json => :an_object)).to eq("Oh dear")
|
188
|
+
allow(subject).to receive(:generate_json_from_object).with(:an_object, {}).and_return("Oh dear")
|
189
|
+
expect(subject.generate_for_render(:json => :an_object)).to eq("Oh dear")
|
204
190
|
end
|
205
191
|
end
|
206
192
|
|
207
193
|
describe "#generate_json_from_object" do
|
208
|
-
it "
|
209
|
-
|
210
|
-
|
211
|
-
expect(controller.generate_json_from_object(object)).to eq(:jsonified)
|
194
|
+
it "encodes given object as json" do
|
195
|
+
allow(Rory::Support).to receive(:encode_as_json).with(:foo).and_return(:encoded_foo)
|
196
|
+
expect(subject.generate_json_from_object(:foo)).to eq(:encoded_foo)
|
212
197
|
end
|
213
198
|
end
|
214
199
|
|
215
200
|
describe "#generate_body_from_template" do
|
216
201
|
it "returns rendered template with given name" do
|
217
|
-
|
218
|
-
expect(controller.generate_body_from_template('test/letsgo')).to eq("Let's go content")
|
202
|
+
expect(subject.generate_body_from_template('test/letsgo')).to eq("Let's go content")
|
219
203
|
end
|
220
204
|
|
221
205
|
it "returns renderer output" do
|
222
|
-
controller = Rory::Controller.new(@request, @routing)
|
223
206
|
allow(Rory::Renderer).to receive(:new).
|
224
|
-
with('not/real',
|
207
|
+
with('not/real', subject.default_renderer_options).
|
225
208
|
and_return(double('Renderer', :render => 'Here ya go'))
|
226
|
-
expect(
|
209
|
+
expect(subject.generate_body_from_template('not/real')).to eq('Here ya go')
|
227
210
|
end
|
228
211
|
|
229
212
|
it "passes layout, exposed locals, and app to renderer" do
|
230
|
-
|
231
|
-
|
232
|
-
allow(
|
213
|
+
subject = Rory::Controller.new(@request, @routing, :scooby)
|
214
|
+
subject.expose(:a => 1)
|
215
|
+
allow(subject).to receive(:layout).and_return('pretend')
|
233
216
|
renderer_options = {
|
234
217
|
:layout => 'pretend',
|
235
218
|
:locals => { :a => 1 },
|
@@ -239,21 +222,19 @@ describe Rory::Controller do
|
|
239
222
|
allow(Rory::Renderer).to receive(:new).
|
240
223
|
with('also/fake', renderer_options).
|
241
224
|
and_return(double('Renderer', :render => 'Scamazing!'))
|
242
|
-
expect(
|
225
|
+
expect(subject.generate_body_from_template('also/fake')).to eq('Scamazing!')
|
243
226
|
end
|
244
227
|
end
|
245
228
|
|
246
229
|
describe "#default_content_type" do
|
247
230
|
it "returns 'text/html' if not json" do
|
248
|
-
|
249
|
-
|
250
|
-
expect(controller.default_content_type).to eq('text/html')
|
231
|
+
allow(subject).to receive(:json_requested?).and_return(false)
|
232
|
+
expect(subject.default_content_type).to eq('text/html')
|
251
233
|
end
|
252
234
|
|
253
235
|
it "returns 'application/json' if json requested" do
|
254
|
-
|
255
|
-
|
256
|
-
expect(controller.default_content_type).to eq('application/json')
|
236
|
+
allow(subject).to receive(:json_requested?).and_return(true)
|
237
|
+
expect(subject.default_content_type).to eq('application/json')
|
257
238
|
end
|
258
239
|
end
|
259
240
|
end
|
@@ -64,4 +64,24 @@ describe Rory::Support do
|
|
64
64
|
expect(described_class.tokenize(:yourFaceIsNice)).to eq('your_face_is_nice')
|
65
65
|
end
|
66
66
|
end
|
67
|
+
|
68
|
+
describe ".encode_as_json" do
|
69
|
+
it "returns given object as json" do
|
70
|
+
object = double(:to_json => :jsonified)
|
71
|
+
expect(described_class.encode_as_json(object)).to eq(:jsonified)
|
72
|
+
end
|
73
|
+
|
74
|
+
it "calls to_hash first if object responds to it" do
|
75
|
+
object = double(:to_hash => { 'april' => 'friday' })
|
76
|
+
expect(described_class.encode_as_json(object)).to eq({ 'april' => 'friday' }.to_json)
|
77
|
+
end
|
78
|
+
|
79
|
+
it "converts each member of an array" do
|
80
|
+
object = [
|
81
|
+
double(:to_hash => :smurf),
|
82
|
+
double(:to_hash => :nerf)
|
83
|
+
]
|
84
|
+
expect(described_class.encode_as_json(object)).to eq([:smurf, :nerf].to_json)
|
85
|
+
end
|
86
|
+
end
|
67
87
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rory
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ravi Gadad
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-03-
|
11
|
+
date: 2015-03-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rack
|