cancan_strong_parameters 0.2.4 → 0.3

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/README.md CHANGED
@@ -58,6 +58,20 @@ Or install it yourself as:
58
58
  attr_accessible :title, :body, :tags_attributes
59
59
  end
60
60
  ```
61
+
62
+ ## Checkboxes
63
+
64
+ _Credit: @jlee42_
65
+
66
+ With 0.3, `cancan_strong_parameters` now supports checkboxes in forms, like `tag_ids`.
67
+
68
+ In order to avoid confusion with other nested attributes in a call to `permit_params`, please use the actual `Array` class object:
69
+
70
+ ```ruby
71
+ class BlogPost < ActiveModel::Base
72
+ permit_params :title, :content, tag_ids: Array
73
+ end
74
+ ```
61
75
 
62
76
  ## Testing
63
77
 
@@ -9,7 +9,7 @@ Gem::Specification.new do |gem|
9
9
  gem.homepage = "https://github.com/colinyoung/cancan_strong_parameters"
10
10
 
11
11
  gem.add_dependency "cancan"
12
- gem.add_dependency "strong_parameters"
12
+ gem.add_dependency "strong_parameters", ">= 0.1.6"
13
13
  gem.add_dependency "activesupport"
14
14
 
15
15
  gem.add_development_dependency "require_all"
@@ -77,17 +77,11 @@ module CancanStrongParameters
77
77
 
78
78
  defaults = CancanStrongParameters::Controller::HASH_DEFAULTS
79
79
 
80
- # @todo We have to stringify everything for 1.8.7 due to a bug in `strong_parameters`.
81
- # More at https://github.com/rails/strong_parameters/pull/51
82
- hash = hash.attributized.stringified
83
-
84
80
  prepend_before_filter(filter_options) do
85
81
  resource_name = self.class.resource_name
86
82
 
87
- # @todo We have to stringify everything for 1.8.7 due to a bug in `strong_parameters`.
88
- # More at https://github.com/rails/strong_parameters/pull/51
89
- parameters = keys.flatten.map! {|k| k.to_s } + defaults
90
- parameters << ActionController::Parameters.new(hash)
83
+ parameters = keys.flatten + defaults
84
+ parameters << ActionController::Parameters.new(hash.attributized)
91
85
 
92
86
  # original: parameters = keys.flatten + defaults
93
87
  # parameters << hash
@@ -103,9 +97,9 @@ module CancanStrongParameters
103
97
  prepend_before_filter(filter_options) do
104
98
  resource_name = self.class.resource_name
105
99
  if params.has_key?(resource_name)
106
- self.params[resource_name] = params[resource_name].send method, *keys.stringified
100
+ self.params[resource_name] = params[resource_name].send method, *keys
107
101
  else
108
- self.params = params.send method, *keys.stringified
102
+ self.params = params.send method, *keys
109
103
  end
110
104
  end
111
105
  end
@@ -146,7 +140,11 @@ class Hash
146
140
 
147
141
  Hash.new.tap do |h|
148
142
  self.each do |k,v|
149
- h[:"#{k}_attributes"] = self[k].attributized + defaults
143
+ if v.respond_to? :attributized
144
+ h[:"#{k}_attributes"] = v.attributized + defaults
145
+ else
146
+ h[k] = v == Array ? [] : v
147
+ end
150
148
  end
151
149
  end
152
150
  end
@@ -208,38 +206,3 @@ class String
208
206
  !!(self =~ /^[0-9a-f]+$/)
209
207
  end
210
208
  end
211
-
212
- # @todo Can be remove when new version of `strong_parameters` (>=0.1.5) is released.
213
- class Hash
214
- def stringified
215
- Hash.new.tap do |h|
216
- each do |key, value|
217
- value = case value
218
- when Symbol
219
- value.to_s
220
- when Hash
221
- value.indifferent
222
- when Array
223
- value.stringified
224
- end
225
- h[key.to_s] = value
226
- end
227
- end
228
- end
229
- end
230
-
231
- class Array
232
- def stringified
233
- Array.new.tap do |a|
234
- each do |value|
235
- value = if value.is_a? Hash
236
- value.stringified.indifferent
237
- else
238
- value.to_s
239
- end
240
-
241
- a << value
242
- end
243
- end
244
- end
245
- end
@@ -1,3 +1,3 @@
1
1
  module CancanStrongParameters
2
- VERSION = "0.2.4"
2
+ VERSION = "0.3"
3
3
  end
@@ -5,7 +5,8 @@ class PostsController < ActionController::Base
5
5
  :comments => [
6
6
  :body,
7
7
  { :tags => [ :name ] } # This is fugly, use 1.9!
8
- ]
8
+ ],
9
+ :label_ids => Array
9
10
 
10
11
  def create
11
12
  @post = Post.new(params[:post])
@@ -3,8 +3,8 @@ class Post
3
3
  include ActiveModel::MassAssignmentSecurity
4
4
  include ActiveModel::AttributeMethods
5
5
 
6
- attr_accessor :title, :body
7
- attr_accessible :title, :body
6
+ attr_accessor :title, :body, :label_ids
7
+ attr_accessible :title, :body, :label_ids
8
8
 
9
9
  def initialize(attributes = {})
10
10
  @attributes = attributes
@@ -111,4 +111,15 @@ class PostsControllerTest < ActionController::TestCase
111
111
  }
112
112
  assigns(:post_attributes).indifferent.must_have_same_content_as expected.indifferent
113
113
  end
114
+
115
+ test "it can permit and post checkbox vars (ending in _ids)" do
116
+ params = {
117
+ :post => {
118
+ :label_ids => ['1','2','3']
119
+ }
120
+ }
121
+
122
+ post :create, params
123
+ assigns(:post_attributes).indifferent['label_ids'].must_equal ['1','2','3']
124
+ end
114
125
  end
data/test/rails_helper.rb CHANGED
@@ -20,7 +20,7 @@ require "rack/test"
20
20
  root = File.expand_path(File.dirname(__FILE__))
21
21
 
22
22
  # Define the application and configuration
23
- module Config
23
+ module Test
24
24
  class Application < ::Rails::Application
25
25
  # configuration here if needed
26
26
  config.active_support.deprecation = :stderr
@@ -28,9 +28,9 @@ module Config
28
28
  end
29
29
 
30
30
  # Initialize the application
31
- Config::Application.initialize!
31
+ Test::Application.initialize!
32
32
 
33
- Config::Application.routes.draw do
33
+ Test::Application.routes.draw do
34
34
  resources :posts
35
35
 
36
36
  match 'title/:id', :to => 'title#update', :via => :put
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cancan_strong_parameters
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: '0.3'
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-02-14 00:00:00.000000000 Z
12
+ date: 2013-06-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: cancan
@@ -34,7 +34,7 @@ dependencies:
34
34
  requirements:
35
35
  - - ! '>='
36
36
  - !ruby/object:Gem::Version
37
- version: '0'
37
+ version: 0.1.6
38
38
  type: :runtime
39
39
  prerelease: false
40
40
  version_requirements: !ruby/object:Gem::Requirement
@@ -42,7 +42,7 @@ dependencies:
42
42
  requirements:
43
43
  - - ! '>='
44
44
  - !ruby/object:Gem::Version
45
- version: '0'
45
+ version: 0.1.6
46
46
  - !ruby/object:Gem::Dependency
47
47
  name: activesupport
48
48
  requirement: !ruby/object:Gem::Requirement
@@ -174,7 +174,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
174
174
  version: '0'
175
175
  requirements: []
176
176
  rubyforge_project:
177
- rubygems_version: 1.8.24
177
+ rubygems_version: 1.8.23
178
178
  signing_key:
179
179
  specification_version: 3
180
180
  summary: make CanCan work with strong_parameters