Module: Grape::DSL::Helpers::ClassMethods

Defined in:
lib/grape/dsl/helpers.rb

Instance Method Summary collapse

Instance Method Details

#helpers(*new_modules, &block) ⇒ Object

Add helper methods that will be accessible from any endpoint within this namespace (and child namespaces).

When called without a block, all known helpers within this scope are included.

Examples:

Define some helpers.


class ExampleAPI < Grape::API
  helpers do
    def current_user
      User.find_by_id(params[:token])
    end
  end
end

Include many modules


class ExampleAPI < Grape::API
  helpers Authentication, Mailer, OtherModule
end

Parameters:

  • new_modules (Array)

    optional array of modules to include

  • block (Block)

    optional block of methods to include


35
36
37
38
39
# File 'lib/grape/dsl/helpers.rb', line 35

def helpers(*new_modules, &block)
  include_new_modules(new_modules) if new_modules.any?
  include_block(block) if block
  include_all_in_scope if !block && new_modules.empty?
end