basic_assumption 0.3.8 → 0.3.9
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.
@@ -20,13 +20,14 @@ module BasicAssumption
|
|
20
20
|
end
|
21
21
|
|
22
22
|
# Returns a block that will attempt to do the correct thing depending
|
23
|
-
# on
|
24
|
-
#
|
25
|
-
#
|
23
|
+
# on the plurality of the name passed to +assume+ and the action for the
|
24
|
+
# current request. If the name is singular and the action is not 'new'
|
25
|
+
# or 'create', then +assume+ will find an instance of
|
26
|
+
# an ActiveRecord model of the name that it received and an id
|
26
27
|
# value in the parameters. If the action is 'new' or 'create', +assume+
|
27
28
|
# will instantiate a new instance of the model class, passing in the
|
28
|
-
# values it finds in the +params+ hash with a key of the
|
29
|
-
#
|
29
|
+
# values it finds in the +params+ hash with for a key of the name passed
|
30
|
+
# to +assume+. For example:
|
30
31
|
#
|
31
32
|
# class WidgetController < ApplicationController
|
32
33
|
# default_assumption :restful_rails
|
@@ -38,16 +39,15 @@ module BasicAssumption
|
|
38
39
|
# end
|
39
40
|
#
|
40
41
|
# Note the object will have been instantiated but not saved, destroyed,
|
41
|
-
# etc.
|
42
|
-
# behavior of +assume+. If the +name+ passed is of singular form, then
|
43
|
-
# a find will be performed, just as for a show or edit action. If the
|
44
|
-
# +name+ is a plural word, then +assume+ will find all instances of
|
45
|
-
# the model class.
|
42
|
+
# etc.
|
46
43
|
#
|
47
|
-
#
|
48
|
-
#
|
49
|
-
#
|
50
|
-
#
|
44
|
+
# If the name passed to assume is plural, there are two possibilities
|
45
|
+
# for the # behavior of +assume+. If the model responds to +paginate+ and
|
46
|
+
# there is a +page+ key in the +params+ hash, +assume+ will attempt to
|
47
|
+
# find all records of the model type paginated based on the +page+
|
48
|
+
# value in params and also a +per_page+ value. 15 will be the default
|
49
|
+
# if +per_page+ is not present. Otherwise, it returns all # records for
|
50
|
+
# the model.
|
51
51
|
def block
|
52
52
|
super
|
53
53
|
end
|
@@ -57,17 +57,13 @@ module BasicAssumption
|
|
57
57
|
list
|
58
58
|
elsif make?
|
59
59
|
model_class.new(resource_attributes)
|
60
|
-
|
60
|
+
elsif lookup?
|
61
61
|
model_class.find(lookup_id)
|
62
62
|
end
|
63
63
|
end
|
64
64
|
|
65
65
|
protected
|
66
66
|
|
67
|
-
def find? #:nodoc:
|
68
|
-
%w(show edit update destroy).include? action
|
69
|
-
end
|
70
|
-
|
71
67
|
def list #:nodoc:
|
72
68
|
if page?
|
73
69
|
model_class.paginate('page' => page, 'per_page' => per_page)
|
@@ -77,7 +73,7 @@ module BasicAssumption
|
|
77
73
|
end
|
78
74
|
|
79
75
|
def list? #:nodoc:
|
80
|
-
|
76
|
+
plural_name.eql?(name)
|
81
77
|
end
|
82
78
|
|
83
79
|
def lookup_id #:nodoc:
|
@@ -85,11 +81,11 @@ module BasicAssumption
|
|
85
81
|
end
|
86
82
|
|
87
83
|
def lookup? #:nodoc:
|
88
|
-
lookup_id.present?
|
84
|
+
lookup_id.present? && !list?
|
89
85
|
end
|
90
86
|
|
91
87
|
def make? #:nodoc:
|
92
|
-
%w(new create).include?(action) || !lookup?
|
88
|
+
%w(new create).include?(action) || !(lookup? || list?)
|
93
89
|
end
|
94
90
|
|
95
91
|
def page? #:nodoc:
|
@@ -22,136 +22,257 @@ describe BasicAssumption::DefaultAssumption::RestfulRails do
|
|
22
22
|
default.stub!(:params).and_return(params)
|
23
23
|
end
|
24
24
|
|
25
|
-
context "
|
26
|
-
let(:
|
25
|
+
context "when the name given to assume is plural" do
|
26
|
+
let(:name) { :models }
|
27
27
|
|
28
|
-
|
29
|
-
|
30
|
-
|
28
|
+
context "without pagination" do
|
29
|
+
%w(create destroy edit index new show update).each do |action|
|
30
|
+
let(:params) { {'action' => action} }
|
31
|
+
|
32
|
+
context "when there is an id in the params" do
|
33
|
+
before { params['id'] = 123 }
|
34
|
+
context "when action is #{action}" do
|
35
|
+
before { Model.should_receive(:all) }
|
36
|
+
context "when 'page' exists in the request params" do
|
37
|
+
before { params['page'] = '5' }
|
38
|
+
it "finds all the records of the model class" do
|
39
|
+
default.block.call(name)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
context "when 'page' does not exist in the request params" do
|
43
|
+
it "finds all the records of the model class" do
|
44
|
+
default.block.call(name)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
context "when there is not an id in the params" do
|
51
|
+
context "when action is #{action}" do
|
52
|
+
before { Model.should_receive(:all) }
|
53
|
+
context "when 'page' exists in the request params" do
|
54
|
+
before { params['page'] = '5' }
|
55
|
+
it "finds all the records of the model class" do
|
56
|
+
default.block.call(name)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
context "when 'page' does not exist in the request params" do
|
60
|
+
it "finds all the records of the model class" do
|
61
|
+
default.block.call(name)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
31
67
|
end
|
32
|
-
end
|
33
68
|
|
34
|
-
|
35
|
-
|
69
|
+
%w(create destroy edit index new show update).each do |action|
|
70
|
+
let(:params) { {'action' => action} }
|
36
71
|
|
37
|
-
|
38
|
-
|
39
|
-
|
72
|
+
context "with pagination" do
|
73
|
+
context "when there is an id in the params" do
|
74
|
+
before { params['id'] = 123 }
|
75
|
+
context "when action is #{action}" do
|
76
|
+
before { Model.stub! :paginate }
|
77
|
+
context "when 'page' exists in the request params" do
|
78
|
+
before { params['page'] = '5' }
|
79
|
+
it "paginates the records of the model class" do
|
80
|
+
Model.should_receive(:paginate)
|
81
|
+
default.block.call(name)
|
82
|
+
end
|
83
|
+
context "when 'per_page' exists in the request params" do
|
84
|
+
it "paginates using 'page' and 'per_page' from the params" do
|
85
|
+
params['per_page'] = '10'
|
86
|
+
Model.should_receive(:paginate).with('page' => '5', 'per_page' => '10')
|
87
|
+
default.block.call(name)
|
88
|
+
end
|
89
|
+
end
|
90
|
+
context "when 'per_page' does not exist in the request params" do
|
91
|
+
it "paginates using 'page' from the params and a default 'per_page' of 15" do
|
92
|
+
Model.should_receive(:paginate).with('page' => '5', 'per_page' => '15')
|
93
|
+
default.block.call(name)
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
context "when 'page' does not exist in the request params" do
|
98
|
+
it "finds all the records of the model class" do
|
99
|
+
Model.should_receive(:all)
|
100
|
+
default.block.call(name)
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
context "when there is not an id in the params" do
|
107
|
+
context "when action is #{action}" do
|
108
|
+
before { Model.stub! :paginate }
|
109
|
+
context "when 'page' exists in the request params" do
|
110
|
+
before { params['page'] = '5' }
|
111
|
+
it "paginates the records of the model class" do
|
112
|
+
Model.should_receive(:paginate)
|
113
|
+
default.block.call(name)
|
114
|
+
end
|
115
|
+
context "when 'per_page' exists in the request params" do
|
116
|
+
it "paginates using 'page' and 'per_page' from the params" do
|
117
|
+
params['per_page'] = '10'
|
118
|
+
Model.should_receive(:paginate).with('page' => '5', 'per_page' => '10')
|
119
|
+
default.block.call(name)
|
120
|
+
end
|
121
|
+
end
|
122
|
+
context "when 'per_page' does not exist in the request params" do
|
123
|
+
it "paginates using 'page' from the params and a default 'per_page' of 15" do
|
124
|
+
Model.should_receive(:paginate).with('page' => '5', 'per_page' => '15')
|
125
|
+
default.block.call(name)
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
129
|
+
context "when 'page' does not exist in the request params" do
|
130
|
+
it "finds all the records of the model class" do
|
131
|
+
Model.should_receive(:all)
|
132
|
+
default.block.call(name)
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|
40
138
|
end
|
41
139
|
end
|
42
140
|
|
43
|
-
context "
|
44
|
-
let(:
|
45
|
-
|
141
|
+
context "when the name given to assume is singular" do
|
142
|
+
let(:name) { :model }
|
143
|
+
context "in the index action" do
|
144
|
+
let(:params) { { 'action' => 'index' } }
|
145
|
+
context "and there is an id in params" do
|
146
|
+
before { params['id'] = 1 }
|
147
|
+
it "attempts to find a model instance based off the given name" do
|
148
|
+
Model.should_receive(:find).with(1).and_return(name)
|
149
|
+
default.block.call(name).should eql(name)
|
150
|
+
end
|
151
|
+
end
|
152
|
+
context "and there is no id in params" do
|
153
|
+
before { params['model'] = :initializers }
|
154
|
+
it "creates a new model instance and passes in appropriate params" do
|
155
|
+
Model.should_receive(:new).with(:initializers).and_return(name)
|
156
|
+
default.block.call(name).should eql(name)
|
157
|
+
end
|
158
|
+
end
|
46
159
|
end
|
47
160
|
|
48
|
-
|
49
|
-
|
50
|
-
|
161
|
+
context "in the show action" do
|
162
|
+
let(:params) { { 'id' => 42, 'action' => 'show' } }
|
163
|
+
|
164
|
+
it "attempts to find a model instance based off the given name" do
|
165
|
+
Model.should_receive(:find).with(42).and_return(name)
|
166
|
+
default.block.call(name).should eql(:model)
|
167
|
+
end
|
51
168
|
end
|
52
|
-
end
|
53
169
|
|
54
|
-
|
55
|
-
|
170
|
+
context "in the edit action" do
|
171
|
+
let(:params) { { 'id' => 42, 'action' => 'edit' } }
|
56
172
|
|
57
|
-
|
58
|
-
|
59
|
-
|
173
|
+
it "attempts to find a model instance based off the given name" do
|
174
|
+
Model.should_receive(:find).with(42).and_return(name)
|
175
|
+
default.block.call(name).should eql(:model)
|
176
|
+
end
|
60
177
|
end
|
61
|
-
end
|
62
178
|
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
179
|
+
context "in the update action" do
|
180
|
+
let(:params) do
|
181
|
+
{ 'id' => 42 }
|
182
|
+
end
|
183
|
+
|
184
|
+
it "attempts to find a model instance based off the given name" do
|
185
|
+
Model.should_receive(:find).with(42).and_return(name)
|
186
|
+
default.block.call(name).should eql(:model)
|
187
|
+
end
|
69
188
|
end
|
70
189
|
|
71
|
-
context "the
|
72
|
-
|
73
|
-
|
74
|
-
|
190
|
+
context "in the destroy action" do
|
191
|
+
let(:params) { { 'id' => 42, 'action' => 'destroy' } }
|
192
|
+
|
193
|
+
it "attempts to find a model instance based off the given name" do
|
194
|
+
Model.should_receive(:find).with(42).and_return(name)
|
195
|
+
default.block.call(name).should eql(:model)
|
196
|
+
end
|
75
197
|
end
|
76
|
-
end
|
77
198
|
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
199
|
+
context "in the create action" do
|
200
|
+
let(:params) do
|
201
|
+
{
|
202
|
+
'action' => 'create',
|
203
|
+
'model' => { 'age' => 27, 'color' => 'blue' }
|
204
|
+
}
|
205
|
+
end
|
206
|
+
|
207
|
+
context "the model instance" do
|
208
|
+
subject { default.block.call(name) }
|
209
|
+
its(:age) { should be(27) }
|
210
|
+
its(:color) { should eql('blue') }
|
211
|
+
end
|
84
212
|
end
|
85
213
|
|
86
|
-
context "the
|
87
|
-
|
88
|
-
|
89
|
-
|
214
|
+
context "in the new action" do
|
215
|
+
let(:params) do
|
216
|
+
{
|
217
|
+
'action' => 'new',
|
218
|
+
'model' => { 'age' => 27, 'color' => 'blue' }
|
219
|
+
}
|
220
|
+
end
|
221
|
+
|
222
|
+
context "the model instance" do
|
223
|
+
subject { default.block.call(name) }
|
224
|
+
its(:age) { should be(27) }
|
225
|
+
its(:color) { should eql('blue') }
|
226
|
+
end
|
90
227
|
end
|
91
228
|
end
|
229
|
+
end
|
92
230
|
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
end
|
104
|
-
end
|
105
|
-
context "when 'page' does not exist in the request params" do
|
106
|
-
it "finds all the records of the model class" do
|
107
|
-
default.block.call(name)
|
108
|
-
end
|
109
|
-
end
|
231
|
+
context "#make?" do
|
232
|
+
let(:rr) { BasicAssumption::DefaultAssumption::RestfulRails.new }
|
233
|
+
subject { rr.send(:make?) }
|
234
|
+
before { rr.stub(:list? => false, :lookup? => false) }
|
235
|
+
context "when the action is not new or create" do
|
236
|
+
context "when #list? is true" do
|
237
|
+
before { rr.stub(:list? => true) }
|
238
|
+
context "when #lookup? is true" do
|
239
|
+
before { rr.stub(:lookup? => true) }
|
240
|
+
it { should be_false }
|
110
241
|
end
|
111
|
-
context "
|
112
|
-
|
113
|
-
context "when 'page' exists in the request params" do
|
114
|
-
before { params['page'] = '5' }
|
115
|
-
it "paginates the records of the model class" do
|
116
|
-
Model.should_receive(:paginate)
|
117
|
-
default.block.call(name)
|
118
|
-
end
|
119
|
-
context "when 'per_page' exists in the request params" do
|
120
|
-
it "paginates using 'page' and 'per_page' from the params" do
|
121
|
-
params['per_page'] = '10'
|
122
|
-
Model.should_receive(:paginate).with('page' => '5', 'per_page' => '10')
|
123
|
-
default.block.call(name)
|
124
|
-
end
|
125
|
-
end
|
126
|
-
context "when 'per_page' does not exist in the request params" do
|
127
|
-
it "paginates using 'page' from the params and a default 'per_page' of 15" do
|
128
|
-
Model.should_receive(:paginate).with('page' => '5', 'per_page' => '15')
|
129
|
-
default.block.call(name)
|
130
|
-
end
|
131
|
-
end
|
132
|
-
end
|
133
|
-
context "when 'page' does not exist in the request params" do
|
134
|
-
it "finds all the records of the model class" do
|
135
|
-
Model.should_receive(:all)
|
136
|
-
default.block.call(name)
|
137
|
-
end
|
138
|
-
end
|
242
|
+
context "when #lookup? is false" do
|
243
|
+
it { should be_false }
|
139
244
|
end
|
140
245
|
end
|
141
|
-
context "when
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
246
|
+
context "when #list? is false" do
|
247
|
+
context "when #lookup? is true" do
|
248
|
+
before { rr.stub(:lookup? => true) }
|
249
|
+
it { should be_false }
|
250
|
+
end
|
251
|
+
context "when #lookup? is false" do
|
252
|
+
it { should be_true }
|
253
|
+
end
|
254
|
+
end
|
255
|
+
end
|
256
|
+
%w(new create).each do |action|
|
257
|
+
context "when the action is #{action}" do
|
258
|
+
before { rr.stub(:action => action) }
|
259
|
+
context "when #list? is true" do
|
260
|
+
before { rr.stub(:list? => true) }
|
261
|
+
context "when #lookup? is true" do
|
262
|
+
before { rr.stub(:lookup? => true) }
|
263
|
+
it { should be_true }
|
264
|
+
end
|
265
|
+
context "when #lookup? is false" do
|
266
|
+
it { should be_true }
|
148
267
|
end
|
149
268
|
end
|
150
|
-
context "
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
269
|
+
context "when #list? is false" do
|
270
|
+
context "when #lookup? is true" do
|
271
|
+
before { rr.stub(:lookup? => true) }
|
272
|
+
it { should be_true }
|
273
|
+
end
|
274
|
+
context "when #lookup? is false" do
|
275
|
+
it { should be_true }
|
155
276
|
end
|
156
277
|
end
|
157
278
|
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 3
|
8
|
-
-
|
9
|
-
version: 0.3.
|
8
|
+
- 9
|
9
|
+
version: 0.3.9
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Matt Yoho
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-06-
|
17
|
+
date: 2010-06-05 00:00:00 -04:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -59,7 +59,7 @@ dependencies:
|
|
59
59
|
version: 2.3.5
|
60
60
|
type: :development
|
61
61
|
version_requirements: *id003
|
62
|
-
description: "\n Allows a simple declarative idiom for accessing resources in controllers and views\n "
|
62
|
+
description: "\n Allows a simple declarative idiom for accessing resources in controllers and views\n via a well-defined interface that increases testability and reduces shared state.\n "
|
63
63
|
email: mby@mattyoho.com
|
64
64
|
executables: []
|
65
65
|
|