inherited_resources_helpers 0.0.7 → 0.0.8

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.
@@ -2,17 +2,18 @@ require 'inherited_resources'
2
2
 
3
3
  module InheritedResources
4
4
  module Helpers
5
- autoload :Accessors, 'inherited_resources/helpers/accessors'
6
- autoload :LinkTo, 'inherited_resources/helpers/link_to'
7
- autoload :Resources, 'inherited_resources/helpers/resources'
8
- autoload :UrlFor, 'inherited_resources/helpers/url_for'
5
+ autoload :Accessors, 'inherited_resources/helpers/accessors'
6
+ autoload :LinkTo, 'inherited_resources/helpers/link_to'
7
+ autoload :Resources, 'inherited_resources/helpers/resources'
8
+ autoload :ResourcesUrlFor, 'inherited_resources/helpers/resources_url_for'
9
+ autoload :UrlFor, 'inherited_resources/helpers/url_for'
9
10
  end
10
11
 
11
12
  Base.class_eval do
12
13
  class << self
13
14
  def inherited_with_helpers(base)
14
15
  base.send :include, Helpers::Resources
15
- base.send :include, Helpers::UrlFor
16
+ base.send :include, Helpers::ResourcesUrlFor
16
17
  base.send :include, Helpers::Accessors
17
18
  inherited_without_helpers(base)
18
19
  end
@@ -1,9 +1,32 @@
1
1
  module InheritedResources
2
2
  module Helpers
3
3
  module LinkTo
4
+ def links_to_actions(actions, *args)
5
+ actions.map { |action| capture { send(:"link_to_#{action}", *args) } }.join.html_safe
6
+ end
7
+
4
8
  [:index, :new, :show, :edit, :destroy].each do |action|
5
9
  define_method(:"link_to_#{action}") do |*args|
6
- link_to(t(:".#{action}"), controller.send(:"#{action}_path"), args.extract_options!)
10
+ options = args.extract_options!
11
+ options[:class] = [action, options[:class]].compact.join(' ')
12
+
13
+ if action == :destroy
14
+ model = args.last.class.respond_to?(:model_name) ? args.last : controller.resource
15
+ name = model.class.model_name.human
16
+ options.reverse_merge!(:method => :delete, :confirm => t(:'.confirm_delete', :model_name => name))
17
+ end
18
+
19
+ link_text = args.shift if args.first.is_a?(String)
20
+ link_text = t(args.shift) if args.first.is_a?(Symbol)
21
+ link_text ||= t(:".#{action}")
22
+
23
+ url = if args.first.is_a?(String)
24
+ args.first
25
+ else
26
+ controller.send(:"#{action}_path", *args)
27
+ end
28
+
29
+ link_to(link_text, url, options)
7
30
  end
8
31
  end
9
32
  end
@@ -0,0 +1,84 @@
1
+ module InheritedResources
2
+ module Helpers
3
+ module ResourcesUrlFor
4
+ include UrlFor
5
+
6
+ def self.included(base)
7
+ base.send(:helper_method, public_instance_methods(false) + UrlFor.public_instance_methods(false))
8
+ end
9
+
10
+ def index_url(*resources)
11
+ resources = normalize_resources_for_url(resources) { parent_resources << resource_class.base_class }
12
+ super(*resources)
13
+ end
14
+
15
+ def new_url(*resources)
16
+ resources = normalize_resources_for_url(resources) { parent_resources << resource_class.name.underscore }
17
+ super(*resources)
18
+ end
19
+
20
+ def show_url(*resources)
21
+ resources = normalize_resources_for_url(resources) { self.resources }
22
+ super(*resources)
23
+ end
24
+
25
+ def edit_url(*resources)
26
+ resources = normalize_resources_for_url(resources) { self.resources }
27
+ super(*resources)
28
+ end
29
+
30
+ def parent_index_url(options = {})
31
+ polymorphic_url(parent_resources[0..-2] << parent_resources.last.class.base_class, options)
32
+ end
33
+
34
+ def parent_new_url(options = {})
35
+ polymorphic_url(parent_resources[0..-2].unshift(:new) << parent_resources.last.class.name.underscore, options)
36
+ end
37
+
38
+ def parent_show_url(options = {})
39
+ polymorphic_url(parent_resources, options)
40
+ end
41
+
42
+ def parent_edit_url(options = {})
43
+ polymorphic_url(parent_resources.unshift(:edit), options)
44
+ end
45
+
46
+ [:parent_index, :parent_new, :parent_show, :parent_edit].each do |action|
47
+ define_method(:"#{action}_path") do |*args|
48
+ send(:"#{action}_url", *args << args.extract_options!.reverse_merge(:routing_type => :path))
49
+ end
50
+ end
51
+
52
+ alias :resources_url :index_url
53
+ alias :resources_path :index_path
54
+
55
+ alias :resource_url :show_url
56
+ alias :resource_path :show_path
57
+
58
+ alias :destroy_url :show_url
59
+ alias :destroy_path :show_path
60
+
61
+ alias :parent_resources_url :parent_index_url
62
+ alias :parent_resources_path :parent_index_path
63
+
64
+ alias :parent_resource_url :parent_show_url
65
+ alias :parent_resource_path :parent_show_path
66
+
67
+ protected
68
+
69
+ def normalize_resources_for_url(args, &default)
70
+ options = args.extract_options!
71
+ args = if args.empty?
72
+ default.call
73
+ elsif args.first.is_a?(Array)
74
+ args.first
75
+ else
76
+ resources = self.resources.dup
77
+ resources.pop if resources.last.new_record?
78
+ resources + args
79
+ end
80
+ [args, options]
81
+ end
82
+ end
83
+ end
84
+ end
@@ -2,90 +2,54 @@ module InheritedResources
2
2
  module Helpers
3
3
  module UrlFor
4
4
  def self.included(base)
5
- base.send(:helper_method, public_instance_methods(false))
5
+ base.send(:helper_method, public_instance_methods(false)) if base.respond_to?(:helper_method, true)
6
6
  end
7
7
 
8
- def index_url(options = {})
9
- polymorphic_url(parent_resources << resource_class.base_class, options)
10
- end
11
-
12
- def new_url(options = {})
13
- polymorphic_url(parent_resources.unshift(:new) << resource_class.name.underscore, options)
14
- end
15
-
16
- def show_url(options = {})
17
- raise_invalid_url_helper(:show_url, resource) if resource.new_record?
18
- raise "can't generate show_url because the current resource (#{resource.inspect}) is a new record" if resource.new_record?
8
+ def index_url(resources, options = {})
9
+ if resources.last.respond_to?(:new_record?)
10
+ resources[-1, 1] = resources.last.class
11
+ elsif resources.last.is_a?(Symbol)
12
+ resources[-1, 1] = resources.last.to_s.camelize.singularize.constantize
13
+ end
14
+ validate_url_helper_arguments!(:index_url, resources)
19
15
  polymorphic_url(resources, options)
20
16
  end
21
17
 
22
- def edit_url(options = {})
23
- raise_invalid_url_helper(:edit_url, resource) if resource.new_record?
24
- polymorphic_url(resources.unshift(:edit), options)
25
- end
26
-
27
- [:index, :new, :show, :edit].each do |action|
28
- define_method(:"#{action}_path") do |*args|
29
- send(:"#{action}_url", args.extract_options!.reverse_merge(:routing_type => :path))
18
+ def new_url(resources, options = {})
19
+ if resources.last.respond_to?(:new_record?)
20
+ resources[-1, 1] = resources.last.class.name.underscore
21
+ elsif resources.last.is_a?(Class) && resources.last < ActiveRecord::Base
22
+ resources[-1, 1] = resources.last.name.underscore
30
23
  end
24
+ resources.unshift(:new) unless resources.first == :new
25
+ validate_url_helper_arguments!(:new_url, resources)
26
+ polymorphic_url(resources, options)
31
27
  end
32
28
 
33
- def parent_index_url(options = {})
34
- polymorphic_url(parent_resources[0..-2] << parent_resources.last.class.base_class, options)
35
- end
36
-
37
- def parent_new_url(options = {})
38
- polymorphic_url(parent_resources[0..-2].unshift(:new) << parent_resources.last.class.name.underscore, options)
39
- end
40
-
41
- def parent_show_url(options = {})
42
- polymorphic_url(parent_resources, options)
29
+ def show_url(resources, options = {})
30
+ validate_url_helper_arguments!(:show_url, resources)
31
+ polymorphic_url(resources, options)
43
32
  end
44
33
 
45
- def parent_edit_url(options = {})
46
- polymorphic_url(parent_resources.unshift(:edit), options)
34
+ def edit_url(resources, options = {})
35
+ resources.unshift(:edit) unless resources.first == :edit
36
+ validate_url_helper_arguments!(:edit_url, resources)
37
+ polymorphic_url(resources, options)
47
38
  end
48
39
 
49
- [:index, :new, :show, :edit].each do |action|
50
- define_method(:"parent_#{action}_path") do |*args|
51
- send(:"parent_#{action}_url", args.extract_options!.reverse_merge(:routing_type => :path))
40
+ [:index, :new, :show, :edit, :parent_index, :parent_new, :parent_show, :parent_edit].each do |action|
41
+ define_method(:"#{action}_path") do |*args|
42
+ send(:"#{action}_url", *args << args.extract_options!.reverse_merge(:routing_type => :path))
52
43
  end
53
44
  end
54
45
 
55
- def children_index_url(child, options = {})
56
- raise_invalid_url_helper("children_index_url(#{:child.inspect})", resource) if resource.new_record?
57
- polymorphic_url(resources << child.to_s.pluralize, options)
58
- end
59
-
60
- def children_index_path(*args)
61
- children_index_url(*args << args.extract_options!.reverse_merge(:routing_type => :path))
62
- end
63
-
64
- def children_new_url(child, options = {})
65
- raise_invalid_url_helper("children_new_url(#{:child.inspect})", resource) if resource.new_record?
66
- polymorphic_url(resources.unshift(:new) << child.to_s.singularize, options)
67
- end
68
-
69
- def children_new_path(*args)
70
- children_new_url(*args << args.extract_options!.reverse_merge(:routing_type => :path))
71
- end
72
-
73
- alias :resources_url :index_url
74
- alias :resources_path :index_path
75
-
76
- alias :resource_url :show_url
77
- alias :resource_path :show_path
78
-
79
- alias :parent_resources_url :parent_index_url
80
- alias :parent_resources_path :parent_index_path
81
-
82
- alias :parent_resource_url :parent_show_url
83
- alias :parent_resource_path :parent_show_path
84
-
85
46
  protected
86
-
87
- def raise_invalid_url_helper(method, resource)
88
- raise "can't generate #{method} because the current resource (#{resource.inspect}) is a new record"
47
+ def validate_url_helper_arguments!(method, args)
48
+ if new_record = args.detect { |arg| arg.respond_to?(:new_record?) && arg.new_record? }
49
+ raise "can't generate #{method} because #{new_record.inspect}) is a new record"
50
+ elsif [:show_url, :edit_url].include?(method) && !(args.last.respond_to?(:persisted?) && args.last.persisted?)
51
+ raise "can't generate #{method} because #{new_record.inspect}) is not a persisted record"
52
+ end
89
53
  end
90
54
  end
91
55
  end
@@ -1,3 +1,3 @@
1
1
  module InheritedResourcesHelpers
2
- VERSION = '0.0.7'
2
+ VERSION = '0.0.8'
3
3
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: inherited_resources_helpers
3
3
  version: !ruby/object:Gem::Version
4
- hash: 17
4
+ hash: 15
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 7
10
- version: 0.0.7
9
+ - 8
10
+ version: 0.0.8
11
11
  platform: ruby
12
12
  authors:
13
13
  - Sven Fuchs
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-09-13 00:00:00 +02:00
18
+ date: 2010-09-22 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
@@ -31,6 +31,7 @@ files:
31
31
  - lib/inherited_resources/helpers/accessors.rb
32
32
  - lib/inherited_resources/helpers/link_to.rb
33
33
  - lib/inherited_resources/helpers/resources.rb
34
+ - lib/inherited_resources/helpers/resources_url_for.rb
34
35
  - lib/inherited_resources/helpers/url_for.rb
35
36
  - lib/inherited_resources/helpers/version.rb
36
37
  - lib/inherited_resources/helpers.rb