Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add ability to use Grape::Entity documentation in the params block #560

Closed
wants to merge 5 commits into from

Conversation

Nerian
Copy link

@Nerian Nerian commented Jan 28, 2014

You can use entity documentation in the params block

class User::Entity < Grape::Entity
  expose :id,          documentation: { type: Integer}
  expose :name,        documentation: { type: String, desc: "Name" }
  expose :email,       documentation: { type: String, desc: "Email address" }
end

class Users < Grape::API
  desc 'Create user'
  params do
    requires :all, except: [:name], using: User::Entity.documentation.except(:id)
  end
  post 'users' do
    # implement create user
  end
end

validates(attrs, validations)
opts = attrs.last.is_a?(Hash) ? attrs.pop : nil
if opts && opts[:using]
if attrs.first == :all
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is getting a bit dense. Maybe try to refactor this into a function or two? Same for the else block.

@dblock
Copy link
Member

dblock commented Jan 28, 2014

This looks very good. Also need a CHANGELOG update, please. Thx.

@Nerian
Copy link
Author

Nerian commented Jan 29, 2014

Cool, changes done :)

@Nerian
Copy link
Author

Nerian commented Jan 29, 2014

I created a new pull request (can't change repo/branch a pull request):

#562

@reynardmh
Copy link
Contributor

Sorry for the delay finishing the test. It should be ready to merge now.
Note: To avoid dependency with Grape::Entity, I wrote the test using hash to define the entity doc. This should provide more flexibility for people who don't use Grape::Entity.

@dblock
Copy link
Member

dblock commented Jan 30, 2014

Thanks! Merged via f5d7306. Can you please double-check it, I rewrote the README section to be a bit simpler.

@dblock dblock closed this Jan 30, 2014
@reynardmh
Copy link
Contributor

Thanks!
Did you change Array(opts[:except]) back to opts[:except].is_a?(Array) ? opts[:except] : [opts[:except]].compact, or my refactoring reynardmh@3805c79 didn't make it to the pull request?
It's better to use Array(opts[:except])right?

@dblock
Copy link
Member

dblock commented Jan 31, 2014

Check master. If anything is missing, sorry about that, and please make another PR.

@dblock
Copy link
Member

dblock commented Jan 31, 2014

Def Array() is better.

qqshfox added a commit to qqshfox/grape that referenced this pull request Jul 4, 2014
The `requires` statement should be in a `params` block according to ruby-grape#560.
@qqshfox qqshfox mentioned this pull request Jul 4, 2014
@kuraga
Copy link
Contributor

kuraga commented Dec 2, 2014

Is using a bad name for documentation (i.e.) option, isn't it?

@dblock
Copy link
Member

dblock commented Dec 2, 2014

Yes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants