inherited_resources 1.2.2 → 1.3.0

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/CHANGELOG CHANGED
@@ -1,3 +1,10 @@
1
+ # Version 1.3.0
2
+
3
+ * Added support for multiple polymorphic optional nesting
4
+ * Fix nested namespace in mountable apps
5
+ * Added support for rails 3.1 new mass assignment conventions
6
+ * Turn InheritedResources::Base into a reloadable constant to fix reloading issues
7
+
1
8
  # Version 1.2.2
2
9
 
3
10
  * Fix a bug in params parsing
data/Gemfile CHANGED
@@ -1,6 +1,6 @@
1
1
  source "http://rubygems.org"
2
2
 
3
- gem "rails", "3.0.3"
3
+ gem "rails", "3.1.0.rc4"
4
4
  gem "responders", "~> 0.6.0"
5
5
  gem "has_scope", "~> 0.5.0"
6
6
  gem "mocha"
data/Gemfile.lock CHANGED
@@ -1,79 +1,97 @@
1
1
  GEM
2
2
  remote: http://rubygems.org/
3
3
  specs:
4
- abstract (1.0.0)
5
- actionmailer (3.0.3)
6
- actionpack (= 3.0.3)
7
- mail (~> 2.2.9)
8
- actionpack (3.0.3)
9
- activemodel (= 3.0.3)
10
- activesupport (= 3.0.3)
11
- builder (~> 2.1.2)
12
- erubis (~> 2.6.6)
13
- i18n (~> 0.4)
14
- rack (~> 1.2.1)
15
- rack-mount (~> 0.6.13)
16
- rack-test (~> 0.5.6)
17
- tzinfo (~> 0.3.23)
18
- activemodel (3.0.3)
19
- activesupport (= 3.0.3)
20
- builder (~> 2.1.2)
21
- i18n (~> 0.4)
22
- activerecord (3.0.3)
23
- activemodel (= 3.0.3)
24
- activesupport (= 3.0.3)
25
- arel (~> 2.0.2)
26
- tzinfo (~> 0.3.23)
27
- activeresource (3.0.3)
28
- activemodel (= 3.0.3)
29
- activesupport (= 3.0.3)
30
- activesupport (3.0.3)
31
- arel (2.0.6)
32
- builder (2.1.2)
33
- columnize (0.3.1)
34
- erubis (2.6.6)
35
- abstract (>= 1.0.0)
4
+ actionmailer (3.1.0.rc4)
5
+ actionpack (= 3.1.0.rc4)
6
+ mail (~> 2.3.0)
7
+ actionpack (3.1.0.rc4)
8
+ activemodel (= 3.1.0.rc4)
9
+ activesupport (= 3.1.0.rc4)
10
+ builder (~> 3.0.0)
11
+ erubis (~> 2.7.0)
12
+ i18n (~> 0.6)
13
+ rack (~> 1.3.0)
14
+ rack-cache (~> 1.0.1)
15
+ rack-mount (~> 0.8.1)
16
+ rack-test (~> 0.6.0)
17
+ sprockets (~> 2.0.0.beta.10)
18
+ tzinfo (~> 0.3.27)
19
+ activemodel (3.1.0.rc4)
20
+ activesupport (= 3.1.0.rc4)
21
+ bcrypt-ruby (~> 2.1.4)
22
+ builder (~> 3.0.0)
23
+ i18n (~> 0.6)
24
+ activerecord (3.1.0.rc4)
25
+ activemodel (= 3.1.0.rc4)
26
+ activesupport (= 3.1.0.rc4)
27
+ arel (~> 2.1.1)
28
+ tzinfo (~> 0.3.27)
29
+ activeresource (3.1.0.rc4)
30
+ activemodel (= 3.1.0.rc4)
31
+ activesupport (= 3.1.0.rc4)
32
+ activesupport (3.1.0.rc4)
33
+ multi_json (~> 1.0)
34
+ arel (2.1.1)
35
+ bcrypt-ruby (2.1.4)
36
+ builder (3.0.0)
37
+ columnize (0.3.4)
38
+ erubis (2.7.0)
36
39
  has_scope (0.5.0)
37
- i18n (0.5.0)
38
- linecache (0.43)
39
- mail (2.2.14)
40
- activesupport (>= 2.3.6)
40
+ hike (1.1.0)
41
+ i18n (0.6.0)
42
+ linecache (0.46)
43
+ rbx-require-relative (> 0.0.4)
44
+ mail (2.3.0)
41
45
  i18n (>= 0.4.0)
42
46
  mime-types (~> 1.16)
43
47
  treetop (~> 1.4.8)
44
48
  mime-types (1.16)
45
49
  mocha (0.9.8)
46
50
  rake
51
+ multi_json (1.0.3)
47
52
  polyglot (0.3.1)
48
- rack (1.2.1)
49
- rack-mount (0.6.13)
53
+ rack (1.3.0)
54
+ rack-cache (1.0.2)
55
+ rack (>= 0.4)
56
+ rack-mount (0.8.1)
50
57
  rack (>= 1.0.0)
51
- rack-test (0.5.7)
58
+ rack-ssl (1.3.2)
59
+ rack
60
+ rack-test (0.6.0)
52
61
  rack (>= 1.0)
53
- rails (3.0.3)
54
- actionmailer (= 3.0.3)
55
- actionpack (= 3.0.3)
56
- activerecord (= 3.0.3)
57
- activeresource (= 3.0.3)
58
- activesupport (= 3.0.3)
62
+ rails (3.1.0.rc4)
63
+ actionmailer (= 3.1.0.rc4)
64
+ actionpack (= 3.1.0.rc4)
65
+ activerecord (= 3.1.0.rc4)
66
+ activeresource (= 3.1.0.rc4)
67
+ activesupport (= 3.1.0.rc4)
59
68
  bundler (~> 1.0)
60
- railties (= 3.0.3)
61
- railties (3.0.3)
62
- actionpack (= 3.0.3)
63
- activesupport (= 3.0.3)
69
+ railties (= 3.1.0.rc4)
70
+ railties (3.1.0.rc4)
71
+ actionpack (= 3.1.0.rc4)
72
+ activesupport (= 3.1.0.rc4)
73
+ rack-ssl (~> 1.3.2)
64
74
  rake (>= 0.8.7)
65
- thor (~> 0.14.4)
75
+ rdoc (~> 3.4)
76
+ thor (~> 0.14.6)
66
77
  rake (0.8.7)
67
- responders (0.6.2)
68
- ruby-debug (0.10.3)
78
+ rbx-require-relative (0.0.5)
79
+ rdoc (3.6.1)
80
+ responders (0.6.4)
81
+ ruby-debug (0.10.4)
69
82
  columnize (>= 0.1)
70
- ruby-debug-base (~> 0.10.3.0)
71
- ruby-debug-base (0.10.3)
83
+ ruby-debug-base (~> 0.10.4.0)
84
+ ruby-debug-base (0.10.4)
72
85
  linecache (>= 0.3)
86
+ sprockets (2.0.0.beta.10)
87
+ hike (~> 1.0)
88
+ rack (~> 1.0)
89
+ tilt (~> 1.1, != 1.3.0)
73
90
  thor (0.14.6)
91
+ tilt (1.3.2)
74
92
  treetop (1.4.9)
75
93
  polyglot (>= 0.3.1)
76
- tzinfo (0.3.24)
94
+ tzinfo (0.3.28)
77
95
 
78
96
  PLATFORMS
79
97
  ruby
@@ -81,6 +99,6 @@ PLATFORMS
81
99
  DEPENDENCIES
82
100
  has_scope (~> 0.5.0)
83
101
  mocha
84
- rails (= 3.0.3)
102
+ rails (= 3.1.0.rc4)
85
103
  responders (~> 0.6.0)
86
104
  ruby-debug
data/README.rdoc CHANGED
@@ -8,8 +8,8 @@ Plus, making your controllers follow a pattern, it helps you to write better
8
8
  code by following fat models and skinny controllers convention. There are
9
9
  two screencasts available besides this README:
10
10
 
11
- http://railscasts.com/episodes/230-inherited-resources
12
- http://akitaonrails.com/2009/09/01/screencast-real-thin-restful-controllers-with-inherited-resources
11
+ * http://railscasts.com/episodes/230-inherited-resources
12
+ * http://akitaonrails.com/2009/09/01/screencast-real-thin-restful-controllers-with-inherited-resources
13
13
 
14
14
  == Installation
15
15
 
@@ -39,15 +39,12 @@ Or checkout from the v1.0 branch:
39
39
 
40
40
  == HasScope
41
41
 
42
- Since Inherited Resources 1.0, has_scope is not part of its core anymore.
43
- However, if you are using has_scope in your application, Inherited Resources
44
- will handle all the required hooks automatically.
45
-
46
- has_scope gem is available at:
42
+ Since Inherited Resources 1.0, has_scope is not part of its core anymore but
43
+ a gem dependency. Be sure to check the documentation to see how you can use it:
47
44
 
48
45
  http://github.com/plataformatec/has_scope
49
46
 
50
- And can be installed as:
47
+ And it can be installed as:
51
48
 
52
49
  gem install has_scope
53
50
 
@@ -514,6 +511,8 @@ check this Wiki page:
514
511
 
515
512
  https://github.com/josevalim/inherited_resources/wiki/Views-Inheritance
516
513
 
514
+ Notice that Rails 3.1 ships with view inheritance built-in.
515
+
517
516
  == Some DSL
518
517
 
519
518
  For those DSL lovers, InheritedResources won't leave you alone. You can overwrite
@@ -534,9 +533,9 @@ And then you can rewrite the last example as:
534
533
 
535
534
  == Bugs and Feedback
536
535
 
537
- If you discover any bugs or want to drop a line, join us in the mailing list:
536
+ If you discover any bugs, please describe it in the issues tracker, including Rails and Inherited Resources versions.
538
537
 
539
- http://groups.google.com/group/inherited_resources
538
+ Questions are better handled on StackOverflow.
540
539
 
541
- Copyright (c) 2009 José Valim http://blog.plataformatec.com.br
540
+ Copyright (c) 2011 José Valim http://blog.plataformatec.com.br
542
541
  See the attached MIT License.
@@ -1,6 +1,3 @@
1
- require 'inherited_resources/blank_slate'
2
- require 'inherited_resources/responder'
3
-
4
1
  module InheritedResources
5
2
  # = Base
6
3
  #
@@ -21,7 +18,7 @@ module InheritedResources
21
18
  extend InheritedResources::UrlHelpers
22
19
 
23
20
  # Add at least :html mime type
24
- respond_to :html
21
+ respond_to :html if self.mimes_for_respond_to.empty?
25
22
  self.responder = InheritedResources::Responder
26
23
 
27
24
  helper_method :resource, :collection, :resource_class, :association_chain,
@@ -33,7 +30,8 @@ module InheritedResources
33
30
  :parent_url, :parent_path,
34
31
  :smart_resource_url, :smart_collection_url
35
32
 
36
- self.class_attribute :resource_class, :parents_symbols, :resources_configuration, :instance_writer => false
33
+ self.class_attribute :resource_class, :instance_writer => false unless self.respond_to? :resource_class
34
+ self.class_attribute :parents_symbols, :resources_configuration, :instance_writer => false
37
35
 
38
36
  protected :resource_class, :parents_symbols, :resources_configuration,
39
37
  :resource_class?, :parents_symbols?, :resources_configuration?
@@ -42,4 +40,4 @@ module InheritedResources
42
40
 
43
41
  inherit_resources(self)
44
42
  end
45
- end
43
+ end
@@ -1,10 +1,12 @@
1
+ require 'rails/engine'
1
2
  require 'responders'
3
+ require 'inherited_resources/blank_slate'
4
+ require 'inherited_resources/responder'
2
5
 
3
6
  module InheritedResources
4
7
  ACTIONS = [ :index, :show, :new, :edit, :create, :update, :destroy ] unless self.const_defined?(:ACTIONS)
5
8
 
6
9
  autoload :Actions, 'inherited_resources/actions'
7
- autoload :Base, 'inherited_resources/base'
8
10
  autoload :BaseHelpers, 'inherited_resources/base_helpers'
9
11
  autoload :ShallowHelpers, 'inherited_resources/shallow_helpers'
10
12
  autoload :BelongsToHelpers, 'inherited_resources/belongs_to_helpers'
@@ -20,7 +22,7 @@ module InheritedResources
20
22
  Responders::FlashResponder.flash_keys = array
21
23
  end
22
24
 
23
- class Railtie < ::Rails::Railtie
25
+ class Railtie < ::Rails::Engine
24
26
  config.inherited_resources = InheritedResources
25
27
  if config.respond_to?(:app_generators)
26
28
  config.app_generators.scaffold_controller = :inherited_resources_controller
@@ -49,7 +49,7 @@ module InheritedResources
49
49
  # instance variable.
50
50
  #
51
51
  def build_resource
52
- get_resource_ivar || set_resource_ivar(end_of_association_chain.send(method_for_build, resource_params))
52
+ get_resource_ivar || set_resource_ivar(end_of_association_chain.send(method_for_build, *resource_params))
53
53
  end
54
54
 
55
55
  # Responsible for saving the resource on :create method. Overwriting this
@@ -75,7 +75,7 @@ module InheritedResources
75
75
  # end
76
76
  #
77
77
  def update_resource(object, attributes)
78
- object.update_attributes(attributes)
78
+ object.update_attributes(*attributes)
79
79
  end
80
80
 
81
81
  # Handle the :destroy method for the resource. Overwrite it to call your
@@ -300,7 +300,19 @@ module InheritedResources
300
300
 
301
301
  # extract attributes from params
302
302
  def resource_params
303
- params[resource_request_name] || params[resource_instance_name] || {}
303
+ rparams = [params[resource_request_name] || params[resource_instance_name] || {}]
304
+ rparams << as_role if role_given?
305
+ rparams
306
+ end
307
+
308
+ # checking if role given
309
+ def role_given?
310
+ self.resources_configuration[:self][:role].present?
311
+ end
312
+
313
+ # getting role for mass-asignment
314
+ def as_role
315
+ { :as => self.resources_configuration[:self][:role] }
304
316
  end
305
317
  end
306
318
  end
@@ -255,6 +255,12 @@ module InheritedResources
255
255
  end
256
256
  end
257
257
 
258
+ # Defines the role to use when creating or updating resource.
259
+ # Makes sense when using rails 3.1 mass assignment conventions
260
+ def with_role(role)
261
+ self.resources_configuration[:self][:role] = role.try(:to_sym)
262
+ end
263
+
258
264
  private
259
265
 
260
266
  def acts_as_singleton! #:nodoc:
@@ -273,21 +279,31 @@ module InheritedResources
273
279
  end
274
280
 
275
281
  def acts_as_shallow! #:nodoc:
282
+ include BelongsToHelpers
276
283
  include ShallowHelpers
277
284
  end
278
285
 
279
286
  # Initialize resources class accessors and set their default values.
280
287
  #
281
288
  def initialize_resources_class_accessors! #:nodoc:
282
- # First priority is the namespaced modek, e.g. User::Group
283
- self.resource_class = begin
289
+ # First priority is the namespaced model, e.g. User::Group
290
+ self.resource_class ||= begin
284
291
  namespaced_class = self.name.sub(/Controller/, '').singularize
285
292
  namespaced_class.constantize
286
293
  rescue NameError
287
294
  nil
288
295
  end
289
296
 
290
- # Second priority the camelcased c, i.e. UserGroup
297
+ # Second priority is the top namespace model, e.g. EngineName::Article for EngineName::Admin::ArticlesController
298
+ self.resource_class ||= begin
299
+ namespaced_classes = self.name.sub(/Controller/, '').split('::')
300
+ namespaced_class = [namespaced_classes.first, namespaced_classes.last].join('::').singularize
301
+ namespaced_class.constantize
302
+ rescue NameError
303
+ nil
304
+ end
305
+
306
+ # Third priority the camelcased c, i.e. UserGroup
291
307
  self.resource_class ||= begin
292
308
  camelcased_class = self.name.sub(/Controller/, '').gsub('::', '').singularize
293
309
  camelcased_class.constantize
@@ -126,28 +126,28 @@ module InheritedResources
126
126
  #
127
127
  def symbols_for_association_chain #:nodoc:
128
128
  polymorphic_config = resources_configuration[:polymorphic]
129
-
130
129
  parents_symbols.map do |symbol|
131
130
  if symbol == :polymorphic
132
131
  params_keys = params.keys
133
132
 
134
- key = polymorphic_config[:symbols].find do |poly|
135
- params_keys.include? resources_configuration[poly][:param].to_s
136
- end
133
+ keys = polymorphic_config[:symbols].map do |poly|
134
+ params_keys.include?(resources_configuration[poly][:param].to_s) ? poly : nil
135
+ end.compact
137
136
 
138
- if key.nil?
137
+ if keys.empty?
139
138
  raise ScriptError, "Could not find param for polymorphic association. The request" <<
140
139
  "parameters are #{params.keys.inspect} and the polymorphic " <<
141
140
  "associations are #{polymorphic_config[:symbols].inspect}." unless polymorphic_config[:optional]
142
141
 
143
142
  nil
144
143
  else
145
- @parent_type = key.to_sym
144
+ @parent_type = keys[-1].to_sym
145
+ @parent_types = keys.map(&:to_sym)
146
146
  end
147
147
  else
148
148
  symbol
149
149
  end
150
- end.compact
150
+ end.flatten.compact
151
151
  end
152
152
 
153
153
  end
@@ -1,87 +1,43 @@
1
1
  module InheritedResources
2
- # = belongs_to
2
+ # Shallow provides a functionality that goes on pair with Rails' shallow.
3
+ # It is very similar to "optional" but it actually finds all the parents
4
+ # resources instead of leaving them blank. Consider the following example:
3
5
  #
4
- # Let's suppose that we have some tasks that belongs to projects. To specify
5
- # this assoication in your controllers, just do:
6
+ # belongs_to :post, :shallow => true do
7
+ # belongs_to :comment
8
+ # end
6
9
  #
7
- # class TasksController < InheritedResources::Base
8
- # belongs_to :project
9
- # end
10
- #
11
- # belongs_to accepts several options to be able to configure the association.
12
- # For example, if you want urls like /projects/:project_title/tasks, you
13
- # can customize how InheritedResources find your projects:
14
- #
15
- # class TasksController < InheritedResources::Base
16
- # belongs_to :project, :finder => :find_by_title!, :param => :project_title
17
- # end
18
- #
19
- # It also accepts :route_name, :parent_class and :instance_name as options.
20
- # Check the lib/inherited_resources/class_methods.rb for more.
21
- #
22
- # = nested_belongs_to
23
- #
24
- # Now, our Tasks get some Comments and you need to nest even deeper. Good
25
- # practices says that you should never nest more than two resources, but sometimes
26
- # you have to for security reasons. So this is an example of how you can do it:
27
- #
28
- # class CommentsController < InheritedResources::Base
29
- # nested_belongs_to :project, :task
30
- # end
31
- #
32
- # If you need to configure any of these belongs to, you can nested them using blocks:
33
- #
34
- # class CommentsController < InheritedResources::Base
35
- # belongs_to :project, :finder => :find_by_title!, :param => :project_title do
36
- # belongs_to :task
37
- # end
38
- # end
39
- #
40
- # Warning: calling several belongs_to is the same as nesting them:
41
- #
42
- # class CommentsController < InheritedResources::Base
43
- # belongs_to :project
44
- # belongs_to :task
45
- # end
46
- #
47
- # In other words, the code above is the same as calling nested_belongs_to.
10
+ # When accessed as /comments/1, Inherited Resources will automatically get
11
+ # the post resource so both objects are actually accessible through the views.
48
12
  #
13
+ # However, when using optional, Inherited Resources wouldn't actually bother
14
+ # with finding the parent object.
49
15
  module ShallowHelpers
50
- include BelongsToHelpers
51
-
52
16
  private
53
17
 
54
- # Evaluate the parent given. This is used to nest parents in the
55
- # association chain.
56
- #
57
-
58
- # Maps parents_symbols to build association chain. In this case, it
59
- # simply return the parent_symbols, however on polymorphic belongs to,
60
- # it has some customization.
61
- #
62
18
  def symbols_for_association_chain #:nodoc:
63
19
  parent_symbols = parents_symbols.dup
64
- if parents_symbols.size > 1 && !params[:id]
65
- inst_class_name = parent_symbols.pop
66
- finder_method = resources_configuration[inst_class_name][:finder] || :find
67
- instance = resources_configuration[inst_class_name][:parent_class].send(finder_method, params[resources_configuration[inst_class_name][:param]])
68
- load_parents(instance, parent_symbols)
69
- end
70
- if params[:id]
20
+ instance = nil
21
+
22
+ if id = params[:id]
71
23
  finder_method = resources_configuration[:self][:finder] || :find
72
- instance = self.resource_class.send(finder_method, params[:id])
73
- load_parents(instance, parent_symbols)
24
+ instance = self.resource_class.send(finder_method, id)
25
+ elsif parents_symbols.size > 1
26
+ config = resources_configuration[parent_symbols.pop]
27
+ finder_method = config[:finder] || :find
28
+ instance = config[:parent_class].send(finder_method, params[config[:param]])
74
29
  end
30
+
31
+ load_parents(instance, parent_symbols) if instance
75
32
  parents_symbols
76
33
  end
77
34
 
78
35
  def load_parents(instance, parent_symbols)
79
-
80
36
  parent_symbols.reverse.each do |parent|
81
37
  instance = instance.send(parent)
82
- params[resources_configuration[parent][:param]] = instance.to_param
38
+ config = resources_configuration[parent]
39
+ params[config[:param]] = instance.to_param
83
40
  end
84
41
  end
85
42
  end
86
-
87
43
  end