-
Notifications
You must be signed in to change notification settings - Fork 7
Helpers
Alexander Popov edited this page Feb 9, 2021
·
1 revision
Helpers are just modules with helping private methods for Controllers:
# controllers/api_controller.rb
module MyProject
class APIController < Flame::Controller
include APIHelper
def index
{ result: 'success', data: 'Welcome!' }
end
end
end
# helpers/api_helper.rb
module MyProject
module APIHelper
# Method for after-hook in controller above
def to_json(content = body)
response.headers[Rack::CONTENT_TYPE] = 'application/json; charset=utf-8'
body JSON.generate(content)
end
protected
# Method for overriding controller executing (any route)
# Must be protected
def execute(method)
# before hooks
check_access!
# You can also check which method is calling (`method` argument)
super # execute calling method
# after hooks
to_json body
end
end
end
But often such things can be just private methods of the correct base controller.