mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Whitelist all attribute assignment by default.
Change the default for newly generated applications to whitelist all attribute assignment. Also update the generated model classes so users are reminded of the importance of attr_accessible.
This commit is contained in:
parent
c8f6025fd3
commit
641a4f6240
4 changed files with 20 additions and 1 deletions
|
@ -30,6 +30,10 @@ module ActiveRecord
|
|||
attributes.select { |a| a.has_index? || (a.reference? && options[:indexes]) }
|
||||
end
|
||||
|
||||
def accessible_attributes
|
||||
attributes.reject(&:reference?)
|
||||
end
|
||||
|
||||
hook_for :test_framework
|
||||
|
||||
protected
|
||||
|
|
|
@ -3,5 +3,10 @@ class <%= class_name %> < <%= parent_class_name.classify %>
|
|||
<% attributes.select {|attr| attr.reference? }.each do |attribute| -%>
|
||||
belongs_to :<%= attribute.name %>
|
||||
<% end -%>
|
||||
<% if !accessible_attributes.empty? -%>
|
||||
attr_accessible <%= accessible_attributes.map {|a| ":#{a.name}" }.sort.join(', ') %>
|
||||
<% else -%>
|
||||
# attr_accessible :title, :body
|
||||
<% end -%>
|
||||
end
|
||||
<% end -%>
|
||||
|
|
|
@ -54,7 +54,7 @@ module <%= app_const_base %>
|
|||
# This will create an empty whitelist of attributes available for mass-assignment for all models
|
||||
# in your app. As such, your models will need to explicitly whitelist or blacklist accessible
|
||||
# parameters by using an attr_accessible or attr_protected declaration.
|
||||
# config.active_record.whitelist_attributes = true
|
||||
config.active_record.whitelist_attributes = true
|
||||
|
||||
# Specifies wether or not has_many or has_one association option :dependent => :restrict raises
|
||||
# an exception. If set to true, then an ActiveRecord::DeleteRestrictionError exception would be
|
||||
|
|
|
@ -317,4 +317,14 @@ class ModelGeneratorTest < Rails::Generators::TestCase
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
def test_attr_accessible_added_with_non_reference_attributes
|
||||
run_generator
|
||||
assert_file 'app/models/account.rb', /attr_accessible :age, :name/
|
||||
end
|
||||
|
||||
def test_attr_accessible_added_with_comments_when_no_attributes_present
|
||||
run_generator ["Account"]
|
||||
assert_file 'app/models/account.rb', /# attr_accessible :title, :body/
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue