rezource 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1 @@
1
+ *.gem
@@ -0,0 +1,90 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ rezource (0.0.1)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ actionmailer (4.0.0)
10
+ actionpack (= 4.0.0)
11
+ mail (~> 2.5.3)
12
+ actionpack (4.0.0)
13
+ activesupport (= 4.0.0)
14
+ builder (~> 3.1.0)
15
+ erubis (~> 2.7.0)
16
+ rack (~> 1.5.2)
17
+ rack-test (~> 0.6.2)
18
+ activemodel (4.0.0)
19
+ activesupport (= 4.0.0)
20
+ builder (~> 3.1.0)
21
+ activerecord (4.0.0)
22
+ activemodel (= 4.0.0)
23
+ activerecord-deprecated_finders (~> 1.0.2)
24
+ activesupport (= 4.0.0)
25
+ arel (~> 4.0.0)
26
+ activerecord-deprecated_finders (1.0.3)
27
+ activesupport (4.0.0)
28
+ i18n (~> 0.6, >= 0.6.4)
29
+ minitest (~> 4.2)
30
+ multi_json (~> 1.3)
31
+ thread_safe (~> 0.1)
32
+ tzinfo (~> 0.3.37)
33
+ arel (4.0.0)
34
+ atomic (1.1.13)
35
+ builder (3.1.4)
36
+ erubis (2.7.0)
37
+ hike (1.2.3)
38
+ i18n (0.6.5)
39
+ mail (2.5.4)
40
+ mime-types (~> 1.16)
41
+ treetop (~> 1.4.8)
42
+ metaclass (0.0.1)
43
+ mime-types (1.24)
44
+ minitest (4.7.5)
45
+ mocha (0.14.0)
46
+ metaclass (~> 0.0.1)
47
+ multi_json (1.7.9)
48
+ polyglot (0.3.3)
49
+ rack (1.5.2)
50
+ rack-test (0.6.2)
51
+ rack (>= 1.0)
52
+ rails (4.0.0)
53
+ actionmailer (= 4.0.0)
54
+ actionpack (= 4.0.0)
55
+ activerecord (= 4.0.0)
56
+ activesupport (= 4.0.0)
57
+ bundler (>= 1.3.0, < 2.0)
58
+ railties (= 4.0.0)
59
+ sprockets-rails (~> 2.0.0)
60
+ railties (4.0.0)
61
+ actionpack (= 4.0.0)
62
+ activesupport (= 4.0.0)
63
+ rake (>= 0.8.7)
64
+ thor (>= 0.18.1, < 2.0)
65
+ rake (10.1.0)
66
+ sprockets (2.10.0)
67
+ hike (~> 1.2)
68
+ multi_json (~> 1.0)
69
+ rack (~> 1.0)
70
+ tilt (~> 1.1, != 1.3.0)
71
+ sprockets-rails (2.0.0)
72
+ actionpack (>= 3.0)
73
+ activesupport (>= 3.0)
74
+ sprockets (~> 2.8)
75
+ thor (0.18.1)
76
+ thread_safe (0.1.2)
77
+ atomic
78
+ tilt (1.4.1)
79
+ treetop (1.4.15)
80
+ polyglot
81
+ polyglot (>= 0.3.1)
82
+ tzinfo (0.3.37)
83
+
84
+ PLATFORMS
85
+ ruby
86
+
87
+ DEPENDENCIES
88
+ mocha
89
+ rails
90
+ rezource!
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2013 Jan Zimmek
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,4 @@
1
+ rezource
2
+ ========
3
+
4
+ Make Rails restful controller even more simple
@@ -3,9 +3,14 @@ require "active_support/all"
3
3
  module Rezource
4
4
  extend ActiveSupport::Concern
5
5
 
6
+ def self.included(clazz)
7
+ clazz.helper_method :resource, :resources, :parent
8
+ clazz.helper_method :resources_path, :resource_path, :new_resource_path, :edit_resource_path
9
+ end
10
+
6
11
  module ClassMethods
7
12
  def resource(opts={})
8
- @belongs_to = opts[:belongs_to] if opts[:belongs_to]
13
+ @rezource = opts
9
14
  self
10
15
  end
11
16
  end
@@ -42,10 +47,60 @@ module Rezource
42
47
 
43
48
  # -----------
44
49
 
50
+ def new_resource_path(*args)
51
+ send raw_path("new").to_sym, *args
52
+ end
53
+
54
+ def edit_resource_path(*args)
55
+ send raw_path("edit").to_sym, *args
56
+ end
57
+
58
+ def resources_path(*args)
59
+ send raw_path.to_sym, *args
60
+ end
61
+
62
+ def resource_path(*args)
63
+ send raw_path(nil, true).to_sym, *args
64
+ end
65
+
66
+ # def resource_path(*args)
67
+ # send raw_path.to_sym, *args
68
+ # end
69
+
70
+ def raw_path(action=nil, singleton=false)
71
+ parts = []
72
+ parts += [action] if action
73
+ parts += namespace
74
+ parts += [parent_ivar] if parent?
75
+
76
+ parts += if ["new", "edit"].include?(action)
77
+ [resource_ivar]
78
+ elsif singleton
79
+ [resource_ivar]
80
+ elsif resource_options[:singleton]
81
+ [resource_ivar]
82
+ else
83
+ [resources_ivar]
84
+ end
85
+
86
+ parts += ["path"]
87
+ parts.join("_")
88
+ end
89
+
90
+ # -----------
91
+
45
92
  def namespace
46
93
  self.class.name.split("::").slice(0...-1).map(&:underscore).map(&:to_sym)
47
94
  end
48
95
 
96
+ def default_respond_with(*args)
97
+ respond_with *([*namespace, resource] + args)
98
+ end
99
+
100
+ def resource_options
101
+ @resource_options ||= (self.class.instance_variable_get("@rezource") || {})
102
+ end
103
+
49
104
  # -----------
50
105
 
51
106
  def parent?
@@ -53,7 +108,7 @@ module Rezource
53
108
  end
54
109
 
55
110
  def parent_class
56
- belongs_to = self.class.instance_variable_get("@belongs_to")
111
+ belongs_to = resource_options[:belongs_to]
57
112
  belongs_to ? (belongs_to.is_a?(Class) ? belongs_to : belongs_to.to_s.camelcase.constantize) : nil
58
113
  end
59
114
 
@@ -121,7 +176,7 @@ module Rezource
121
176
  def resource_ivar; resource_class.name.split("::").last.underscore; end
122
177
  def resource_ivar_set(value); instance_variable_set("@#{resource_ivar}", value); value; end
123
178
  def resource_ivar_get; instance_variable_get "@#{resource_ivar}"; end
124
- def resource; resource_ivar_get; end
179
+ def resource; resource_ivar_get || find_resource; end
125
180
 
126
181
  def resources_ivar; resource_class.name.split("::").last.pluralize.underscore; end
127
182
  def resources_ivar_set(value); instance_variable_set("@#{resources_ivar}", value); value; end
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "rezource"
3
- s.version = "0.0.1"
3
+ s.version = "0.0.2"
4
4
 
5
5
  s.authors = ["Jan Zimmek"]
6
6
  s.email = %q{jan.zimmek@web.de}
@@ -5,15 +5,19 @@ require "rezource"
5
5
  class SampleParent
6
6
  end
7
7
 
8
- class Sample
8
+ class Sample
9
9
  end
10
10
 
11
11
  class SamplesController
12
+ def self.helper_method(*args)
13
+ end
12
14
  include Rezource
13
15
  end
14
16
 
15
17
  module Namespaced
16
18
  class SamplesController
19
+ def self.helper_method(*args)
20
+ end
17
21
  include Rezource
18
22
  end
19
23
  end
@@ -22,8 +26,8 @@ class RezourceTest < Test::Unit::TestCase
22
26
 
23
27
 
24
28
  def teardown
25
- SamplesController.instance_variable_set "@belongs_to", nil
26
- Namespaced::SamplesController.instance_variable_set "@belongs_to", nil
29
+ SamplesController.instance_variable_set "@rezource", nil
30
+ Namespaced::SamplesController.instance_variable_set "@rezource", nil
27
31
  end
28
32
 
29
33
  def test_namespace
@@ -32,33 +36,105 @@ class RezourceTest < Test::Unit::TestCase
32
36
  end
33
37
 
34
38
 
35
- # ----- actions
39
+ def test_helper_method
40
+ c = SamplesController.new
41
+ assert c.respond_to? :resource
42
+ assert c.respond_to? :resources
43
+ assert c.respond_to? :parent
44
+ assert c.respond_to? :resources_path
45
+ assert c.respond_to? :new_resource_path
46
+ end
47
+
48
+ def test_resource_options
49
+ options = {key: "value"}
50
+ SamplesController.instance_variable_set "@rezource", options
51
+ assert_equal options, SamplesController.new.resource_options
52
+ end
53
+
54
+ # ----- url resources
55
+
56
+
57
+ def test_raw_path
58
+ c = SamplesController.new
59
+ assert_equal "samples_path", c.raw_path
60
+ end
61
+
62
+ def test_raw_path__with_singleton
63
+ c = SamplesController.new
64
+ assert_equal "sample_path", c.raw_path(nil, true)
65
+ end
66
+
67
+ def test_raw_path__with_new_action
68
+ c = SamplesController.new
69
+ assert_equal "new_sample_path", c.raw_path("new")
70
+ end
71
+
72
+ def test_raw_path__with_edit_action
73
+ c = SamplesController.new
74
+ assert_equal "edit_sample_path", c.raw_path("edit")
75
+ end
76
+
77
+ def test_raw_path__with_namespace
78
+ c = Namespaced::SamplesController.new
79
+ assert_equal "namespaced_samples_path", c.raw_path
80
+ end
81
+
82
+ def test_raw_path__with_parent
83
+ c = SamplesController.resource(belongs_to: SampleParent).new
84
+ assert_equal "sample_parent_samples_path", c.raw_path
85
+ end
86
+
87
+ def test_raw_path__with_parent_and_namespace
88
+ c = Namespaced::SamplesController.resource(belongs_to: SampleParent).new
89
+ assert_equal "namespaced_sample_parent_samples_path", c.raw_path
90
+ end
91
+
92
+ def test_resources_path
93
+ c = SamplesController.new
94
+ c.stubs(:samples_path).returns "/samples"
95
+ assert_equal "/samples", c.resources_path
96
+ end
36
97
 
98
+ def test_resource_path
99
+ c = SamplesController.new
100
+ c.stubs(:sample_path).returns "/sample/XYZ"
101
+ assert_equal "/sample/XYZ", c.resource_path
102
+ end
37
103
 
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
104
+ def test_new_resources_path
105
+ c = SamplesController.new
106
+ c.stubs(:new_sample_path).returns "/samples/new"
107
+ assert_equal "/samples/new", c.new_resource_path
108
+ end
43
109
 
44
- # assert_equal c.resources, [1,2,3]
45
- # assert_equal c.resource, nil
46
- # end
110
+ def test_edit_resources_path
111
+ c = SamplesController.new
112
+ c.stubs(:edit_sample_path).returns "/samples/XYZ/edit"
113
+ assert_equal "/samples/XYZ/edit", c.edit_resource_path
114
+ end
47
115
 
48
- # def test_index_with_parent
49
- # c = SamplesController.resource(belongs_to: SampleParent).new
50
- # c.expects(:params).with(:action).returns "index"
51
116
 
52
- # parent = {samples: [{id: 1}]}
117
+ def test_default_respond_with
118
+ c = SamplesController.new
53
119
 
54
- # c.expects(:find_parent).returns(parent)
55
- # c.expects(:respond_with)
56
- # c.index
120
+ c.expects(:namespace).returns [1,2,3]
121
+ c.expects(:resource).returns "joe"
57
122
 
58
- # # assert_equal c.resources, [1,2,3]
59
- # # assert_equal c.resource, nil
60
- # end
123
+ c.expects(:respond_with).with(1, 2, 3, "joe")
61
124
 
125
+ c.default_respond_with
126
+ end
127
+
128
+ def test_default_respond_with__with_args
129
+ c = SamplesController.new
130
+
131
+ c.expects(:namespace).returns [1,2,3]
132
+ c.expects(:resource).returns "joe"
133
+
134
+ c.expects(:respond_with).with(1, 2, 3, "joe", {source: 1})
135
+
136
+ c.default_respond_with source: 1
137
+ end
62
138
 
63
139
  # -----
64
140
 
@@ -208,9 +284,16 @@ class RezourceTest < Test::Unit::TestCase
208
284
  def test_resource
209
285
  c = SamplesController.new
210
286
  c.expects(:resource_ivar_get)
287
+ c.expects(:find_resource)
211
288
  c.resource
212
289
  end
213
290
 
291
+ def test_resource__existing_ivar
292
+ c = SamplesController.new
293
+ c.expects(:resource_ivar_get).returns 123
294
+ assert_equal 123, c.resource
295
+ end
296
+
214
297
  # ------ resources
215
298
 
216
299
  def test_resources_ivar
@@ -253,14 +336,12 @@ class RezourceTest < Test::Unit::TestCase
253
336
  end
254
337
 
255
338
  def test_parent_class__symbol
256
- c = SamplesController.new
257
- c.class.instance_variable_set("@belongs_to", :sample_parent)
339
+ c = SamplesController.resource(belongs_to: :sample_parent).new
258
340
  assert_equal SampleParent, c.parent_class
259
341
  end
260
342
 
261
343
  def test_parent_class__class
262
- c = SamplesController.new
263
- c.class.instance_variable_set("@belongs_to", SampleParent)
344
+ c = SamplesController.resource(belongs_to: SampleParent).new
264
345
  assert_equal SampleParent, c.parent_class
265
346
  end
266
347
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rezource
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-08-17 00:00:00.000000000 Z
12
+ date: 2013-08-18 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Make Rails restful controller even more simple
15
15
  email: jan.zimmek@web.de
@@ -17,7 +17,11 @@ executables: []
17
17
  extensions: []
18
18
  extra_rdoc_files: []
19
19
  files:
20
+ - .gitignore
20
21
  - Gemfile
22
+ - Gemfile.lock
23
+ - LICENSE
24
+ - README.md
21
25
  - Rakefile
22
26
  - lib/rezource.rb
23
27
  - rezource.gemspec