cancan_strong_parameters 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  CanCan and [strong_parameters](https://github.com/rails/strong_parameters) are friends now!
4
4
 
5
+ [![Build Status](https://secure.travis-ci.org/colinyoung/cancan_strong_parameters.png)](http://travis-ci.org/colinyoung/cancan_strong_parameters)
6
+
5
7
  ## Authors
6
8
 
7
9
  The majority of this gem is credited to @mckeed, who posted this gist: https://gist.github.com/2878508
@@ -48,4 +50,8 @@ Run with `bundle exec rake test`.
48
50
  ## Changelog
49
51
 
50
52
  * Fixed some issues with nested form subfields in `permit_params`
51
- * Made compatible for nested forms
53
+ * Made compatible for nested forms
54
+ * Added default allows for _destroy.
55
+ * Tests pass in Travis.
56
+ * Fixes for irregular parameters posted like {"child_attributes" => {"0" => {}}}.
57
+ * Fixed a major security problem where I was manually inserting IDs - should be allowed by default, but not manually added
@@ -1,7 +1,7 @@
1
1
  module CancanStrongParameters
2
2
  module Controller
3
3
 
4
- HASH_DEFAULTS = [:_destroy, :_delete]
4
+ HASH_DEFAULTS = [:id, :_destroy, :_delete]
5
5
 
6
6
  module ClassMethods
7
7
  # Use this with CanCan's load_resource to permit a set of params before
@@ -76,7 +76,7 @@ module CancanStrongParameters
76
76
 
77
77
  prepend_before_filter :only => actions do
78
78
  resource_name = self.class.resource_name
79
- self.params[resource_name] = params[resource_name].send method, *[*keys.flatten + @@defaults, @@hash]
79
+ self.params[resource_name] = params[resource_name].standardized.send method, *[*keys.flatten + @@defaults, @@hash]
80
80
  end
81
81
  elsif hash.present?
82
82
  prepend_before_filter :only => actions do
@@ -119,13 +119,28 @@ class Hash
119
119
 
120
120
  Hash.new.tap do |h|
121
121
  self.each do |k,v|
122
- h[:"#{k}_attributes"] = self.delete(k).attributized + defaults
122
+ h[:"#{k}_attributes"] = self[k].attributized + defaults
123
+ end
124
+ end
125
+ end
126
+
127
+ # Converts keyed nested_forms (like task_attributes: {"0" => {}}) to normal params arrays.
128
+ def to_parameter_array
129
+ return self if self.empty?
130
+
131
+ return self unless (k = self.keys.first).is_a?(String) and k[0..3] == "new_" or k.is_i? or k.is_hex?
132
+
133
+ Array.new.tap do |a|
134
+ self.each do |k,v|
135
+ a << v.standardized
123
136
  end
124
137
  end
125
138
  end
126
139
  end
127
140
 
128
141
  class Array
142
+
143
+ # Attributizes each element in an array
129
144
  def attributized
130
145
  Array.new.tap do |a|
131
146
  self.each do |v|
@@ -134,4 +149,35 @@ class Array
134
149
  end
135
150
  end
136
151
  end
152
+ end
153
+
154
+ class ActiveSupport::HashWithIndifferentAccess
155
+
156
+ # Takes params that are passed in for nested_forms (like the example below) and cleans them up.
157
+ #
158
+ # post: {
159
+ # comments_attributes: {
160
+ # "0" => {},
161
+ # "1" => {},
162
+ # "new_23023032" => {}
163
+ # }
164
+ # }
165
+ #
166
+ def standardized
167
+ ActionController::Parameters.new.tap do |h|
168
+ self.each do |k,v|
169
+ h[k] = v.is_a?(Hash) ? v.to_parameter_array : v
170
+ end
171
+ end
172
+ end
173
+ end
174
+
175
+ class String
176
+ def is_i?
177
+ !!(self =~ /^[-+]?[0-9]+$/)
178
+ end
179
+
180
+ def is_hex?
181
+ !!(self =~ /^[0-9a-f]+$/)
182
+ end
137
183
  end
@@ -1,3 +1,3 @@
1
1
  module CancanStrongParameters
2
- VERSION = "0.1.4"
2
+ VERSION = "0.1.5"
3
3
  end
@@ -1,6 +1,11 @@
1
1
  class Post
2
2
  include ActiveModel::Serialization
3
3
  include ActiveModel::MassAssignmentSecurity
4
+ include ActiveModel::AttributeMethods
4
5
 
5
6
  attr_accessible :body, :content
7
+
8
+ def initialize(attributes = {})
9
+ @attributes = attributes
10
+ end
6
11
  end
@@ -42,4 +42,87 @@ class PostsControllerTest < ActionController::TestCase
42
42
  ActiveSupport::HashWithIndifferentAccess.new(assigns(:post_attributes)),
43
43
  ActiveSupport::HashWithIndifferentAccess.new(params[:post])
44
44
  end
45
+
46
+ test "can handle multiple items" do
47
+ params = {
48
+ post: {
49
+ title: "Hello",
50
+ comments_attributes: {
51
+ "0" => {
52
+ body: "Comment 1",
53
+ tags_attributes: {
54
+ "0" => {
55
+ name: "article"
56
+ },
57
+ "1" => {
58
+ name: "post"
59
+ },
60
+ }
61
+ },
62
+ "1" => {
63
+ body: "Comment 2"
64
+ },
65
+ "new_3904949" => {
66
+ body: "Comment 3"
67
+ }
68
+ }
69
+ }
70
+ }
71
+
72
+ post :create, params
73
+ assert_equal \
74
+ ActiveSupport::HashWithIndifferentAccess.new(assigns(:post_attributes)),
75
+ ActiveSupport::HashWithIndifferentAccess.new({
76
+ title: "Hello",
77
+ comments_attributes: [
78
+ {
79
+ body: "Comment 1",
80
+ tags_attributes: [{
81
+ name: "article"
82
+ },
83
+ {
84
+ name: "post"
85
+ }
86
+ ]
87
+ },
88
+ {
89
+ body: "Comment 2"
90
+ },
91
+ {
92
+ body: "Comment 3"
93
+ }
94
+ ]
95
+ })
96
+ end
97
+
98
+ test "can handle multiple items but with only new itesm" do
99
+ params = {
100
+ post: {
101
+ title: "Hello",
102
+ comments_attributes: {
103
+ "new_3904949" => {
104
+ body: "Comment 3",
105
+ tags_attributes: {
106
+ "new_23040234" => {
107
+ name: "article"
108
+ }
109
+ }
110
+ }
111
+ }
112
+ }
113
+ }
114
+
115
+ post :create, params
116
+ assert_equal \
117
+ ActiveSupport::HashWithIndifferentAccess.new(assigns(:post_attributes)),
118
+ ActiveSupport::HashWithIndifferentAccess.new({
119
+ title: "Hello",
120
+ comments_attributes: [{
121
+ body: "Comment 3",
122
+ tags_attributes: [{
123
+ name: "article"
124
+ }]
125
+ }]
126
+ })
127
+ end
45
128
  end
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.1.4
4
+ version: 0.1.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: