Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Jérome Perrin
gitlab-ce
Commits
b2c771f4
Commit
b2c771f4
authored
Oct 13, 2016
by
Rémy Coutable
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add an API styleguide
Signed-off-by:
Rémy Coutable
<
remy@rymai.me
>
parent
626d5e55
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
60 additions
and
0 deletions
+60
-0
doc/development/README.md
doc/development/README.md
+2
-0
doc/development/api_styleguide.md
doc/development/api_styleguide.md
+58
-0
No files found.
doc/development/README.md
View file @
b2c771f4
...
...
@@ -8,6 +8,8 @@
## Styleguides
-
[
API styleguide
](
api_styleguide.md
)
Use this styleguide if you are
contributing to the API.
-
[
Documentation styleguide
](
doc_styleguide.md
)
Use this styleguide if you are
contributing to documentation.
-
[
SQL Migration Style Guide
](
migration_style_guide.md
)
for creating safe SQL migrations
...
...
doc/development/api_styleguide.md
0 → 100644
View file @
b2c771f4
# API styleguide
This styleguide recommends best practices for the API development.
## Declared params
> Grape allows you to access only the parameters that have been declared by your
`params`
block. It filters out the params that have been passed, but are not
allowed.
– https://github.com/ruby-grape/grape#declared
### Exclude params from parent namespaces
> By default `declared(params) `includes parameters that were defined in all
parent namespaces.
– https://github.com/ruby-grape/grape#include-parent-namespaces
In most cases you will want to exclude params from the parent namespaces:
```
ruby
declared
(
params
,
include_parent_namespaces:
false
)
```
### When to use `declared(params)`?
You should always use
`declared(params)`
when you pass the params hash as
arguments to a method call.
For instance:
```
ruby
# bad
User
.
create
(
params
)
# imagine the user submitted `admin=1`... :)
# good
User
.
create
(
declared
(
params
,
include_parent_namespaces:
false
).
to_h
)
```
>**Note:**
`declared(params)`
return a
`Hashie::Mash`
object, on which you will have to
call
`.to_h`
.
But we can use directly
`params[key]`
when we access single elements.
For instance:
```
ruby
# good
Model
.
create
(
foo:
params
[
:foo
])
```
>**Note:**
Since you
[
should use Grape's DSL to declare params
](
doc_styleguide.md#method-description
)
, [parameters validation and
coercion] comes for free!
[
parameters validation and coercion
]:
https://github.com/ruby-grape/grape#parameter-validation-and-coercion
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