[#282] Make safe_attributes work with sub classes
This commit is contained in:
parent
40dbfd7744
commit
b5b3684473
|
@ -31,10 +31,15 @@ module Redmine
|
||||||
def safe_attributes(*args)
|
def safe_attributes(*args)
|
||||||
@safe_attributes ||= []
|
@safe_attributes ||= []
|
||||||
if args.empty?
|
if args.empty?
|
||||||
@safe_attributes
|
if superclass < Redmine::SafeAttributes
|
||||||
|
superclass.safe_attributes + @safe_attributes
|
||||||
|
else
|
||||||
|
@safe_attributes
|
||||||
|
end
|
||||||
else
|
else
|
||||||
options = args.last.is_a?(Hash) ? args.pop : {}
|
options = args.last.is_a?(Hash) ? args.pop : {}
|
||||||
@safe_attributes << [args, options]
|
@safe_attributes << [args, options]
|
||||||
|
safe_attributes
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -35,11 +35,16 @@ class Redmine::SafeAttributesTest < ActiveSupport::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
class Book < Base
|
class Book < Base
|
||||||
attr_accessor :title
|
attr_accessor :title, :isbn
|
||||||
include Redmine::SafeAttributes
|
include Redmine::SafeAttributes
|
||||||
safe_attributes :title
|
safe_attributes :title
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
class PublishedBook < Book
|
||||||
|
safe_attributes :isbn
|
||||||
|
end
|
||||||
|
|
||||||
def test_safe_attribute_names
|
def test_safe_attribute_names
|
||||||
p = Person.new
|
p = Person.new
|
||||||
assert_equal ['firstname', 'lastname'], p.safe_attribute_names(User.anonymous)
|
assert_equal ['firstname', 'lastname'], p.safe_attribute_names(User.anonymous)
|
||||||
|
@ -84,4 +89,18 @@ class Redmine::SafeAttributesTest < ActiveSupport::TestCase
|
||||||
assert_equal 'Smith', p.lastname
|
assert_equal 'Smith', p.lastname
|
||||||
assert_equal 'jsmith', p.login
|
assert_equal 'jsmith', p.login
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_use_safe_attributes_in_subclasses
|
||||||
|
b = Book.new
|
||||||
|
p = PublishedBook.new
|
||||||
|
|
||||||
|
b.safe_attributes = {'title' => 'My awesome Ruby Book', 'isbn' => '1221132343'}
|
||||||
|
p.safe_attributes = {'title' => 'The Pickaxe', 'isbn' => '1934356085'}
|
||||||
|
|
||||||
|
assert_equal 'My awesome Ruby Book', b.title
|
||||||
|
assert_nil b.isbn
|
||||||
|
|
||||||
|
assert_equal 'The Pickaxe', p.title
|
||||||
|
assert_equal '1934356085', p.isbn
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue