inherited_resources 1.4.1 → 1.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: df31765cf3b996812936adaeb26f33e4e387b55a
4
- data.tar.gz: b4da9990905f738c3d77c277e3581b2e8d0d142e
3
+ metadata.gz: e6392aaded1e0d085dc72577ecbc0793327b6a00
4
+ data.tar.gz: d4891c303bae5e1255a5a74b580a1cfb0162f5d9
5
5
  SHA512:
6
- metadata.gz: 6a67fcd38ac5d44035110616ef92a76b5c96eff5c0ed70c218ccba2198d95490c2f4ec89635424edbbd3b53057e0bffc6e0b3a47a4997fd9d3ac079182bcaed0
7
- data.tar.gz: 571dff6d8493a3a5596d9eb5020a8da9e3e303d903ac4a5de2763e876e4816e7c97144ec241ace3b3c90ca48f27863b17d5b2b479836cb77292e2051dbec6139
6
+ metadata.gz: 034f81e33b0c640c9f4ca86499b7a9869fced0a34d278ce89b73e146c96ed2a0dece341bbe398de25b68c54add3142395f71224c15ecc01dd039cf435726669d
7
+ data.tar.gz: 606ca852e3a8b8a548e1edb65500dd7491dd692498298aaf12b555b036778334f442d98cbf04b030641abc7f033fe7d2f99105e3822c92c55b83aacb1b1b2a50
data/CHANGELOG CHANGED
@@ -1,3 +1,8 @@
1
+ # 1.5.0
2
+
3
+ * Supports nested modules (namespaced models and controllers).
4
+ * Supports Rails 4 Strong Parameters notation.
5
+
1
6
  # Version 1.4.1
2
7
 
3
8
  * Supports Rails 4
data/Gemfile CHANGED
@@ -5,3 +5,7 @@ gemspec
5
5
  gem 'rails', '>= 3.2', '< 5'
6
6
  gem 'mocha'
7
7
  gem 'turn'
8
+
9
+ group :test do
10
+ gem 'strong_parameters'
11
+ end
data/Gemfile.lock CHANGED
@@ -1,9 +1,9 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- inherited_resources (1.4.0)
4
+ inherited_resources (1.5.0)
5
5
  has_scope (~> 0.6.0.rc)
6
- responders (~> 1.0.0.rc)
6
+ responders (~> 1.0)
7
7
 
8
8
  GEM
9
9
  remote: https://rubygems.org/
@@ -69,7 +69,7 @@ GEM
69
69
  rake (>= 0.8.7)
70
70
  thor (>= 0.18.1, < 2.0)
71
71
  rake (10.1.0)
72
- responders (1.0.0.rc)
72
+ responders (1.1.0)
73
73
  railties (>= 3.2, < 5)
74
74
  sprockets (2.10.0)
75
75
  hike (~> 1.2)
@@ -80,6 +80,10 @@ GEM
80
80
  actionpack (>= 3.0)
81
81
  activesupport (>= 3.0)
82
82
  sprockets (~> 2.8)
83
+ strong_parameters (0.1.4)
84
+ actionpack (>= 3.2.0)
85
+ activemodel (>= 3.2.0)
86
+ railties (>= 3.2.0)
83
87
  thor (0.18.1)
84
88
  thread_safe (0.1.2)
85
89
  atomic
@@ -98,4 +102,5 @@ DEPENDENCIES
98
102
  inherited_resources!
99
103
  mocha
100
104
  rails (>= 3.2, < 5)
105
+ strong_parameters
101
106
  turn
data/README.md CHANGED
@@ -52,9 +52,7 @@ http://github.com/josevalim/inherited_resources/tree/v1.0
52
52
  Since Inherited Resources 1.0, has_scope is not part of its core anymore but
53
53
  a gem dependency. Be sure to check the documentation to see how you can use it:
54
54
 
55
- ```
56
- http://github.com/plataformatec/has_scope
57
- ```
55
+ - <http://github.com/plataformatec/has_scope>
58
56
 
59
57
  And it can be installed as:
60
58
 
@@ -69,9 +67,7 @@ but is set as Inherited Resources dependency and it's used by default by
69
67
  InheritedResources controllers. Be sure to check the documentation to see
70
68
  how it will change your application:
71
69
 
72
- ```
73
- http://github.com/plataformatec/responders
74
- ```
70
+ - <http://github.com/plataformatec/responders>
75
71
 
76
72
  And it can be installed with:
77
73
 
@@ -166,7 +162,7 @@ One reason to use the `inherit_resources` macro would be to ensure that your con
166
162
  never responds with the html mime-type. `InheritedResources::Base` already
167
163
  responds to `:html`, and the `respond_to` macro is strictly additive.
168
164
  Therefore, if you want to create a controller that, for example, responds ONLY via `:js`,
169
- you will have write it this way:
165
+ you will have to write it this way:
170
166
 
171
167
  ```ruby
172
168
  class AccountsController < ApplicationController
@@ -266,7 +262,7 @@ end
266
262
  ```
267
263
 
268
264
  Since most of the time when you change a create, update or destroy
269
- action you do so because you want to to change its redirect url, a shortcut is
265
+ action you do so because you want to change its redirect url, a shortcut is
270
266
  provided. So you can do:
271
267
 
272
268
  ```ruby
@@ -315,6 +311,18 @@ end
315
311
  Yes, it's that simple! The nice part is since you already set the instance variable
316
312
  `@project`, it will not build a project again.
317
313
 
314
+ Same goes for updating the project:
315
+
316
+ ```ruby
317
+ class ProjectsController < InheritedResources::Base
318
+ def update
319
+ @project = Project.find(params[:id])
320
+ @project.something_special!
321
+ update!
322
+ end
323
+ end
324
+ ```
325
+
318
326
  Before we finish this topic, we should talk about one more thing: "success/failure
319
327
  blocks". Let's suppose that when we update our project, in case of failure, we
320
328
  want to redirect to the project url instead of re-rendering the edit template.
@@ -638,9 +646,8 @@ index actions accordingly). Also, it will produce `delete_resource_{path,url}` a
638
646
  Sometimes just DRYing up the controllers is not enough. If you need to DRY up your views,
639
647
  check this Wiki page:
640
648
 
641
- ```
642
649
  https://github.com/josevalim/inherited_resources/wiki/Views-Inheritance
643
- ```
650
+
644
651
 
645
652
  Notice that Rails 3.1 ships with view inheritance built-in.
646
653
 
@@ -678,6 +685,8 @@ def permitted_params
678
685
  end
679
686
  ```
680
687
 
688
+ Remember that if your field is sent by client to server as an array, you have to write `:permitted_field => []`, not just `:permitted_field`.
689
+
681
690
  Note that this doesn't work if you use strong_parameters' require method
682
691
  instead of permit, because whereas permit returns the entire sanitized
683
692
  parameter hash, require returns only the sanitized params below the parameter
@@ -687,7 +696,7 @@ If you need `params.require` you can do it like this:
687
696
 
688
697
  ```ruby
689
698
  def permitted_params
690
- {:widget => params.require(:widget => [:permitted_field, :other_permitted_field])}
699
+ {:widget => params.fetch(:widget, {}).permit(:permitted_field, :other_permitted_field)}
691
700
  end
692
701
  ```
693
702
 
@@ -695,10 +704,22 @@ Or better yet just override `#build_resource_params` directly:
695
704
 
696
705
  ```ruby
697
706
  def build_resource_params
698
- [params.require(:widget => [:permitted_field, :other_permitted_field])]
707
+ [params.fetch(:widget, {}).permit(:permitted_field, :other_permitted_field)]
708
+ end
709
+ ```
710
+
711
+
712
+ Instead you can stick to a standard Rails 4 notation (as rails scaffold generates) and write:
713
+
714
+ ```ruby
715
+ def widget_params
716
+ params.require(:widget).permit(:permitted_field, :other_permitted_field)
699
717
  end
700
718
  ```
701
719
 
720
+ In such case you should remove #permitted_params method because it has greater priority.
721
+
722
+
702
723
  ## Bugs and Feedback
703
724
 
704
725
  If you discover any bugs, please describe it in the issues tracker, including Rails and InheritedResources versions.
@@ -7,10 +7,9 @@ Gem::Specification.new do |s|
7
7
  s.version = InheritedResources::VERSION.dup
8
8
  s.platform = Gem::Platform::RUBY
9
9
  s.summary = "Inherited Resources speeds up development by making your controllers inherit all restful actions so you just have to focus on what is important."
10
- s.email = "developers@plataformatec.com.br"
11
10
  s.homepage = "http://github.com/josevalim/inherited_resources"
12
11
  s.description = "Inherited Resources speeds up development by making your controllers inherit all restful actions so you just have to focus on what is important."
13
- s.authors = ['José Valim']
12
+ s.authors = ['José Valim', 'Joel Moss']
14
13
  s.license = "MIT"
15
14
 
16
15
  s.rubyforge_project = "inherited_resources"
@@ -19,6 +18,6 @@ Gem::Specification.new do |s|
19
18
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
20
19
  s.require_paths = ["lib"]
21
20
 
22
- s.add_dependency("responders", "~> 1.0.0.rc")
21
+ s.add_dependency("responders", "~> 1.0")
23
22
  s.add_dependency("has_scope", "~> 0.6.0.rc")
24
23
  end
@@ -51,7 +51,7 @@ module InheritedResources
51
51
  get_resource_ivar || set_resource_ivar(end_of_association_chain.send(method_for_find, params[:id]))
52
52
  end
53
53
 
54
- # This method is responsable for building the object on :new and :create
54
+ # This method is responsible for building the object on :new and :create
55
55
  # methods. If you overwrite it, don't forget to cache the result in an
56
56
  # instance variable.
57
57
  #
@@ -316,9 +316,44 @@ module InheritedResources
316
316
  @resource_params ||= build_resource_params
317
317
  end
318
318
 
319
+ def resource_params_method_name
320
+ "#{resource_instance_name}_params"
321
+ end
322
+
323
+ # Returns hash of sanitized params in a form like
324
+ # `{:project => {:project_attribute => 'value'}}`
325
+ #
326
+ # This method makes use of `project_params` (or `smth_else_params`) which
327
+ # is a default Rails controller method for strong parameters definition.
328
+ #
329
+ # `permitted_params` is usually fired by method :new, :create, :update
330
+ # actions. Action :new usually has no parameters so strong parameters
331
+ # `require` directive raises a +ActionController::ParameterMissing+
332
+ # exception. `#permitted_params` rescues such exceptions in :new and
333
+ # returns an empty hash of parameters (which is reasonable default).
334
+ # If for any reasons you need something more specific, you can redefine
335
+ # this method in a way previous `inherited_resources` versions did:
336
+ #
337
+ # # Unnecessary redefinition
338
+ # def permitted_params
339
+ # params.permit(:project => [:project_attribute])
340
+ # end
341
+ #
342
+ def permitted_params
343
+ return nil unless respond_to?(resource_params_method_name, true)
344
+ {resource_request_name => send(resource_params_method_name)}
345
+ rescue ActionController::ParameterMissing
346
+ # typically :new action
347
+ if params[:action].to_s == 'new'
348
+ {resource_request_name => {}}
349
+ else
350
+ raise
351
+ end
352
+ end
353
+
319
354
  # extract attributes from params
320
355
  def build_resource_params
321
- parameters = respond_to?(:permitted_params, true) ? permitted_params : params
356
+ parameters = permitted_params || params
322
357
  rparams = [parameters[resource_request_name] || parameters[resource_instance_name] || {}]
323
358
  if without_protection_given?
324
359
  rparams << without_protection
@@ -171,13 +171,34 @@ module InheritedResources
171
171
 
172
172
  config = self.resources_configuration[symbol] = {}
173
173
 
174
- config[:parent_class] = options.delete(:parent_class) || begin
175
- class_name = (options.delete(:class_name) || symbol).to_s.pluralize.classify
176
- class_name.constantize
177
- rescue NameError => e
178
- raise unless e.message.include?(class_name)
179
- nil
180
- end
174
+ class_name = ''
175
+ config[:parent_class] = options.delete(:parent_class) ||
176
+ begin
177
+ class_name = if options[:class_name]
178
+ options.delete(:class_name).to_s.pluralize.classify
179
+ else
180
+ namespace = self.name.deconstantize
181
+ model_name = symbol.to_s.pluralize.classify
182
+
183
+ klass = model_name
184
+ while namespace != ''
185
+ klass = "#{namespace}::#{model_name}"
186
+ if klass.safe_constantize
187
+ break
188
+ else
189
+ namespace = namespace.deconstantize
190
+ end
191
+ end
192
+
193
+ klass = model_name if klass.start_with?('::')
194
+
195
+ klass
196
+ end
197
+ class_name.constantize
198
+ rescue NameError => e
199
+ raise unless e.message.include?(class_name)
200
+ nil
201
+ end
181
202
 
182
203
  config[:singleton] = options.delete(:singleton) || false
183
204
  config[:collection_name] = options.delete(:collection_name) || symbol.to_s.pluralize.to_sym
@@ -229,13 +250,13 @@ module InheritedResources
229
250
  # * <tt>:resource</tt> - Allows you to specify resource actions.
230
251
  # custom_actions :resource => :delete
231
252
  # This macro creates 'delete' method in controller and defines
232
- # delete_reource_{path,url} helpers. The body of generated 'delete'
253
+ # delete_resource_{path,url} helpers. The body of generated 'delete'
233
254
  # method is same as 'show' method. So you can override it if need
234
255
  #
235
256
  # * <tt>:collection</tt> - Allows you to specify collection actions.
236
257
  # custom_actions :collection => :search
237
258
  # This macro creates 'search' method in controller and defines
238
- # search_reources_{path,url} helpers. The body of generated 'search'
259
+ # search_resources_{path,url} helpers. The body of generated 'search'
239
260
  # method is same as 'index' method. So you can override it if need
240
261
  #
241
262
  def custom_actions(options)
@@ -1,3 +1,3 @@
1
1
  module InheritedResources
2
- VERSION = '1.4.1'.freeze
2
+ VERSION = '1.5.0'.freeze
3
3
  end
@@ -23,6 +23,17 @@ class DeansController < InheritedResources::Base
23
23
  belongs_to :school
24
24
  end
25
25
 
26
+ module Library
27
+ class Category
28
+ end
29
+
30
+ class Subcategory
31
+ end
32
+
33
+ class SubcategoriesController < InheritedResources::Base
34
+ end
35
+ end
36
+
26
37
  class ActionsClassMethodTest < ActionController::TestCase
27
38
  tests BooksController
28
39
 
@@ -108,7 +119,7 @@ class BelongsToErrorsTest < ActiveSupport::TestCase
108
119
 
109
120
  def test_belongs_to_raises_an_error_when_multiple_associations_are_given_with_options
110
121
  assert_raise ArgumentError do
111
- DeansController.send(:belongs_to, :arguments, :with_options, :parent_class => Professor)
122
+ DeansController.send(:belongs_to, :arguments, :with_options, :parent_class => Book)
112
123
  end
113
124
  end
114
125
 
@@ -131,4 +142,14 @@ class BelongsToErrorsTest < ActiveSupport::TestCase
131
142
  ensure
132
143
  DeansController.send(:parents_symbols=, [:school])
133
144
  end
145
+
146
+ def test_belongs_to_for_namespaced_controller_and_namespaced_model_fetches_model_in_the_namespace_firstly
147
+ Library::SubcategoriesController.send(:belongs_to, :category)
148
+ assert_equal Library::Category, Library::SubcategoriesController.resources_configuration[:category][:parent_class]
149
+ end
150
+
151
+ def test_belongs_to_without_namespace_sets_parent_class_properly
152
+ FoldersController.send(:belongs_to, :book)
153
+ assert_equal Book, FoldersController.resources_configuration[:book][:parent_class]
154
+ end
134
155
  end
@@ -1,5 +1,9 @@
1
1
  require File.expand_path('test_helper', File.dirname(__FILE__))
2
2
 
3
+ if ActionPack::VERSION::MAJOR == 3
4
+ require 'strong_parameters'
5
+ end
6
+
3
7
  class Widget
4
8
  extend ActiveModel::Naming
5
9
  end
@@ -7,6 +11,7 @@ end
7
11
  class WidgetsController < InheritedResources::Base
8
12
  end
9
13
 
14
+ # test usage of `permitted_params`
10
15
  class StrongParametersTest < ActionController::TestCase
11
16
  def setup
12
17
  @controller = WidgetsController.new
@@ -34,4 +39,82 @@ class StrongParametersTest < ActionController::TestCase
34
39
  Widget.expects(:find).with('42').returns(mock_widget)
35
40
  put :update, :id => '42', :widget => {:permitted => 'param', :prohibited => 'param'}
36
41
  end
37
- end
42
+
43
+ # `permitted_params` has greater priority than `widget_params`
44
+ def test_with_permitted_and_resource_methods
45
+ @controller.stubs(:widget_params).returns(:permitted => 'another_param')
46
+ class << @controller
47
+ private :widget_params
48
+ end
49
+ Widget.expects(:new).with(:permitted => 'param')
50
+ get :new, :widget => { :permitted => 'param', :prohibited => 'param' }
51
+ end
52
+ end
53
+
54
+ # test usage of `widget_params`
55
+ class StrongParametersWithoutPermittedParamsTest < ActionController::TestCase
56
+ def setup
57
+ @controller = WidgetsController.new
58
+ @controller.stubs(:widget_url).returns("/")
59
+ @controller.stubs(:widget_params).returns(:permitted => 'param')
60
+ class << @controller
61
+ private :widget_params
62
+ end
63
+ end
64
+
65
+ def test_permitted_params_from_new
66
+ Widget.expects(:new).with(:permitted => 'param')
67
+ get :new, :widget => { :permitted => 'param', :prohibited => 'param' }
68
+ end
69
+
70
+ def test_permitted_params_from_create
71
+ Widget.expects(:new).with(:permitted => 'param').returns(mock(:save => true))
72
+ post :create, :widget => { :permitted => 'param', :prohibited => 'param' }
73
+ end
74
+
75
+ def test_permitted_params_from_update
76
+ mock_widget = mock
77
+ mock_widget.stubs(:class).returns(Widget)
78
+ mock_widget.expects(:update_attributes).with(:permitted => 'param')
79
+ Widget.expects(:find).with('42').returns(mock_widget)
80
+ put :update, :id => '42', :widget => {:permitted => 'param', :prohibited => 'param'}
81
+ end
82
+ end
83
+
84
+ # test usage of `widget_params` integrated with strong parameters (not using stubs)
85
+ class StrongParametersIntegrationTest < ActionController::TestCase
86
+ def setup
87
+ @controller = WidgetsController.new
88
+ @controller.stubs(:widget_url).returns("/")
89
+
90
+ class << @controller
91
+ define_method :widget_params do
92
+ params.require(:widget).permit(:permitted)
93
+ end
94
+ private :widget_params
95
+ end
96
+ end
97
+
98
+ def test_permitted_empty_params_from_new
99
+ Widget.expects(:new).with({})
100
+ get :new, {}
101
+ end
102
+
103
+ def test_permitted_params_from_new
104
+ Widget.expects(:new).with('permitted' => 'param')
105
+ get :new, :widget => { :permitted => 'param', :prohibited => 'param' }
106
+ end
107
+
108
+ def test_permitted_params_from_create
109
+ Widget.expects(:new).with('permitted' => 'param').returns(mock(:save => true))
110
+ post :create, :widget => { :permitted => 'param', :prohibited => 'param' }
111
+ end
112
+
113
+ def test_permitted_params_from_update
114
+ mock_widget = mock
115
+ mock_widget.stubs(:class).returns(Widget)
116
+ mock_widget.expects(:update_attributes).with('permitted' => 'param')
117
+ Widget.expects(:find).with('42').returns(mock_widget)
118
+ put :update, :id => '42', :widget => {:permitted => 'param', :prohibited => 'param'}
119
+ end
120
+ end
metadata CHANGED
@@ -1,51 +1,52 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: inherited_resources
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.1
4
+ version: 1.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - José Valim
8
+ - Joel Moss
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2013-07-31 00:00:00.000000000 Z
12
+ date: 2014-06-03 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
15
  name: responders
15
16
  requirement: !ruby/object:Gem::Requirement
16
17
  requirements:
17
- - - ~>
18
+ - - "~>"
18
19
  - !ruby/object:Gem::Version
19
- version: 1.0.0.rc
20
+ version: '1.0'
20
21
  type: :runtime
21
22
  prerelease: false
22
23
  version_requirements: !ruby/object:Gem::Requirement
23
24
  requirements:
24
- - - ~>
25
+ - - "~>"
25
26
  - !ruby/object:Gem::Version
26
- version: 1.0.0.rc
27
+ version: '1.0'
27
28
  - !ruby/object:Gem::Dependency
28
29
  name: has_scope
29
30
  requirement: !ruby/object:Gem::Requirement
30
31
  requirements:
31
- - - ~>
32
+ - - "~>"
32
33
  - !ruby/object:Gem::Version
33
34
  version: 0.6.0.rc
34
35
  type: :runtime
35
36
  prerelease: false
36
37
  version_requirements: !ruby/object:Gem::Requirement
37
38
  requirements:
38
- - - ~>
39
+ - - "~>"
39
40
  - !ruby/object:Gem::Version
40
41
  version: 0.6.0.rc
41
42
  description: Inherited Resources speeds up development by making your controllers
42
43
  inherit all restful actions so you just have to focus on what is important.
43
- email: developers@plataformatec.com.br
44
+ email:
44
45
  executables: []
45
46
  extensions: []
46
47
  extra_rdoc_files: []
47
48
  files:
48
- - .gitignore
49
+ - ".gitignore"
49
50
  - CHANGELOG
50
51
  - Gemfile
51
52
  - Gemfile.lock
@@ -179,17 +180,17 @@ require_paths:
179
180
  - lib
180
181
  required_ruby_version: !ruby/object:Gem::Requirement
181
182
  requirements:
182
- - - '>='
183
+ - - ">="
183
184
  - !ruby/object:Gem::Version
184
185
  version: '0'
185
186
  required_rubygems_version: !ruby/object:Gem::Requirement
186
187
  requirements:
187
- - - '>='
188
+ - - ">="
188
189
  - !ruby/object:Gem::Version
189
190
  version: '0'
190
191
  requirements: []
191
192
  rubyforge_project: inherited_resources
192
- rubygems_version: 2.0.6
193
+ rubygems_version: 2.2.2
193
194
  signing_key:
194
195
  specification_version: 4
195
196
  summary: Inherited Resources speeds up development by making your controllers inherit
@@ -294,4 +295,3 @@ test_files:
294
295
  - test/views/users/update.js.erb
295
296
  - test/views/venue/show.html.erb
296
297
  - test/views/widgets/new.html.erb
297
- has_rdoc: