rezource 0.0.1

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/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem 'rails'
6
+ gem "mocha"
@@ -0,0 +1,13 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rake/testtask'
5
+
6
+ desc 'Run tests for Rezource.'
7
+ Rake::TestTask.new(:test) do |t|
8
+ t.pattern = 'test/**/test_*.rb'
9
+ t.verbose = true
10
+ end
11
+
12
+ # Make test the default task.
13
+ task :default => :test
@@ -0,0 +1,173 @@
1
+ require "active_support/all"
2
+
3
+ module Rezource
4
+ extend ActiveSupport::Concern
5
+
6
+ module ClassMethods
7
+ def resource(opts={})
8
+ @belongs_to = opts[:belongs_to] if opts[:belongs_to]
9
+ self
10
+ end
11
+ end
12
+
13
+ # ----------
14
+
15
+ def index
16
+ respond_with *namespace, find_resources
17
+ end
18
+
19
+ def show
20
+ respond_with *namespace, find_resource
21
+ end
22
+
23
+ def new
24
+ respond_with *namespace, build_resource
25
+ end
26
+
27
+ def create
28
+ resource_create build_resource(permitted_params_create)
29
+ end
30
+
31
+ def edit
32
+ respond_with *namespace, find_resource
33
+ end
34
+
35
+ def update
36
+ resource_update find_resource, permitted_params_update
37
+ end
38
+
39
+ def destroy
40
+ resource_destroy find_resource
41
+ end
42
+
43
+ # -----------
44
+
45
+ def namespace
46
+ self.class.name.split("::").slice(0...-1).map(&:underscore).map(&:to_sym)
47
+ end
48
+
49
+ # -----------
50
+
51
+ def parent?
52
+ parent_class
53
+ end
54
+
55
+ def parent_class
56
+ belongs_to = self.class.instance_variable_get("@belongs_to")
57
+ belongs_to ? (belongs_to.is_a?(Class) ? belongs_to : belongs_to.to_s.camelcase.constantize) : nil
58
+ end
59
+
60
+ def find_parent
61
+ parent_ivar_get || parent_ivar_set(find_parent_scope.first)
62
+ end
63
+
64
+ def find_parent_scope
65
+ parent_class.where(id: params[:"#{parent_ivar}_id"])
66
+ end
67
+
68
+ def parent_ivar; parent_class.name.split("::").last.underscore; end
69
+ def parent_ivar_set(value); instance_variable_set("@#{parent_ivar}", value); value; end
70
+ def parent_ivar_get; instance_variable_get "@#{parent_ivar}"; end
71
+ def parent; parent_ivar_get; end
72
+
73
+
74
+ # -----------
75
+
76
+ def resource_class
77
+ self.class.name.split("::").last.gsub(/Controller$/, "").singularize.constantize
78
+ end
79
+
80
+ def build_resource(attributes={})
81
+ resource_ivar_get || resource_ivar_set(send(parent? ? :build_resource_with_parent : :build_resource_without_parent, attributes))
82
+ end
83
+
84
+ def build_resource_with_parent(attributes)
85
+ find_parent.send(resources_ivar.to_sym).build(attributes)
86
+ end
87
+
88
+ def build_resource_without_parent(attributes)
89
+ resource_class.new(attributes)
90
+ end
91
+
92
+ def find_resources
93
+ resources_ivar_get || resources_ivar_set(find_resources_scope.to_a)
94
+ end
95
+
96
+ def find_resources_scope
97
+ parent? ? find_parent.send(resources_ivar.to_sym) : resource_class
98
+ end
99
+
100
+ def find_resource
101
+ return resource_ivar_get if resource_ivar_get
102
+
103
+ resource = find_resource_scope.first
104
+ reverse_set_parent_from_resource resource if parent?
105
+
106
+ resource_ivar_set resource
107
+ end
108
+
109
+ def find_resource_scope
110
+ resource_class.where(id: params[:id])
111
+ end
112
+
113
+ def reverse_set_parent_from_resource(resource)
114
+ parent_ivar_set resource.send(parent_ivar.to_sym) if resource.respond_to?(parent_ivar.to_sym)
115
+ end
116
+
117
+ # -----------
118
+
119
+
120
+
121
+ def resource_ivar; resource_class.name.split("::").last.underscore; end
122
+ def resource_ivar_set(value); instance_variable_set("@#{resource_ivar}", value); value; end
123
+ def resource_ivar_get; instance_variable_get "@#{resource_ivar}"; end
124
+ def resource; resource_ivar_get; end
125
+
126
+ def resources_ivar; resource_class.name.split("::").last.pluralize.underscore; end
127
+ def resources_ivar_set(value); instance_variable_set("@#{resources_ivar}", value); value; end
128
+ def resources_ivar_get; instance_variable_get "@#{resources_ivar}"; end
129
+ def resources; resources_ivar_get; end
130
+
131
+ def before_create(resource); end
132
+ def before_update(resource); end
133
+ def before_destroy(resource); end
134
+
135
+ def after_create(resource); end
136
+ def after_update(resource); end
137
+ def after_destroy(resource); end
138
+
139
+ def resource_create(resource)
140
+ before_create resource
141
+ resource.save if resource.valid?
142
+ after_create resource
143
+
144
+ respond_with *namespace, resource
145
+ end
146
+
147
+ def resource_update(resource, attributes={})
148
+ before_update resource
149
+ resource.update_attributes attributes if resource.valid?
150
+ after_update resource
151
+
152
+ respond_with *namespace, resource
153
+ end
154
+
155
+ def resource_destroy(resource)
156
+ before_destroy resource
157
+ resource.destroy
158
+ after_destroy resource
159
+
160
+ respond_with *namespace, resource
161
+ end
162
+
163
+
164
+ def permitted_params_create; permitted_params; end
165
+ def permitted_params_update; permitted_params; end
166
+ def permitted_params; raise RuntimeError.new("implement me"); end
167
+
168
+
169
+ def interpolation_options
170
+ {resource_name: resource.respond_to?(:name) ? resource.send(:name) : "NAME"}
171
+ end
172
+
173
+ end
@@ -0,0 +1,16 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "rezource"
3
+ s.version = "0.0.1"
4
+
5
+ s.authors = ["Jan Zimmek"]
6
+ s.email = %q{jan.zimmek@web.de}
7
+
8
+ s.summary = %q{Make Rails restful controller even more simple}
9
+ s.description = %q{Make Rails restful controller even more simple}
10
+
11
+
12
+ s.files = `git ls-files`.split("\n")
13
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
14
+
15
+ s.require_paths = ["lib"]
16
+ end
@@ -0,0 +1,401 @@
1
+ require "test/unit"
2
+ require "mocha/setup"
3
+ require "rezource"
4
+
5
+ class SampleParent
6
+ end
7
+
8
+ class Sample
9
+ end
10
+
11
+ class SamplesController
12
+ include Rezource
13
+ end
14
+
15
+ module Namespaced
16
+ class SamplesController
17
+ include Rezource
18
+ end
19
+ end
20
+
21
+ class RezourceTest < Test::Unit::TestCase
22
+
23
+
24
+ def teardown
25
+ SamplesController.instance_variable_set "@belongs_to", nil
26
+ Namespaced::SamplesController.instance_variable_set "@belongs_to", nil
27
+ end
28
+
29
+ def test_namespace
30
+ assert_equal [], SamplesController.new.namespace
31
+ assert_equal [:namespaced], Namespaced::SamplesController.new.namespace
32
+ end
33
+
34
+
35
+ # ----- actions
36
+
37
+
38
+ # def test_index_no_parent
39
+ # c = SamplesController.new
40
+ # c.expects(:find_resources).returns [1,2,3]
41
+ # c.expects(:respond_with)
42
+ # c.index
43
+
44
+ # assert_equal c.resources, [1,2,3]
45
+ # assert_equal c.resource, nil
46
+ # end
47
+
48
+ # def test_index_with_parent
49
+ # c = SamplesController.resource(belongs_to: SampleParent).new
50
+ # c.expects(:params).with(:action).returns "index"
51
+
52
+ # parent = {samples: [{id: 1}]}
53
+
54
+ # c.expects(:find_parent).returns(parent)
55
+ # c.expects(:respond_with)
56
+ # c.index
57
+
58
+ # # assert_equal c.resources, [1,2,3]
59
+ # # assert_equal c.resource, nil
60
+ # end
61
+
62
+
63
+ # -----
64
+
65
+ def test_resource_class
66
+ assert_equal Sample, SamplesController.new.resource_class
67
+ assert_equal Sample, Namespaced::SamplesController.new.resource_class
68
+ end
69
+
70
+ def test_build_resource__with_parent
71
+ c = SamplesController.new
72
+ attributes = {name: "joe"}
73
+ c.expects(:build_resource_without_parent).with(attributes)
74
+ c.build_resource attributes
75
+ end
76
+
77
+ def test_build_resource__without_parent
78
+ c = SamplesController.resource(belongs_to: SampleParent).new
79
+ attributes = {name: "joe"}
80
+ c.expects(:build_resource_with_parent).with(attributes)
81
+ c.build_resource attributes
82
+ end
83
+
84
+ def test_build_resource_with_parent
85
+ c = SamplesController.resource(belongs_to: SampleParent).new
86
+ attributes = {name: "joe"}
87
+
88
+ association = mock()
89
+ association.expects(:build).with(attributes)
90
+
91
+ parent = mock()
92
+ parent.expects(:send).returns association
93
+
94
+ c.expects(:find_parent).returns parent
95
+ c.build_resource_with_parent attributes
96
+ end
97
+
98
+ def test_build_resource_without_parent
99
+ c = SamplesController.new
100
+ attributes = {name: "joe"}
101
+ Sample.stubs(:new).with(attributes)
102
+ c.build_resource_without_parent attributes
103
+ end
104
+
105
+ # --- find resources
106
+
107
+ def test_find_resources
108
+ c = SamplesController.new
109
+
110
+ scope = mock();
111
+ scope.expects(:to_a).returns 123
112
+
113
+ c.expects(:find_resources_scope).returns scope
114
+ c.find_resources
115
+
116
+ assert_equal c.resources, 123
117
+ end
118
+
119
+ def test_find_resources__without_parent
120
+ c = SamplesController.new
121
+
122
+ scope = mock()
123
+ scope.expects(:to_a)
124
+
125
+ c.expects(:find_resources_scope).returns scope
126
+ c.find_resources
127
+ end
128
+
129
+ def test_find_resources__with_parent
130
+ c = SamplesController.resource(belongs_to: SampleParent).new
131
+
132
+ association = mock()
133
+ association.expects(:to_a)
134
+
135
+ parent = mock();
136
+ parent.expects(:send).returns(association)
137
+
138
+ c.expects(:find_parent).returns(parent)
139
+ c.find_resources
140
+ end
141
+
142
+ def test_find_resources_scope
143
+ c = SamplesController.new
144
+ assert_same Sample, c.find_resources_scope
145
+ end
146
+
147
+
148
+ # --- find resource
149
+
150
+ def test_find_resource
151
+ # TODO
152
+ end
153
+
154
+ def test_find_resource_scope
155
+ c = SamplesController.new
156
+
157
+ c.expects(:params).returns({id: 123})
158
+
159
+ scope = mock()
160
+ scope.expects(:where).with({id: 123})
161
+
162
+ c.expects(:resource_class).returns scope
163
+
164
+ c.find_resource_scope
165
+ end
166
+
167
+ def test_reverse_set_parent_from_resource__with_reverse_association
168
+ c = SamplesController.resource(belongs_to: SampleParent).new
169
+
170
+ resource = mock()
171
+ resource.expects(:respond_to?).with(:sample_parent).returns true
172
+ resource.expects(:sample_parent).returns 123
173
+
174
+ c.expects(:parent_ivar_set).with(123)
175
+ c.reverse_set_parent_from_resource resource
176
+ end
177
+
178
+ def test_reverse_set_parent_from_resource__without_reverse_association
179
+ c = SamplesController.resource(belongs_to: SampleParent).new
180
+
181
+ resource = mock()
182
+ resource.expects(:respond_to?).with(:sample_parent).returns false
183
+ c.reverse_set_parent_from_resource resource
184
+ end
185
+
186
+
187
+ # ------ resource
188
+
189
+ def test_resource_ivar
190
+ assert_equal "sample", SamplesController.new.resource_ivar
191
+ assert_equal "sample", Namespaced::SamplesController.new.resource_ivar
192
+ end
193
+
194
+ def test_resource_ivar_set
195
+ c = SamplesController.new
196
+ res = c.resource_ivar_set 123
197
+ assert_equal 123, res
198
+ assert_equal 123, c.instance_variable_get("@sample")
199
+ end
200
+
201
+ def test_resource_ivar_get
202
+ c = SamplesController.new
203
+ c.instance_variable_set "@sample", 123
204
+
205
+ assert_equal 123, c.resource_ivar_get
206
+ end
207
+
208
+ def test_resource
209
+ c = SamplesController.new
210
+ c.expects(:resource_ivar_get)
211
+ c.resource
212
+ end
213
+
214
+ # ------ resources
215
+
216
+ def test_resources_ivar
217
+ assert_equal "samples", SamplesController.new.resources_ivar
218
+ assert_equal "samples", Namespaced::SamplesController.new.resources_ivar
219
+ end
220
+
221
+ def test_resources_ivar_set
222
+ c = SamplesController.new
223
+ res = c.resources_ivar_set 123
224
+ assert_equal 123, res
225
+ assert_equal 123, c.instance_variable_get("@samples")
226
+ end
227
+
228
+ def test_resources_ivar_get
229
+ c = SamplesController.new
230
+ c.instance_variable_set "@samples", 123
231
+
232
+ assert_equal 123, c.resources_ivar_get
233
+ end
234
+
235
+ def test_resources
236
+ c = SamplesController.new
237
+ c.expects(:resources_ivar_get)
238
+ c.resources
239
+ end
240
+
241
+
242
+ # ---- parent
243
+
244
+ def test_parent?
245
+ c = SamplesController.new
246
+ c.expects(:parent_class)
247
+ c.parent?
248
+ end
249
+
250
+ def test_parent_class__none
251
+ c = SamplesController.new
252
+ assert_nil c.parent_class
253
+ end
254
+
255
+ def test_parent_class__symbol
256
+ c = SamplesController.new
257
+ c.class.instance_variable_set("@belongs_to", :sample_parent)
258
+ assert_equal SampleParent, c.parent_class
259
+ end
260
+
261
+ def test_parent_class__class
262
+ c = SamplesController.new
263
+ c.class.instance_variable_set("@belongs_to", SampleParent)
264
+ assert_equal SampleParent, c.parent_class
265
+ end
266
+
267
+ def find_parent
268
+ c = SamplesController.resource(belongs_to: SampleParent).new
269
+
270
+ scope = mock()
271
+ scope.expects(:first).returns 123
272
+
273
+ c.expects(:find_parent_scope).returns scope
274
+ c.expects(:parent_ivar_set).with(123)
275
+
276
+ c.find_parent
277
+ end
278
+
279
+ def test_parent_ivar
280
+ c = SamplesController.new
281
+ c.expects(:parent_class).returns SampleParent
282
+ assert_equal "sample_parent", c.parent_ivar
283
+ end
284
+
285
+ def test_parent_ivar_set
286
+ c = SamplesController.resource(belongs_to: SampleParent).new
287
+ res = c.parent_ivar_set(123)
288
+ assert_equal 123, res
289
+ assert_equal 123, c.instance_variable_get("@sample_parent")
290
+ end
291
+
292
+ def test_parent_ivar_get
293
+ c = SamplesController.resource(belongs_to: SampleParent).new
294
+ c.instance_variable_set "@sample_parent", 123
295
+ assert_equal 123, c.parent_ivar_get
296
+ end
297
+
298
+ def parent
299
+ c = SamplesController.resource(belongs_to: SampleParent).new
300
+ c.expects(:parent_ivar_get)
301
+ c.parent
302
+ end
303
+
304
+ # ---- create, update, destroy
305
+
306
+ def test_resource_create__valid
307
+ c = SamplesController.new
308
+ m = mock()
309
+
310
+ c.expects(:before_create).with(m)
311
+ m.expects(:valid?).returns true
312
+ m.expects(:save)
313
+ c.expects(:after_create).with(m)
314
+ c.expects(:respond_with).with(*(c.namespace + [m]))
315
+
316
+ c.resource_create m
317
+ end
318
+
319
+ def test_resource_create__invalid
320
+ c = SamplesController.new
321
+ m = mock()
322
+
323
+ c.expects(:before_create).with(m)
324
+ m.expects(:valid?).returns false
325
+ c.expects(:after_create).with(m)
326
+ c.expects(:respond_with).with(*(c.namespace + [m]))
327
+
328
+ c.resource_create m
329
+ end
330
+
331
+ def test_resource_update__valid
332
+ c = SamplesController.new
333
+ attributes = {name: "joe"}
334
+ m = mock()
335
+
336
+ c.expects(:before_update).with(m)
337
+ m.expects(:valid?).returns true
338
+ m.expects(:update_attributes).with(attributes)
339
+ c.expects(:after_update).with(m)
340
+ c.expects(:respond_with).with(*(c.namespace + [m]))
341
+
342
+ c.resource_update m, attributes
343
+ end
344
+
345
+ def test_resource_update__invalid
346
+ c = SamplesController.new
347
+ attributes = {name: "joe"}
348
+ m = mock()
349
+
350
+ c.expects(:before_update).with(m)
351
+ m.expects(:valid?).returns false
352
+ c.expects(:after_update).with(m)
353
+ c.expects(:respond_with).with(*(c.namespace + [m]))
354
+
355
+ c.resource_update m, attributes
356
+ end
357
+
358
+ def test_resource_destroy
359
+ c = SamplesController.new
360
+ m = mock()
361
+
362
+ c.expects(:before_destroy).with(m)
363
+ m.expects(:destroy)
364
+ c.expects(:after_destroy).with(m)
365
+ c.expects(:respond_with).with(*(c.namespace + [m]))
366
+
367
+ c.resource_destroy m
368
+ end
369
+
370
+
371
+
372
+ # -----
373
+
374
+
375
+ def test_permitted_params_create
376
+ c = SamplesController.new
377
+ c.expects(:permitted_params)
378
+ c.permitted_params_create
379
+ end
380
+
381
+ def test_permitted_params_update
382
+ c = SamplesController.new
383
+ c.expects(:permitted_params)
384
+ c.permitted_params_update
385
+ end
386
+
387
+ def test_permitted_params
388
+ c = SamplesController.new
389
+
390
+ assert_raise RuntimeError do
391
+ c.permitted_params
392
+ end
393
+ end
394
+
395
+ # ----
396
+
397
+ def test_interpolation_options
398
+ # TODO
399
+ end
400
+
401
+ end
metadata ADDED
@@ -0,0 +1,50 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rezource
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jan Zimmek
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-08-17 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Make Rails restful controller even more simple
15
+ email: jan.zimmek@web.de
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - Gemfile
21
+ - Rakefile
22
+ - lib/rezource.rb
23
+ - rezource.gemspec
24
+ - test/test_rezource.rb
25
+ homepage:
26
+ licenses: []
27
+ post_install_message:
28
+ rdoc_options: []
29
+ require_paths:
30
+ - lib
31
+ required_ruby_version: !ruby/object:Gem::Requirement
32
+ none: false
33
+ requirements:
34
+ - - ! '>='
35
+ - !ruby/object:Gem::Version
36
+ version: '0'
37
+ required_rubygems_version: !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ! '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ requirements: []
44
+ rubyforge_project:
45
+ rubygems_version: 1.8.23
46
+ signing_key:
47
+ specification_version: 3
48
+ summary: Make Rails restful controller even more simple
49
+ test_files:
50
+ - test/test_rezource.rb