Skip to content

Commit 8d1a6ca

Browse files
author
Jerry Cheung
committed
add helpers to README
1 parent d3a2dd2 commit 8d1a6ca

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

README.markdown

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,37 @@ To circumvent this default behaviour, one could use the `:strict` option. When t
8686

8787
Serialization takes place automatically. For more detailed usage information, please visit the [Grape Wiki](http://github.com/intridea/grape/wiki).
8888

89+
## Helpers
90+
91+
You can define helper methods that your endpoints can use with the `helpers`
92+
macro by either giving a block or a module:
93+
94+
````ruby
95+
module MyHelpers
96+
def say_hello(user)
97+
"hey there #{user.name}"
98+
end
99+
end
100+
101+
class API < Grape::API
102+
# define helpers with a block
103+
helpers do
104+
def current_user
105+
User.find(params[:user_id])
106+
end
107+
end
108+
109+
# or mix in a module
110+
helpers MyHelpers
111+
112+
get '/hello' do
113+
# helpers available in your endpoint and filters
114+
say_hello(current_user)
115+
end
116+
end
117+
````
118+
119+
89120
## Working with Entities
90121

91122
A common problem in designing Ruby APIs is that you probably don't want

lib/grape/api.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,9 @@ def represent(model_class, options)
163163
# When called without a block, all known helpers within this scope
164164
# are included.
165165
#
166+
# @param mod [Module] optional module of methods to include
167+
# @param &block [Block] optional block of methods to include
168+
#
166169
# @example Define some helpers.
167170
# class ExampleAPI < Grape::API
168171
# helpers do

0 commit comments

Comments
 (0)