Skip to content

Commit acbd3f9

Browse files
committed
implement strong parameters instead of protected attributes
1 parent 98c1680 commit acbd3f9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+178
-96
lines changed

Gemfile

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ gem 'actionpack-xml_parser', '~>1.0.0'
1010
gem 'actionview-encoded_mail_to', '~>1.0.4'
1111
gem 'activerecord-session_store', '~>0.0.1'
1212
gem 'activeresource', '~>4.0.0.beta1'
13-
gem 'protected_attributes', '~>1.0.1'
13+
# gem 'protected_attributes', '~>1.0.1'
1414
gem 'rails-observers', '~>0.1.1'
1515
gem 'rails-perftest', '~>0.0.2'
1616

@@ -30,7 +30,7 @@ gem 'pg'
3030

3131
gem 'figaro' # for handling config via ENV variables
3232

33-
gem 'cancan' # for checking member privileges
33+
gem 'cancancan', '~> 1.9' # for checking member privileges
3434

3535
gem 'gibbon' # for Mailchimp newsletter subscriptions
3636

Gemfile.lock

+2-5
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ GEM
7979
columnize (~> 0.8)
8080
debugger-linecache (~> 1.2)
8181
slop (~> 3.6)
82-
cancan (1.6.10)
82+
cancancan (1.9.2)
8383
capybara (2.4.4)
8484
mime-types (>= 1.16)
8585
nokogiri (>= 1.3.3)
@@ -223,8 +223,6 @@ GEM
223223
cliver (~> 0.3.1)
224224
multi_json (~> 1.0)
225225
websocket-driver (>= 0.2.0)
226-
protected_attributes (1.0.8)
227-
activemodel (>= 4.0.1, < 5.0)
228226
pry (0.10.1)
229227
coderay (~> 1.1.0)
230228
method_source (~> 0.8.1)
@@ -356,7 +354,7 @@ DEPENDENCIES
356354
bootstrap-datepicker-rails
357355
bundler (>= 1.1.5)
358356
byebug
359-
cancan
357+
cancancan (~> 1.9)
360358
capybara
361359
capybara-email
362360
coffee-rails (~> 4.1.0)
@@ -392,7 +390,6 @@ DEPENDENCIES
392390
omniauth-twitter
393391
pg
394392
poltergeist (~> 1.5.1)
395-
protected_attributes (~> 1.0.1)
396393
pry
397394
rails (= 4.1.7)
398395
rails-observers (~> 0.1.1)

app/controllers/account_types_controller.rb

+8-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def edit
3434

3535
# POST /account_types
3636
def create
37-
@account_type = AccountType.new(params[:account_type])
37+
@account_type = AccountType.new(account_type_params)
3838

3939
respond_to do |format|
4040
if @account_type.save
@@ -50,7 +50,7 @@ def update
5050
@account_type = AccountType.find(params[:id])
5151

5252
respond_to do |format|
53-
if @account_type.update_attributes(params[:account_type])
53+
if @account_type.update(account_type_params)
5454
format.html { redirect_to @account_type, notice: 'Account type was successfully updated.' }
5555
else
5656
format.html { render action: "edit" }
@@ -67,4 +67,10 @@ def destroy
6767
format.html { redirect_to account_types_url, notice: 'Account type was successfully deleted.' }
6868
end
6969
end
70+
71+
private
72+
73+
def account_type_params
74+
params.require(:account_type).permit(:is_paid, :is_permanent_paid, :name)
75+
end
7076
end

app/controllers/accounts_controller.rb

+7-1
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,18 @@ def update
2828
@account = Account.find(params[:id])
2929

3030
respond_to do |format|
31-
if @account.update_attributes(params[:account])
31+
if @account.update(params[:account])
3232
format.html { redirect_to @account, notice: 'Account detail was successfully updated.' }
3333
else
3434
format.html { render action: "edit" }
3535
end
3636
end
3737
end
3838

39+
private
40+
41+
def account_params
42+
params.require(:account).permit(:account_type_id, :member_id, :paid_until)
43+
end
44+
3945
end

app/controllers/alternate_names_controller.rb

+8-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def edit
4444
# POST /alternate_names.json
4545
def create
4646
params[:alternate_name][:creator_id] = current_member.id
47-
@alternate_name = AlternateName.new(params[:alternate_name])
47+
@alternate_name = AlternateName.new(alternate_name_params)
4848

4949
respond_to do |format|
5050
if @alternate_name.save
@@ -63,7 +63,7 @@ def update
6363
@alternate_name = AlternateName.find(params[:id])
6464

6565
respond_to do |format|
66-
if @alternate_name.update_attributes(params[:alternate_name])
66+
if @alternate_name.update(alternate_name_params)
6767
format.html { redirect_to @alternate_name.crop, notice: 'Alternate name was successfully updated.' }
6868
format.json { head :no_content }
6969
else
@@ -87,4 +87,10 @@ def destroy
8787
format.json { head :no_content }
8888
end
8989
end
90+
91+
private
92+
93+
def alternate_name_params
94+
params.require(:alternate_name).permit(:crop_id, :name, :creator_id)
95+
end
9096
end

app/controllers/application_controller.rb

+11-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,17 @@ def extract_locale_from_subdomain
4848
protected
4949

5050
def configure_permitted_parameters
51-
devise_parameter_sanitizer.for(:sign_up) {|u| u.permit(:login_name, :email, :password, :password_confirmation, :tos_agreement, :newsletter) }
51+
devise_parameter_sanitizer.for(:sign_up) do |u|
52+
u.permit(:login_name, :email, :password, :password_confirmation,
53+
:remember_me, :login,
54+
# terms of service
55+
:tos_agreement,
56+
# profile stuff
57+
:bio, :location, :latitude, :longitude,
58+
# email settings
59+
:show_email, :newsletter, :send_notification_email, :send_planting_reminder
60+
)
61+
end
5262
end
5363

5464
end

app/controllers/comments_controller.rb

+8-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def edit
5454
# POST /comments.json
5555
def create
5656
params[:comment][:author_id] = current_member.id
57-
@comment = Comment.new(params[:comment])
57+
@comment = Comment.new(comment_params)
5858

5959
respond_to do |format|
6060
if @comment.save
@@ -78,7 +78,7 @@ def update
7878
params[:comment].delete("author_id")
7979

8080
respond_to do |format|
81-
if @comment.update_attributes(params[:comment])
81+
if @comment.update(comment_params)
8282
format.html { redirect_to @comment.post, notice: 'Comment was successfully updated.' }
8383
format.json { head :no_content }
8484
else
@@ -100,4 +100,10 @@ def destroy
100100
format.json { head :no_content }
101101
end
102102
end
103+
104+
private
105+
106+
def comment_params
107+
params.require(:comment).permit(:author_id, :body, :post_id)
108+
end
103109
end

app/controllers/crops_controller.rb

+8-2
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ def edit
103103
# POST /crops.json
104104
def create
105105
params[:crop][:creator_id] = current_member.id
106-
@crop = Crop.new(params[:crop])
106+
@crop = Crop.new(crop_params)
107107

108108
respond_to do |format|
109109
if @crop.save
@@ -122,7 +122,7 @@ def update
122122
@crop = Crop.find(params[:id])
123123

124124
respond_to do |format|
125-
if @crop.update_attributes(params[:crop])
125+
if @crop.update(crop_params)
126126
format.html { redirect_to @crop, notice: 'Crop was successfully updated.' }
127127
format.json { head :no_content }
128128
else
@@ -143,4 +143,10 @@ def destroy
143143
format.json { head :no_content }
144144
end
145145
end
146+
147+
private
148+
149+
def crop_params
150+
require(:crop).permit(:en_wikipedia_url, :name, :parent_id, :creator_id, :scientific_names_attributes)
151+
end
146152
end

app/controllers/forums_controller.rb

+8-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def edit
4444
# POST /forums
4545
# POST /forums.json
4646
def create
47-
@forum = Forum.new(params[:forum])
47+
@forum = Forum.new(forum_params)
4848

4949
respond_to do |format|
5050
if @forum.save
@@ -63,7 +63,7 @@ def update
6363
@forum = Forum.find(params[:id])
6464

6565
respond_to do |format|
66-
if @forum.update_attributes(params[:forum])
66+
if @forum.update(forum_params)
6767
format.html { redirect_to @forum, notice: 'Forum was successfully updated.' }
6868
format.json { head :no_content }
6969
else
@@ -84,4 +84,10 @@ def destroy
8484
format.json { head :no_content }
8585
end
8686
end
87+
88+
private
89+
90+
def forum_params
91+
params.require(:forum).permit(:description, :name, :owner_id, :slug)
92+
end
8793
end

app/controllers/gardens_controller.rb

+9-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def edit
4949
# POST /gardens.json
5050
def create
5151
params[:garden][:owner_id] = current_member.id
52-
@garden = Garden.new(params[:garden])
52+
@garden = Garden.new(garden_params)
5353

5454
respond_to do |format|
5555
if @garden.save
@@ -68,7 +68,7 @@ def update
6868
@garden = Garden.find(params[:id])
6969

7070
respond_to do |format|
71-
if @garden.update_attributes(params[:garden])
71+
if @garden.update(garden_params)
7272
format.html { redirect_to @garden, notice: 'Garden was successfully updated.' }
7373
format.json { head :no_content }
7474
else
@@ -89,4 +89,11 @@ def destroy
8989
format.json { head :no_content }
9090
end
9191
end
92+
93+
private
94+
95+
def garden_params
96+
params.require(:garden).permit(:name, :slug, :owner_id, :description, :active,
97+
:location, :latitude, :longitude, :area, :area_unit)
98+
end
9299
end

app/controllers/harvests_controller.rb

+9-2
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def edit
6161
def create
6262
params[:harvest][:owner_id] = current_member.id
6363
params[:harvested_at] = parse_date(params[:harvested_at])
64-
@harvest = Harvest.new(params[:harvest])
64+
@harvest = Harvest.new(harvest_params)
6565

6666
respond_to do |format|
6767
if @harvest.save
@@ -80,7 +80,7 @@ def update
8080
@harvest = Harvest.find(params[:id])
8181

8282
respond_to do |format|
83-
if @harvest.update_attributes(params[:harvest])
83+
if @harvest.update(harvest_params)
8484
format.html { redirect_to @harvest, notice: 'Harvest was successfully updated.' }
8585
format.json { head :no_content }
8686
else
@@ -101,4 +101,11 @@ def destroy
101101
format.json { head :no_content }
102102
end
103103
end
104+
105+
private
106+
107+
def harvest_params
108+
params.require(:harvest).permit(:crop_id, :harvested_at, :description, :owner_id,
109+
:quantity, :unit, :weight_quantity, :weight_unit, :plant_part_id, :slug)
110+
end
104111
end

app/controllers/notifications_controller.rb

+7-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def destroy
4747
# POST /notifications
4848
def create
4949
params[:notification][:sender_id] = current_member.id
50-
@notification = Notification.new(params[:notification])
50+
@notification = Notification.new(notification_params)
5151
@recipient = Member.find_by_id(params[:notification][:recipient_id])
5252

5353
respond_to do |format|
@@ -58,4 +58,10 @@ def create
5858
end
5959
end
6060
end
61+
62+
private
63+
64+
def notification_params
65+
params.require(:notification).permit(:sender_id, :recipient_id, :subject, :body, :post_id, :read)
66+
end
6167
end

app/controllers/order_items_controller.rb

+7-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ def create
66
if params[:order_item][:price]
77
params[:order_item][:price] = params[:order_item][:price].to_f * 100 # convert to cents
88
end
9-
@order_item = OrderItem.new(params[:order_item])
9+
@order_item = OrderItem.new(order_item_params)
1010
@order_item.order = current_member.current_order || Order.create(:member_id => current_member.id)
1111

1212
respond_to do |format|
@@ -19,4 +19,10 @@ def create
1919
end
2020
end
2121
end
22+
23+
private
24+
25+
def order_item_params
26+
params.require(:order_item).permit(:order_id, :price, :product_id, :quantity)
27+
end
2228
end

app/controllers/photos_controller.rb

+9-2
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def edit
6060
# POST /photos.json
6161
def create
6262
@photo = Photo.find_by_flickr_photo_id(params[:photo][:flickr_photo_id]) ||
63-
Photo.new(params[:photo])
63+
Photo.new(photo_params)
6464
@photo.owner_id = current_member.id
6565
@photo.set_flickr_metadata
6666

@@ -110,7 +110,7 @@ def update
110110
@photo = Photo.find(params[:id])
111111

112112
respond_to do |format|
113-
if @photo.update_attributes(params[:photo])
113+
if @photo.update(photo_params)
114114
format.html { redirect_to @photo, notice: 'Photo was successfully updated.' }
115115
format.json { head :no_content }
116116
else
@@ -131,4 +131,11 @@ def destroy
131131
format.json { head :no_content }
132132
end
133133
end
134+
135+
private
136+
137+
def photo_params
138+
params.require(:photo).permit(:flickr_photo_id, :owner_id, :title, :license_name,
139+
:license_url, :thumbnail_url, :fullsize_url, :link_url)
140+
end
134141
end

app/controllers/plant_parts_controller.rb

+8-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def edit
4242
# POST /plant_parts
4343
# POST /plant_parts.json
4444
def create
45-
@plant_part = PlantPart.new(params[:plant_part])
45+
@plant_part = PlantPart.new(plant_part_params)
4646

4747
respond_to do |format|
4848
if @plant_part.save
@@ -61,7 +61,7 @@ def update
6161
@plant_part = PlantPart.find(params[:id])
6262

6363
respond_to do |format|
64-
if @plant_part.update_attributes(params[:plant_part])
64+
if @plant_part.update(plant_part_params)
6565
format.html { redirect_to @plant_part, notice: 'Plant part was successfully updated.' }
6666
format.json { head :no_content }
6767
else
@@ -82,4 +82,10 @@ def destroy
8282
format.json { head :no_content }
8383
end
8484
end
85+
86+
private
87+
88+
def plant_part_params
89+
params.require(:plant_part).permit(:name, :slug)
90+
end
8591
end

0 commit comments

Comments
 (0)