extended_inherited_resources 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,182 @@
1
+ module InheritedResources
2
+ # = URLHelpers
3
+ #
4
+ # When you use InheritedResources it creates some UrlHelpers for you.
5
+ # And they handle everything for you.
6
+ #
7
+ # # /posts/1/comments
8
+ # resource_url # => /posts/1/comments/#{@comment.to_param}
9
+ # resource_url(comment) # => /posts/1/comments/#{comment.to_param}
10
+ # new_resource_url # => /posts/1/comments/new
11
+ # edit_resource_url # => /posts/1/comments/#{@comment.to_param}/edit
12
+ # collection_url # => /posts/1/comments
13
+ # parent_url # => /posts/1
14
+ #
15
+ # # /projects/1/tasks
16
+ # resource_url # => /projects/1/tasks/#{@task.to_param}
17
+ # resource_url(task) # => /projects/1/tasks/#{task.to_param}
18
+ # new_resource_url # => /projects/1/tasks/new
19
+ # edit_resource_url # => /projects/1/tasks/#{@task.to_param}/edit
20
+ # collection_url # => /projects/1/tasks
21
+ # parent_url # => /projects/1
22
+ #
23
+ # # /users
24
+ # resource_url # => /users/#{@user.to_param}
25
+ # resource_url(user) # => /users/#{user.to_param}
26
+ # new_resource_url # => /users/new
27
+ # edit_resource_url # => /users/#{@user.to_param}/edit
28
+ # collection_url # => /users
29
+ # parent_url # => /
30
+ #
31
+ # The nice thing is that those urls are not guessed during runtime. They are
32
+ # all created when you inherit.
33
+ #
34
+ module UrlHelpers
35
+
36
+ # This method hard code url helpers in the class.
37
+ #
38
+ # We are doing this because is cheaper than guessing them when our action
39
+ # is being processed (and even more cheaper when we are using nested
40
+ # resources).
41
+ #
42
+ # When we are using polymorphic associations, those helpers rely on
43
+ # polymorphic_url Rails helper.
44
+ #
45
+ def create_resources_url_helpers!
46
+ resource_segments, resource_ivars = [], []
47
+ resource_config = self.resources_configuration[:self]
48
+
49
+ singleton = self.resources_configuration[:self][:singleton]
50
+ polymorphic = self.parents_symbols.include?(:polymorphic)
51
+
52
+ # Add route_prefix if any.
53
+ unless resource_config[:route_prefix].blank?
54
+ if polymorphic
55
+ resource_ivars << resource_config[:route_prefix].to_s.inspect
56
+ else
57
+ resource_segments << resource_config[:route_prefix]
58
+ end
59
+ end
60
+
61
+ # Deal with belongs_to associations and polymorphic associations.
62
+ # Remember that we don't have to build the segments in polymorphic cases,
63
+ # because the url will be polymorphic_url.
64
+ #
65
+ self.parents_symbols.each do |symbol|
66
+ if symbol == :polymorphic
67
+ resource_ivars << :parent
68
+ else
69
+ config = self.resources_configuration[symbol]
70
+ resource_segments << config[:route_name]
71
+ resource_ivars << :"@#{config[:instance_name]}"
72
+ end
73
+ end
74
+
75
+ collection_ivars = resource_ivars.dup
76
+ collection_segments = resource_segments.dup
77
+
78
+ # Generate parent url before we add resource instances.
79
+ unless parents_symbols.empty?
80
+ generate_url_and_path_helpers nil, :parent, resource_segments, resource_ivars
81
+ generate_url_and_path_helpers :edit, :parent, resource_segments, resource_ivars
82
+ end
83
+
84
+ # This is the default route configuration, later we have to deal with
85
+ # exception from polymorphic and singleton cases.
86
+ #
87
+ collection_segments << resource_config[:route_collection_name]
88
+ resource_segments << resource_config[:route_instance_name]
89
+ resource_ivars << :"@#{resource_config[:instance_name]}"
90
+
91
+ # In singleton cases, we do not send the current element instance variable
92
+ # because the id is not in the URL. For example, we should call:
93
+ #
94
+ # project_manager_url(@project)
95
+ #
96
+ # Instead of:
97
+ #
98
+ # project_manager_url(@project, @manager)
99
+ #
100
+ # Another exception in singleton cases is that collection url does not
101
+ # exist. In such cases, we create the parent collection url. So in the
102
+ # manager case above, the collection url will be:
103
+ #
104
+ # project_url(@project)
105
+ #
106
+ # If the singleton does not have a parent, it will default to root_url.
107
+ #
108
+ # Finally, polymorphic cases we have to give hints to the polymorphic url
109
+ # builder. This works by attaching new ivars as symbols or records.
110
+ #
111
+ if singleton
112
+ collection_segments.pop
113
+ resource_ivars.pop
114
+
115
+ if polymorphic
116
+ resource_ivars << resource_config[:instance_name].inspect
117
+ new_ivars = resource_ivars
118
+ end
119
+ elsif polymorphic
120
+ collection_ivars << '(@_resource_class_new ||= resource_class.new)'
121
+ end
122
+
123
+ generate_url_and_path_helpers nil, :collection, collection_segments, collection_ivars
124
+ generate_url_and_path_helpers :new, :resource, resource_segments, new_ivars || collection_ivars
125
+ generate_url_and_path_helpers nil, :resource, resource_segments, resource_ivars
126
+ generate_url_and_path_helpers :edit, :resource, resource_segments, resource_ivars
127
+ end
128
+
129
+ def generate_url_and_path_helpers(prefix, name, resource_segments, resource_ivars) #:nodoc:
130
+ ivars = resource_ivars.dup
131
+
132
+ singleton = self.resources_configuration[:self][:singleton]
133
+ polymorphic = self.parents_symbols.include?(:polymorphic)
134
+
135
+ # If it's not a singleton, ivars are not empty, not a collection or
136
+ # not a "new" named route, we can pass a resource as argument.
137
+ #
138
+ unless (singleton && name != :parent) || ivars.empty? || name == :collection || prefix == :new
139
+ ivars.push "(given_args.first || #{ivars.pop})"
140
+ end
141
+
142
+ # In collection in polymorphic cases, allow an argument to be given as a
143
+ # replacemente for the parent.
144
+ #
145
+ if name == :collection && polymorphic
146
+ index = ivars.index(:parent)
147
+ ivars.insert index, "(given_args.first || parent)"
148
+ ivars.delete(:parent)
149
+ end
150
+
151
+ # When polymorphic is true, the segments must be replace by :polymorphic
152
+ # and ivars should be gathered into an array, which is compacted when
153
+ # optional.
154
+ #
155
+ if polymorphic
156
+ segments = :polymorphic
157
+ ivars = "[#{ivars.join(', ')}]"
158
+ ivars << '.compact' if self.resources_configuration[:polymorphic][:optional]
159
+ else
160
+ segments = resource_segments.empty? ? 'root' : resource_segments.join('_')
161
+ ivars = ivars.join(', ')
162
+ end
163
+
164
+ prefix = prefix ? "#{prefix}_" : ''
165
+ ivars << (ivars.empty? ? 'given_options' : ', given_options')
166
+
167
+ class_eval <<-URL_HELPERS, __FILE__, __LINE__
168
+ protected
169
+ def #{prefix}#{name}_path(*given_args)
170
+ given_options = given_args.extract_options!
171
+ #{prefix}#{segments}_path(#{ivars})
172
+ end
173
+
174
+ def #{prefix}#{name}_url(*given_args)
175
+ given_options = given_args.extract_options!
176
+ #{prefix}#{segments}_url(#{ivars})
177
+ end
178
+ URL_HELPERS
179
+ end
180
+
181
+ end
182
+ end
@@ -0,0 +1,3 @@
1
+ module InheritedResources
2
+ VERSION = '1.0.3'.freeze
3
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'extended_inherited_resources'
8
+
9
+ class Test::Unit::TestCase
10
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestExtendedInheritedResources < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: extended_inherited_resources
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - josevalim
13
+ - stevo
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-04-22 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: thoughtbot-shoulda
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 0
30
+ version: "0"
31
+ type: :development
32
+ version_requirements: *id001
33
+ description: ""
34
+ email: b.kosmowski@selleo.com
35
+ executables: []
36
+
37
+ extensions: []
38
+
39
+ extra_rdoc_files:
40
+ - LICENSE
41
+ - README.rdoc
42
+ files:
43
+ - .document
44
+ - .gitignore
45
+ - LICENSE
46
+ - README.rdoc
47
+ - Rakefile
48
+ - VERSION
49
+ - extended_inherited_resources.gemspec
50
+ - init.rb
51
+ - lib/inherited_resources.rb
52
+ - lib/inherited_resources/actions.rb
53
+ - lib/inherited_resources/base.rb
54
+ - lib/inherited_resources/base_helpers.rb
55
+ - lib/inherited_resources/belongs_to_helpers.rb
56
+ - lib/inherited_resources/blank_slate.rb
57
+ - lib/inherited_resources/class_methods.rb
58
+ - lib/inherited_resources/dsl.rb
59
+ - lib/inherited_resources/legacy/respond_to.rb
60
+ - lib/inherited_resources/legacy/responder.rb
61
+ - lib/inherited_resources/locales/en.yml
62
+ - lib/inherited_resources/polymorphic_helpers.rb
63
+ - lib/inherited_resources/responder.rb
64
+ - lib/inherited_resources/singleton_helpers.rb
65
+ - lib/inherited_resources/url_helpers.rb
66
+ - lib/inherited_resources/version.rb
67
+ - test/helper.rb
68
+ - test/test_extended_inherited_resources.rb
69
+ has_rdoc: true
70
+ homepage: http://github.com/stevo/extended_inherited_resources
71
+ licenses: []
72
+
73
+ post_install_message:
74
+ rdoc_options:
75
+ - --charset=UTF-8
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ segments:
83
+ - 0
84
+ version: "0"
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ segments:
90
+ - 0
91
+ version: "0"
92
+ requirements: []
93
+
94
+ rubyforge_project:
95
+ rubygems_version: 1.3.6
96
+ signing_key:
97
+ specification_version: 3
98
+ summary: Slightly extended version of josevalim inherited_resources - for rails 2.3
99
+ test_files:
100
+ - test/helper.rb
101
+ - test/test_extended_inherited_resources.rb