super_resources 1.0.0.rc2 → 1.0.0.rc3

Sign up to get free protection for your applications and to get access to all the features.
@@ -15,17 +15,6 @@ module SuperResources
15
15
 
16
16
  protected
17
17
 
18
- def method_missing(m, *args, &block)
19
- case
20
- when m == resource_params_name
21
- resource
22
- when i = symbols_for_association_chain.index(m)
23
- association_chain[i]
24
- else
25
- super
26
- end
27
- end
28
-
29
18
  def collection
30
19
  memoize_collection { end_of_association_chain }
31
20
  end
@@ -77,5 +66,13 @@ module SuperResources
77
66
  def with_chain(object)
78
67
  association_chain + [ object ]
79
68
  end
69
+
70
+ def method_missing(method, *args, &block)
71
+ if index = symbols_for_association_chain.index(method)
72
+ association_chain[index]
73
+ else
74
+ super
75
+ end
76
+ end
80
77
  end
81
78
  end
@@ -3,7 +3,8 @@ module SuperResources
3
3
  extend ActiveSupport::Concern
4
4
 
5
5
  included do
6
- helper_method :collection, :resource, :resource_class, :parent, :nested?
6
+ helper_method :collection, :collection?, :resource, :resource?,
7
+ :resource_class, :parent, :nested?
7
8
  end
8
9
 
9
10
  protected
@@ -32,6 +33,10 @@ module SuperResources
32
33
  @collection ||= block.call
33
34
  end
34
35
 
36
+ def collection?
37
+ not resource?
38
+ end
39
+
35
40
  def resource
36
41
  memoize_resource { resource_class.send(finder_method, params[:id]) }
37
42
  end
@@ -40,6 +45,10 @@ module SuperResources
40
45
  @resource ||= block.call
41
46
  end
42
47
 
48
+ def resource?
49
+ params[:id].present?
50
+ end
51
+
43
52
  def finder_method
44
53
  :find
45
54
  end
@@ -71,5 +80,9 @@ module SuperResources
71
80
  def destroy_resource
72
81
  resource.destroy
73
82
  end
83
+
84
+ def method_missing(method, *args, &block)
85
+ method == resource_instance_name ? resource : super
86
+ end
74
87
  end
75
88
  end
@@ -8,6 +8,10 @@ module SuperResources
8
8
  Rails.application.routes || request.env['action_dispatch.routes']
9
9
  end
10
10
 
11
+ def named_routes
12
+ routes.named_routes
13
+ end
14
+
11
15
  def router
12
16
  routes.router
13
17
  end
@@ -16,6 +20,10 @@ module SuperResources
16
20
  @route ||= recognize_route(request.path.present? ? request : mock_request)
17
21
  end
18
22
 
23
+ def path_parameters
24
+ request.env['action_dispatch.request.path_parameters'].symbolize_keys
25
+ end
26
+
19
27
  private
20
28
 
21
29
  def recognize_route(request)
@@ -24,10 +32,6 @@ module SuperResources
24
32
  end
25
33
  end
26
34
 
27
- def path_parameters
28
- request.env['action_dispatch.request.path_parameters'].symbolize_keys
29
- end
30
-
31
35
  def mock_request
32
36
  # used when there's no path in +request+. only seems to happen when
33
37
  # testing controller actions
@@ -0,0 +1,23 @@
1
+ module SuperResources
2
+ module Draper
3
+ extend ActiveSupport::Concern
4
+
5
+ protected
6
+
7
+ def decorator_class
8
+ resource_class.decorator_class
9
+ end
10
+
11
+ def resource
12
+ memoize_resource { decorator_class.decorate(super) }
13
+ end
14
+
15
+ def collection
16
+ memoize_collection { decorator_class.decorate_collection(super) }
17
+ end
18
+
19
+ def build_resource
20
+ memoize_resource { decorator_class.decorate(super) }
21
+ end
22
+ end
23
+ end
@@ -3,59 +3,61 @@ module SuperResources
3
3
  extend ActiveSupport::Concern
4
4
 
5
5
  included do
6
- helper_method :collection_path, :resource_path, :new_resource_path,
7
- :edit_resource_path
8
- end
6
+ helpers = %w(collection resource new_resource edit_resource)
7
+
8
+ helpers.each do |helper|
9
+ class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
10
+ def #{helper}_path(*args)
11
+ url_for(hash_for_#{helper}_path(*args))
12
+ end
13
+
14
+ def #{helper}_url(*args)
15
+ url_for(hash_for_#{helper}_url(*args))
16
+ end
17
+
18
+ def hash_for_#{helper}_path(*args)
19
+ hash_for_#{helper}_url(*args).merge(:only_path => true)
20
+ end
21
+ RUBY_EVAL
22
+ end
9
23
 
10
- protected
24
+ helper_methods = helpers.map do |helper|
25
+ [ :"#{helper}_path", :"hash_for_#{helper}_path",
26
+ :"#{helper}_url", :"hash_for_#{helper}_url" ]
27
+ end
11
28
 
12
- def full_association_chain_symbols
13
- symbols_for_association_chain + [resource_instance_name]
29
+ helper_method(*(helper_methods.flatten))
14
30
  end
15
31
 
16
- def collection_route
17
- chain = symbols_for_association_chain + [resource_collection_name]
18
- :"#{chain.join('_')}_url"
19
- end
32
+ protected
20
33
 
21
- def resource_route
22
- :"#{full_association_chain_symbols.join('_')}_url"
23
- end
34
+ # core stuff ..............................................................
24
35
 
25
- def new_resource_route
26
- :"new_#{resource_route}"
36
+ def route_hash
37
+ path_parameters.except(:id, :action)
27
38
  end
28
39
 
29
- def edit_resource_route
30
- :"edit_#{resource_route}"
31
- end
40
+ # route helpers ...........................................................
32
41
 
33
- def collection_url
34
- send(collection_route, *association_chain)
42
+ def hash_for_collection_url(options={})
43
+ route_hash.merge(options).merge(:action => 'index')
35
44
  end
36
45
 
37
- alias_method :collection_path, :collection_url
38
-
39
- def resource_url(object = resource)
40
- if object.persisted?
41
- send(resource_route, *(association_chain + [object]))
42
- else
43
- collection_url # probably a new action
44
- end
46
+ def hash_for_resource_url(*args)
47
+ options = args.extract_options!
48
+ route_hash.merge(options)
49
+ .merge(:action => 'show', :id => args.first || resource)
45
50
  end
46
51
 
47
- alias_method :resource_path, :resource_url
48
-
49
- def new_resource_url
50
- send(new_resource_route, *association_chain)
52
+ def hash_for_new_resource_url(options={})
53
+ route_hash.merge(options).merge(:action => 'new')
51
54
  end
52
55
 
53
- alias_method :new_resource_path, :new_resource_url
54
-
55
- def edit_resource_url(object = resource)
56
- send(edit_resource_route, *(association_chain + [object]))
56
+ def hash_for_edit_resource_url(*args)
57
+ options = args.extract_options!
58
+ route_hash.merge(options)
59
+ .merge(:action => 'edit', :id => args.first || resource)
57
60
  end
58
-
59
- alias_method :edit_resource_path, :edit_resource_url
60
61
  end
62
+
61
63
  end
@@ -1,3 +1,3 @@
1
1
  module SuperResources
2
- VERSION = "1.0.0.rc2"
2
+ VERSION = "1.0.0.rc3"
3
3
  end
@@ -10,8 +10,9 @@ module SuperResources
10
10
  autoload :Version
11
11
 
12
12
  autoload_under 'support' do
13
- autoload :HasScope
14
13
  autoload :Cancan
14
+ autoload :Draper
15
+ autoload :HasScope
15
16
  end
16
17
  end
17
18
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: super_resources
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0.rc2
4
+ version: 1.0.0.rc3
5
5
  prerelease: 6
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2013-02-11 00:00:00.000000000 Z
13
+ date: 2013-03-07 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: railties
@@ -51,6 +51,7 @@ files:
51
51
  - lib/super_resources/resources.rb
52
52
  - lib/super_resources/routing.rb
53
53
  - lib/super_resources/support/cancan.rb
54
+ - lib/super_resources/support/draper.rb
54
55
  - lib/super_resources/support/has_scope.rb
55
56
  - lib/super_resources/url_helpers.rb
56
57
  - lib/super_resources/version.rb