Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
R
rails_param
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
ikcrm_common
rails_param
Commits
7b7c37bd
Commit
7b7c37bd
authored
Oct 28, 2014
by
cole bradley
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add recursion feature for validating nested attributes
add tests for nested attributes
parent
86c2ca0a
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
83 additions
and
2 deletions
+83
-2
lib/rails_param/param.rb
+20
-2
spec/fixtures/controllers.rb
+14
-0
spec/fixtures/fake_rails_application.rb
+1
-0
spec/rails_integration_spec.rb
+48
-0
spec/rails_param/param_spec.rb
+0
-0
No files found.
lib/rails_param/param.rb
View file @
7b7c37bd
...
...
@@ -7,6 +7,13 @@ module RailsParam
attr_accessor
:param
,
:options
end
class
MockController
include
RailsParam
::
Param
attr_accessor
:params
# def params
# end
end
def
param!
(
name
,
type
,
options
=
{})
name
=
name
.
to_s
...
...
@@ -17,8 +24,19 @@ module RailsParam
params
[
name
]
=
(
options
[
:default
].
call
if
options
[
:default
].
respond_to?
(
:call
))
||
options
[
:default
]
if
params
[
name
].
nil?
and
options
[
:default
]
params
[
name
]
=
options
[
:transform
].
to_proc
.
call
(
params
[
name
])
if
params
[
name
]
and
options
[
:transform
]
validate!
(
params
[
name
],
options
)
if
block_given?
controller
=
RailsParam
::
Param
::
MockController
.
new
controller
.
params
=
params
[
name
]
# begin
yield
(
controller
)
# exception.param, exception.options = name, options
# raise InvalidParameterError exception
# end
end
rescue
InvalidParameterError
=>
exception
exception
.
param
,
exception
.
options
=
name
,
options
exception
.
param
||=
name
exception
.
options
||=
options
raise
exception
end
end
...
...
@@ -58,7 +76,7 @@ module RailsParam
return
(
/(false|f|no|n|0)$/i
===
param
.
to_s
?
false
:
(
/(true|t|yes|y|1)$/i
===
param
.
to_s
?
true
:
nil
))
if
type
==
TrueClass
||
type
==
FalseClass
||
type
==
:boolean
if
type
==
BigDecimal
param
=
param
.
delete
(
'$,'
).
strip
.
to_f
if
param
.
is_a?
(
String
)
return
BigDecimal
.
new
(
param
,
(
options
[
:precision
]
||
DEFAULT_PRECISION
))
if
type
==
BigDecimal
return
BigDecimal
.
new
(
param
,
(
options
[
:precision
]
||
DEFAULT_PRECISION
))
end
return
nil
rescue
ArgumentError
...
...
spec/fixtures/controllers.rb
View file @
7b7c37bd
...
...
@@ -17,4 +17,18 @@ class FakeController < ActionController::Base
def
new
render
text:
"new"
end
def
edit
param!
:book
,
Hash
,
required:
true
do
|
b
|
b
.
param!
:title
,
String
,
required:
true
b
.
param!
:author
,
Hash
do
|
a
|
a
.
param!
:first_name
,
String
,
required:
true
a
.
param!
:last_name
,
String
,
required:
true
a
.
param!
:age
,
Integer
,
required:
true
end
b
.
param!
:price
,
BigDecimal
,
required:
true
end
render
text: :book
end
end
spec/fixtures/fake_rails_application.rb
View file @
7b7c37bd
...
...
@@ -15,6 +15,7 @@ module Rails
get
'/fake/new'
=>
"fake#new"
get
'/fakes'
=>
"fake#index"
get
'/fake/(:id)'
=>
"fake#show"
get
'/fake/edit'
=>
"fake#edit"
end
@routes
end
...
...
spec/rails_integration_spec.rb
View file @
7b7c37bd
...
...
@@ -10,6 +10,54 @@ describe FakeController, type: :controller do
end
end
describe
"nested_hash"
do
it
"validates nested properties"
do
params
=
{
'book'
=>
{
'title'
=>
'One Hundred Years of Solitude'
,
'author'
=>
{
'first_name'
=>
'Garbriel Garcia'
,
'last_name'
=>
'Marquez'
,
'age'
=>
'70'
},
'price'
=>
'$1,000.00'
}}
get
:edit
,
params
expect
(
controller
.
params
[
:book
][
:author
][
:age
]).
to
eql
70
expect
(
controller
.
params
[
:book
][
:author
][
:age
]).
to
be_kind_of
Integer
expect
(
controller
.
params
[
:book
][
:price
]).
to
eql
1000.0
expect
(
controller
.
params
[
:book
][
:price
]).
to
be_instance_of
BigDecimal
end
it
"raises error when required nested attribute missing"
do
params
=
{
'book'
=>
{
'title'
=>
'One Hundred Years of Solitude'
,
'author'
=>
{
'last_name'
=>
'Marquez'
,
'age'
=>
'70'
},
'price'
=>
'$1,000.00'
}}
expect
{
get
:edit
,
params
}.
to
raise_error
{
|
error
|
expect
(
error
).
to
be_a
(
RailsParam
::
Param
::
InvalidParameterError
)
expect
(
error
.
param
).
to
eql
(
"first_name"
)
expect
(
error
.
options
).
to
eql
({
:required
=>
true
})
}
end
it
"passes when hash that's not required but has required attributes is missing"
do
params
=
{
'book'
=>
{
'title'
=>
'One Hundred Years of Solitude'
,
'price'
=>
'$1,000.00'
}}
get
:edit
,
params
expect
(
controller
.
params
[
:book
][
:price
]).
to
eql
1000.0
expect
(
controller
.
params
[
:book
][
:price
]).
to
be_instance_of
BigDecimal
end
end
describe
"InvalidParameterError"
do
it
"raises an exception with params attributes"
do
expect
{
get
:index
,
sort:
"foo"
}.
to
raise_error
{
|
error
|
...
...
spec/rails_param/param_spec.rb
View file @
7b7c37bd
This diff is collapsed.
Click to expand it.
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