class Flame::Controller
Class initialize when Dispatcher
found route with it For new request and response
Constants
- FORBIDDEN_ACTIONS
Public Class Methods
Shortcut for not-inherited public methods: actions
# File lib/flame/controller.rb, line 15 def self.actions public_instance_methods(false) end
Initialize the controller for request execution @param dispatcher [Flame::Dispatcher] dispatcher object
# File lib/flame/controller.rb, line 27 def initialize(dispatcher) @dispatcher = dispatcher end
Private Class Methods
Default root path of the controller for requests
# File lib/flame/controller.rb, line 176 def default_path modules = underscore.split('/') parts = modules.pop.split('_') parts.shift if parts.first == 'index' parts.pop if %w[controller ctrl].include? parts.last parts = [modules.last] if parts.empty? Flame::Path.merge nil, parts.join('_') end
Re-define public instance method from parent @example Inherit controller with parent actions by method
class MyController < BaseController.with_actions end
@example Define actions from module in controller
class MyController < BaseController include with_actions Module1 include with_actions Module2 .... end
# File lib/flame/controller.rb, line 195 def with_actions(mod = nil) return mod.extend(ModuleActions) if mod @with_actions ||= Class.new(self) { extend ParentActions } end
Public Instance Methods
Set the Content-Disposition to “attachment” with the specified filename, instructing the user agents to prompt to save, and set Content-Type by filename. @param filename [String, nil] filename of attachment @param disposition [Symbol, String] main content for Content-Disposition @example Set Content-Disposition header without filename
attachment
@example Set Content-Disposition header with filename and Content-Type
attachment 'style.css'
# File lib/flame/controller.rb, line 97 def attachment(filename = nil, disposition = :attachment) content_dis = 'Content-Disposition' response[content_dis] = disposition.to_s return unless filename response[content_dis] << "; filename=\"#{File.basename(filename)}\"" ext = File.extname(filename) response.content_type = ext unless ext.empty? end
Helpers
# File lib/flame/controller.rb, line 32 def path_to(*args) add_controller_class(args) @dispatcher.path_to(*args) end
Redirect for response @overload redirect(path, status)
Redirect to the string path @param path [String] path @param status [Ingeter, nil] HTTP status @return [nil] @example Redirect to '/hello' redirect '/hello' @example Redirect to '/hello' with status 301 redirect '/hello', 301
@overload redirect(uri, status)
Redirect to the URI location @param uri [URI] URI object @param status [Ingeter, nil] HTTP status @return [nil] @example Redirect to 'http://example.com' redirect URI::HTTP.build(host: 'example.com') @example Redirect to 'http://example.com' with status 301 redirect URI::HTTP.build(host: 'example.com'), 301
@overload redirect(*args, status)
Redirect to the path of `path_to` method @param args arguments for `path_to` method @param status [Ingeter, nil] HTTP status @return [nil] @example Redirect to `show` method of `ArticlesController` with id = 2 redirect ArticlesController, :show, id: 2 @example Redirect to method of controller with status 301 redirect ArticlesController, :show, { id: 2 }, 301
# File lib/flame/controller.rb, line 78 def redirect(*args) args[0] = args.first.to_s if args.first.is_a? URI unless args.first.is_a? String path_to_args_range = 0..(args.last.is_a?(Integer) ? -2 : -1) args[path_to_args_range] = path_to(*args[path_to_args_range]) end response.redirect(*args) status end
Build a URI to the given controller and action, or path
# File lib/flame/controller.rb, line 38 def url_to(*args, **options) first_arg = args.first path = if first_arg.is_a?(String) || first_arg.is_a?(Flame::Path) static_file = find_static(first_arg) static_file.path(with_version: options[:version]) else path_to(*args, **options) end "#{request.scheme}://#{request.host_with_port}#{path}" end
Render
a template with `Flame::Render` (based on Tilt-engine) @param path [Symbol, nil] path to the template file @param options [Hash] options for the `Flame::Render` rendering @return [String] rendered template
# File lib/flame/controller.rb, line 110 def view(path = nil, options = {}) cache = options.delete(:cache) cache = config[:environment] == 'production' if cache.nil? template = Flame::Render.new( self, (path || caller_locations(1, 1)[0].label.to_sym), options ) template.render(cache: cache) end
Protected Instance Methods
Execute the method of the controller with hooks (may be overloaded) @param method [Symbol] name of the controller method
# File lib/flame/controller.rb, line 126 def execute(method) body send(method, *extract_params_for(method)) end
Default method for Internal Server Error, can be inherited
# File lib/flame/controller.rb, line 131 def server_error(_exception) body default_body end
Private Instance Methods
# File lib/flame/controller.rb, line 167 def add_controller_class(args) args.unshift(self.class) if args[0].is_a?(Symbol) args.insert(1, :index) if args[0].is_a?(Class) && !args[1].is_a?(Symbol) end
# File lib/flame/controller.rb, line 152 def extract_params_for(action) # Take parameters from action method parameters = method(action).parameters # Fill variables with values from params req_values, opt_values = %i[req opt].map! do |type| params.values_at( *parameters.select { |key, _value| key == type }.map!(&:last) ) end # Remove nils from the end of optional values opt_values.pop while opt_values.last.nil? && !opt_values.empty? # Concat values req_values + opt_values end
Execute any action from any controller @example Execute `new` action of `ArticlesController`
reroute ArticlesController, :new
@example Execute `index` action of `ArticlesController`
reroute ArticlesController
@example Execute `foo` action of current controller
reroute :foo
# File lib/flame/controller.rb, line 144 def reroute(*args) add_controller_class(args) ctrl, action = args[0..1] ctrl_object = ctrl == self.class ? self : ctrl.new(@dispatcher) ctrl_object.send :execute, action body end