basic_assumption 0.5.1 → 0.5.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,112 +0,0 @@
1
- require 'basic_assumption/default_assumption/rails'
2
-
3
- module BasicAssumption
4
- module DefaultAssumption
5
- # Restful default behavior in the context of Rails
6
- class RestfulRails < BasicAssumption::DefaultAssumption::Rails
7
- attr_reader :action,
8
- :page,
9
- :per_page,
10
- :resource_attributes #:nodoc:
11
-
12
- def initialize(name = nil, context={}, params = {}) #:nodoc:
13
- super
14
- @action = params['action']
15
- @resource_attributes = params[singular_name]
16
-
17
- if @page = params[:page]
18
- @per_page = params[:per_page]
19
- end
20
- end
21
-
22
- # Returns a block that will attempt to do the correct thing depending
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
27
- # value in the parameters. If the action is 'new' or 'create', +assume+
28
- # will instantiate a new instance of the model class, passing in the
29
- # values it finds in the +params+ hash with for a key of the name passed
30
- # to +assume+. For example:
31
- #
32
- # class WidgetController < ApplicationController
33
- # default_assumption :restful_rails
34
- # assume :widget
35
- #
36
- # def create
37
- # widget.save! # widget is: Widget.new(params[:widget])
38
- # end
39
- # end
40
- #
41
- # Note the object will have been instantiated but not saved, destroyed,
42
- # etc.
43
- #
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. Otherwise, it returns all
49
- # # records for the model.
50
- #
51
- # It is possible to specify an alternative model name:
52
- #
53
- # class WidgetController < ApplicationController
54
- # assume :sprocket, :as => :widget
55
- # end
56
- #
57
- # This will create a +sprocket+ method in your actions and view
58
- # that will use the Widget model for its lookup.
59
- def block
60
- super
61
- end
62
-
63
- def result #:nodoc:
64
- if list?
65
- list
66
- elsif make?
67
- model_class.new(resource_attributes)
68
- elsif lookup?
69
- model_class.find(lookup_id)
70
- end
71
- end
72
-
73
- protected
74
-
75
- def list #:nodoc:
76
- if page?
77
- model_class.paginate(:page => page, :per_page => per_page)
78
- else
79
- model_class.all
80
- end
81
- end
82
-
83
- def list? #:nodoc:
84
- plural_name.eql?(name)
85
- end
86
-
87
- def lookup_id #:nodoc:
88
- params['id']
89
- end
90
-
91
- def lookup? #:nodoc:
92
- lookup_id.present? && !list?
93
- end
94
-
95
- def make? #:nodoc:
96
- %w(new create).include?(action) || !(lookup? || list?)
97
- end
98
-
99
- def page? #:nodoc:
100
- page.present? && model_class.respond_to?(:paginate)
101
- end
102
-
103
- def plural_name #:nodoc:
104
- name.pluralize
105
- end
106
-
107
- def singular_name #:nodoc:
108
- name.singularize
109
- end
110
- end
111
- end
112
- end
@@ -1,280 +0,0 @@
1
- require 'spec_helper'
2
- require 'active_support'
3
- require 'basic_assumption/default_assumption/restful_rails'
4
-
5
- class Model
6
- attr_accessor :age, :color
7
- def initialize(hash = {})
8
- hash.each do |k, v|
9
- self.send("#{k}=", v)
10
- end
11
- end
12
- end
13
-
14
- describe BasicAssumption::DefaultAssumption::RestfulRails do
15
-
16
- context "#block" do
17
- let(:default) { BasicAssumption::DefaultAssumption::RestfulRails.new(:model, {}, params) }
18
-
19
- before(:each) do
20
- Model.stub!(:find)
21
- default.stub!(:params).and_return(params)
22
- end
23
-
24
- context "when the name given to assume is plural" do
25
- let(:name) { :models }
26
-
27
- context "without pagination" do
28
- %w(create destroy edit index new show update).each do |action|
29
- let(:params) { {'action' => action} }
30
-
31
- context "when there is an id in the params" do
32
- before { params['id'] = 123 }
33
- context "when action is #{action}" do
34
- before { Model.should_receive(:all) }
35
- context "when :page exists in the request params" do
36
- before { params[:page] = '5' }
37
- it "finds all the records of the model class" do
38
- default.block.call(name, {})
39
- end
40
- end
41
- context "when :page does not exist in the request params" do
42
- it "finds all the records of the model class" do
43
- default.block.call(name, {})
44
- end
45
- end
46
- end
47
- end
48
-
49
- context "when there is not an id in the params" do
50
- context "when action is #{action}" do
51
- before { Model.should_receive(:all) }
52
- context "when :page exists in the request params" do
53
- before { params[:page] = '5' }
54
- it "finds all the records of the model class" do
55
- default.block.call(name, {})
56
- end
57
- end
58
- context "when :page does not exist in the request params" do
59
- it "finds all the records of the model class" do
60
- default.block.call(name, {})
61
- end
62
- end
63
- end
64
- end
65
- end
66
- end
67
-
68
- %w(create destroy edit index new show update).each do |action|
69
- let(:params) { {'action' => action} }
70
-
71
- context "with pagination" do
72
- context "when there is an id in the params" do
73
- before { params['id'] = 123 }
74
- context "when action is #{action}" do
75
- before { Model.stub! :paginate }
76
- context "when :page exists in the request params" do
77
- before { params[:page] = '5' }
78
- it "paginates the records of the model class" do
79
- Model.should_receive(:paginate)
80
- default.block.call(name, {})
81
- end
82
- context "when :per_page exists in the request params" do
83
- it "paginates using :page and :per_page from the params" do
84
- params[:per_page] = '10'
85
- Model.should_receive(:paginate).with(:page => '5', :per_page => '10')
86
- default.block.call(name, {})
87
- end
88
- end
89
- context "when :per_page does not exist in the request params" do
90
- it "paginates using :page from the params" do
91
- Model.should_receive(:paginate).with(:page => '5', :per_page => nil)
92
- default.block.call(name, {})
93
- end
94
- end
95
- end
96
- context "when :page does not exist in the request params" do
97
- it "finds all the records of the model class" do
98
- Model.should_receive(:all)
99
- default.block.call(name, {})
100
- end
101
- end
102
- end
103
- end
104
-
105
- context "when there is not an id in the params" do
106
- context "when action is #{action}" do
107
- before { Model.stub! :paginate }
108
- context "when :page exists in the request params" do
109
- before { params[:page] = '5' }
110
- it "paginates the records of the model class" do
111
- Model.should_receive(:paginate)
112
- default.block.call(name, {})
113
- end
114
- context "when :per_page exists in the request params" do
115
- it "paginates using :page and :per_page from the params" do
116
- params[:per_page] = '10'
117
- Model.should_receive(:paginate).with(:page => '5', :per_page => '10')
118
- default.block.call(name, {})
119
- end
120
- end
121
- context "when :per_page does not exist in the request params" do
122
- it "paginates using :page from the params" do
123
- Model.should_receive(:paginate).with(:page => '5', :per_page => nil)
124
- default.block.call(name, {})
125
- end
126
- end
127
- end
128
- context "when :page does not exist in the request params" do
129
- it "finds all the records of the model class" do
130
- Model.should_receive(:all)
131
- default.block.call(name, {})
132
- end
133
- end
134
- end
135
- end
136
- end
137
- end
138
- end
139
-
140
- context "when the name given to assume is singular" do
141
- let(:name) { :model }
142
- context "in the index action" do
143
- let(:params) { { 'action' => 'index' } }
144
- context "and there is an id in params" do
145
- before { params['id'] = 1 }
146
- it "attempts to find a model instance based off the given name" do
147
- Model.should_receive(:find).with(1).and_return(name)
148
- default.block.call(name, {}).should eql(name)
149
- end
150
- end
151
- context "and there is no id in params" do
152
- before { params['model'] = :initializers }
153
- it "creates a new model instance and passes in appropriate params" do
154
- Model.should_receive(:new).with(:initializers).and_return(name)
155
- default.block.call(name, {}).should eql(name)
156
- end
157
- end
158
- end
159
-
160
- context "in the show action" do
161
- let(:params) { { 'id' => 42, 'action' => 'show' } }
162
-
163
- it "attempts to find a model instance based off the given name" do
164
- Model.should_receive(:find).with(42).and_return(name)
165
- default.block.call(name, {}).should eql(:model)
166
- end
167
- end
168
-
169
- context "in the edit action" do
170
- let(:params) { { 'id' => 42, 'action' => 'edit' } }
171
-
172
- it "attempts to find a model instance based off the given name" do
173
- Model.should_receive(:find).with(42).and_return(name)
174
- default.block.call(name, {}).should eql(:model)
175
- end
176
- end
177
-
178
- context "in the update action" do
179
- let(:params) do
180
- { 'id' => 42 }
181
- end
182
-
183
- it "attempts to find a model instance based off the given name" do
184
- Model.should_receive(:find).with(42).and_return(name)
185
- default.block.call(name, {}).should eql(:model)
186
- end
187
- end
188
-
189
- context "in the destroy action" do
190
- let(:params) { { 'id' => 42, 'action' => 'destroy' } }
191
-
192
- it "attempts to find a model instance based off the given name" do
193
- Model.should_receive(:find).with(42).and_return(name)
194
- default.block.call(name, {}).should eql(:model)
195
- end
196
- end
197
-
198
- context "in the create action" do
199
- let(:params) do
200
- {
201
- 'action' => 'create',
202
- 'model' => { 'age' => 27, 'color' => 'blue' }
203
- }
204
- end
205
-
206
- context "the model instance" do
207
- subject { default.block.call(name, {}) }
208
- its(:age) { should be(27) }
209
- its(:color) { should eql('blue') }
210
- end
211
- end
212
-
213
- context "in the new action" do
214
- let(:params) do
215
- {
216
- 'action' => 'new',
217
- 'model' => { 'age' => 27, 'color' => 'blue' }
218
- }
219
- end
220
-
221
- context "the model instance" do
222
- subject { default.block.call(name, {}) }
223
- its(:age) { should be(27) }
224
- its(:color) { should eql('blue') }
225
- end
226
- end
227
- end
228
- end
229
-
230
- context "#make?" do
231
- let(:rr) { BasicAssumption::DefaultAssumption::RestfulRails.new }
232
- subject { rr.send(:make?) }
233
- before { rr.stub(:list? => false, :lookup? => false) }
234
- context "when the action is not new or create" do
235
- context "when #list? is true" do
236
- before { rr.stub(:list? => true) }
237
- context "when #lookup? is true" do
238
- before { rr.stub(:lookup? => true) }
239
- it { should be_false }
240
- end
241
- context "when #lookup? is false" do
242
- it { should be_false }
243
- end
244
- end
245
- context "when #list? is false" do
246
- context "when #lookup? is true" do
247
- before { rr.stub(:lookup? => true) }
248
- it { should be_false }
249
- end
250
- context "when #lookup? is false" do
251
- it { should be_true }
252
- end
253
- end
254
- end
255
- %w(new create).each do |action|
256
- context "when the action is #{action}" do
257
- before { rr.stub(:action => action) }
258
- context "when #list? is true" do
259
- before { rr.stub(:list? => true) }
260
- context "when #lookup? is true" do
261
- before { rr.stub(:lookup? => true) }
262
- it { should be_true }
263
- end
264
- context "when #lookup? is false" do
265
- it { should be_true }
266
- end
267
- end
268
- context "when #list? is false" do
269
- context "when #lookup? is true" do
270
- before { rr.stub(:lookup? => true) }
271
- it { should be_true }
272
- end
273
- context "when #lookup? is false" do
274
- it { should be_true }
275
- end
276
- end
277
- end
278
- end
279
- end
280
- end