Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
O
OHR Support
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
97
Issues
97
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
image/svg+xml
Discourse
Discourse
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Projects
OHR Support
Commits
9c2089c5
Commit
9c2089c5
authored
Mar 19, 2011
by
Eric Davis
Browse files
Options
Browse Files
Download
Plain Diff
Merge remote branch 'schmidt/f/282-enhance-safe_attributes' into upstream-master
parents
61be2ea6
0320517d
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
35 additions
and
4 deletions
+35
-4
safe_attributes.rb
lib/redmine/safe_attributes.rb
+8
-3
safe_attributes_test.rb
test/unit/lib/redmine/safe_attributes_test.rb
+27
-1
No files found.
lib/redmine/safe_attributes.rb
View file @
9c2089c5
...
...
@@ -31,14 +31,19 @@ module Redmine
def
safe_attributes
(
*
args
)
@safe_attributes
||=
[]
if
args
.
empty?
@safe_attributes
if
superclass
<
Redmine
::
SafeAttributes
superclass
.
safe_attributes
+
@safe_attributes
else
@safe_attributes
end
else
options
=
args
.
last
.
is_a?
(
Hash
)
?
args
.
pop
:
{}
@safe_attributes
<<
[
args
,
options
]
safe_attributes
end
end
end
# Returns an array that can be safely set by user or current user
#
# Example:
...
...
@@ -62,7 +67,7 @@ module Redmine
# # => {'title' => 'My book'}
def
delete_unsafe_attributes
(
attrs
,
user
=
User
.
current
)
safe
=
safe_attribute_names
(
user
)
attrs
.
dup
.
delete_if
{
|
k
,
v
|
!
safe
.
include?
(
k
)}
attrs
.
dup
.
delete_if
{
|
k
,
v
|
!
safe
.
include?
(
k
.
to_s
)}
end
# Sets attributes from attrs that are safe
...
...
test/unit/lib/redmine/safe_attributes_test.rb
View file @
9c2089c5
...
...
@@ -35,11 +35,16 @@ class Redmine::SafeAttributesTest < ActiveSupport::TestCase
end
class
Book
<
Base
attr_accessor
:title
attr_accessor
:title
,
:isbn
include
Redmine
::
SafeAttributes
safe_attributes
:title
end
class
PublishedBook
<
Book
safe_attributes
:isbn
end
def
test_safe_attribute_names
p
=
Person
.
new
assert_equal
[
'firstname'
,
'lastname'
],
p
.
safe_attribute_names
(
User
.
anonymous
)
...
...
@@ -84,4 +89,25 @@ class Redmine::SafeAttributesTest < ActiveSupport::TestCase
assert_equal
'Smith'
,
p
.
lastname
assert_equal
'jsmith'
,
p
.
login
end
def
test_with_indifferent_access
p
=
Person
.
new
p
.
safe_attributes
=
{
'firstname'
=>
'Jack'
,
:lastname
=>
'Miller'
}
assert_equal
'Jack'
,
p
.
firstname
assert_equal
'Miller'
,
p
.
lastname
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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment