Skip to content

Commit

Permalink
Merge pull request #572 from johnallen3d/parameter-documentation
Browse files Browse the repository at this point in the history
Added documentation support to parameters
  • Loading branch information
dblock committed Feb 4, 2014
2 parents c890d57 + 4516cd1 commit bc0ad13
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Next Release
* [#545](https://github.com/intridea/grape/pull/545): Added `type` (Array or Hash) support to `requires`, `optional` and `group` - [@bwalex](https://github.com/bwalex).
* [#550](https://github.com/intridea/grape/pull/550): Added possibility to define reusable params - [@dm1try](https://github.com/dm1try).
* [#560](https://github.com/intridea/grape/pull/560): Use `Grape::Entity` documentation to define required and optional parameters with `requires using:` - [@reynardmh](https://github.com/reynardmh).
* [#572](https://github.com/intridea/grape/pull/572): Added `documentation` support to `requires`, `optional` and `group` parameters - [@johnallen3d](https://github.com/johnallen3d).
* Your contribution here.

#### Fixes
Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,17 @@ class API < Grape::API
end
```

## Parameter Documentation

You can attach additional documentation to `params` using a `documentation` hash.

```ruby
params do
optional :first_name, type: String, documentation: { example: 'Jim' }
requires :last_name, type: String, documentation: { example: 'Smith' }
end
```

## Cookies

You can set, get and delete your cookies very simply using `cookies` method.
Expand Down
2 changes: 2 additions & 0 deletions lib/grape/validations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,8 @@ def validates(attrs, validations)
raise Grape::Exceptions::IncompatibleOptionValues.new(:type, coerce_type, :values, values)
end

doc_attrs[:documentation] = validations.delete(:documentation) if validations.key?(:documentation)

full_attrs = attrs.collect { |name| { name: name, full_name: full_name(name) } }
@api.document_attribute(full_attrs, doc_attrs)

Expand Down
14 changes: 14 additions & 0 deletions spec/grape/validations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -823,5 +823,19 @@ module SharedParams

end
end

context 'documentation' do
it 'can be included with a hash' do
documentation = { example: 'Joe' }

subject.params do
requires 'first_name', documentation: documentation
end
subject.get '/' do
end

subject.routes.first.route_params['first_name'][:documentation].should eq(documentation)
end
end
end
end

0 comments on commit bc0ad13

Please sign in to comment.