Skip to content

Less active support concern #2574

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

Merged
merged 1 commit into from
Jun 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions lib/grape/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ class API
# Class methods that we want to call on the API rather than on the API object
NON_OVERRIDABLE = %i[call call! configuration compile! inherited recognize_path].freeze

Helpers = Grape::DSL::Helpers::BaseHelper

class Boolean
def self.build(val)
return nil if val != true && val != false
Expand All @@ -15,10 +17,6 @@ def self.build(val)
end
end

class Instance
Boolean = Grape::API::Boolean
end

class << self
extend Forwardable
attr_accessor :base_instance, :instances
Expand Down
9 changes: 0 additions & 9 deletions lib/grape/api/helpers.rb

This file was deleted.

14 changes: 12 additions & 2 deletions lib/grape/api/instance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,18 @@ class API
# The API Instance class, is the engine behind Grape::API. Each class that inherits
# from this will represent a different API instance
class Instance
extend Grape::DSL::Settings
extend Grape::DSL::Desc
extend Grape::DSL::Validations
extend Grape::DSL::Callbacks
extend Grape::DSL::Logger
extend Grape::DSL::Middleware
extend Grape::DSL::RequestResponse
extend Grape::DSL::Routing
extend Grape::DSL::Helpers
extend Grape::Middleware::Auth::DSL
include Grape::DSL::API

Boolean = Grape::API::Boolean

class << self
attr_reader :instance, :base
Expand Down Expand Up @@ -128,7 +138,7 @@ def evaluate_as_instance_with_configuration(block, lazy: false)
def inherited(subclass)
super
subclass.reset!
subclass.logger = logger.clone
subclass.logger logger.clone
end

def inherit_settings(other_settings)
Expand Down
17 changes: 0 additions & 17 deletions lib/grape/dsl/api.rb

This file was deleted.

66 changes: 8 additions & 58 deletions lib/grape/dsl/callbacks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,66 +2,16 @@

module Grape
module DSL
# Blocks can be executed before or after every API call, using `before`, `after`,
# `before_validation` and `after_validation`.
#
# Before and after callbacks execute in the following order:
#
# 1. `before`
# 2. `before_validation`
# 3. _validations_
# 4. `after_validation`
# 5. _the API call_
# 6. `after`
#
# Steps 4, 5 and 6 only happen if validation succeeds.
module Callbacks
extend ActiveSupport::Concern
# before: execute the given block before validation, coercion, or any endpoint
# before_validation: execute the given block after `before`, but prior to validation or coercion
# after_validation: execute the given block after validations and coercions, but before any endpoint code
# after: execute the given block after the endpoint code has run except in unsuccessful
# finally: execute the given block after the endpoint code even if unsuccessful

include Grape::DSL::Configuration

module ClassMethods
# Execute the given block before validation, coercion, or any endpoint
# code is executed.
def before(&block)
namespace_stackable(:befores, block)
end

# Execute the given block after `before`, but prior to validation or
# coercion.
def before_validation(&block)
namespace_stackable(:before_validations, block)
end

# Execute the given block after validations and coercions, but before
# any endpoint code.
def after_validation(&block)
namespace_stackable(:after_validations, block)
end

# Execute the given block after the endpoint code has run.
def after(&block)
namespace_stackable(:afters, block)
end

# Allows you to specify a something that will always be executed after a call
# API call. Unlike the `after` block, this code will run even on
# unsuccesful requests.
# @example
# class ExampleAPI < Grape::API
# before do
# ApiLogger.start
# end
# finally do
# ApiLogger.close
# end
# end
#
# This will make sure that the ApiLogger is opened and closed around every
# request
# @param ensured_block [Proc] The block to be executed after every api_call
def finally(&block)
namespace_stackable(:finallies, block)
%w[before before_validation after_validation after finally].each do |callback_method|
define_method callback_method.to_sym do |&block|
namespace_stackable(callback_method.pluralize.to_sym, block)
end
end
end
Expand Down
15 changes: 0 additions & 15 deletions lib/grape/dsl/configuration.rb

This file was deleted.

2 changes: 0 additions & 2 deletions lib/grape/dsl/desc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
module Grape
module DSL
module Desc
include Grape::DSL::Settings

ROUTE_ATTRIBUTES = %i[
body_name
consumes
Expand Down
121 changes: 58 additions & 63 deletions lib/grape/dsl/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,81 +3,76 @@
module Grape
module DSL
module Helpers
extend ActiveSupport::Concern
include Grape::DSL::Configuration

module ClassMethods
# 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.
#
# @param [Array] new_modules optional array of modules to include
# @param [Block] block optional block of methods to include
#
# @example Define some helpers.
#
# class ExampleAPI < Grape::API
# helpers do
# def current_user
# User.find_by_id(params[:token])
# end
# end
# end
#
# @example Include many modules
#
# class ExampleAPI < Grape::API
# helpers Authentication, Mailer, OtherModule
# end
#
def helpers(*new_modules, &block)
include_new_modules(new_modules)
include_block(block)
include_all_in_scope if !block && new_modules.empty?
end
# 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.
#
# @param [Array] new_modules optional array of modules to include
# @param [Block] block optional block of methods to include
#
# @example Define some helpers.
#
# class ExampleAPI < Grape::API
# helpers do
# def current_user
# User.find_by_id(params[:token])
# end
# end
# end
#
# @example Include many modules
#
# class ExampleAPI < Grape::API
# helpers Authentication, Mailer, OtherModule
# end
#
def helpers(*new_modules, &block)
include_new_modules(new_modules)
include_block(block)
include_all_in_scope if !block && new_modules.empty?
end

protected
protected

def include_new_modules(modules)
return if modules.empty?
def include_new_modules(modules)
return if modules.empty?

modules.each { |mod| make_inclusion(mod) }
end
modules.each { |mod| make_inclusion(mod) }
end

def include_block(block)
return unless block
def include_block(block)
return unless block

Module.new.tap do |mod|
make_inclusion(mod) { mod.class_eval(&block) }
end
Module.new.tap do |mod|
make_inclusion(mod) { mod.class_eval(&block) }
end
end

def make_inclusion(mod, &block)
define_boolean_in_mod(mod)
inject_api_helpers_to_mod(mod, &block)
namespace_stackable(:helpers, mod)
end
def make_inclusion(mod, &block)
define_boolean_in_mod(mod)
inject_api_helpers_to_mod(mod, &block)
namespace_stackable(:helpers, mod)
end

def include_all_in_scope
Module.new.tap do |mod|
namespace_stackable(:helpers).each { |mod_to_include| mod.include mod_to_include }
change!
end
def include_all_in_scope
Module.new.tap do |mod|
namespace_stackable(:helpers).each { |mod_to_include| mod.include mod_to_include }
change!
end
end

def define_boolean_in_mod(mod)
return if defined? mod::Boolean
def define_boolean_in_mod(mod)
return if defined? mod::Boolean

mod.const_set(:Boolean, Grape::API::Boolean)
end
mod.const_set(:Boolean, Grape::API::Boolean)
end

def inject_api_helpers_to_mod(mod, &block)
mod.extend(BaseHelper) unless mod.is_a?(BaseHelper)
yield if block
mod.api_changed(self)
end
def inject_api_helpers_to_mod(mod, &block)
mod.extend(BaseHelper) unless mod.is_a?(BaseHelper)
yield if block
mod.api_changed(self)
end

# This module extends user defined helpers
Expand Down
4 changes: 0 additions & 4 deletions lib/grape/dsl/inside_route.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@
module Grape
module DSL
module InsideRoute
extend ActiveSupport::Concern
include Grape::DSL::Settings
include Grape::DSL::Headers

# Denotes a situation where a DSL method has been invoked in a
# filter which it should not yet be available in
class MethodNotYetAvailable < StandardError; end
Expand Down
4 changes: 0 additions & 4 deletions lib/grape/dsl/logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@
module Grape
module DSL
module Logger
include Grape::DSL::Settings

attr_writer :logger

# Set or retrive the configured logger. If none was configured, this
# method will create a new one, logging to stdout.
# @param logger [Object] the new logger to use
Expand Down
Loading