Class: Flame::Controller
- Inherits:
-
Object
- Object
- Flame::Controller
- Extended by:
- Forwardable
- Defined in:
- lib/flame/controller.rb
Overview
Class initialize when Dispatcher found route with it For new request and response
Defined Under Namespace
Modules: ModuleActions, ParentActions
Constant Summary collapse
- FORBIDDEN_ACTIONS =
[].freeze
Class Method Summary collapse
-
.actions ⇒ Object
Shortcut for not-inherited public methods: actions.
-
.default_path ⇒ Object
Default root path of the controller for requests.
-
.with_actions(mod = nil) ⇒ Object
Re-define public instance method from parent.
Instance Method Summary collapse
-
#attachment(filename = nil, disposition = :attachment) ⇒ Object
Set the Content-Disposition to “attachment” with the specified filename, instructing the user agents to prompt to save, and set Content-Type by filename.
-
#initialize(dispatcher) ⇒ Controller
constructor
Initialize the controller for request execution.
-
#path_to(*args) ⇒ Object
Helpers.
-
#redirect(*args) ⇒ Object
Redirect for response.
-
#url_to(*args, **options) ⇒ Object
Build a URI to the given controller and action, or path.
-
#view(path = nil, options = {}) ⇒ String
(also: #render)
Render a template with ‘Flame::Render` (based on Tilt-engine).
Constructor Details
#initialize(dispatcher) ⇒ Controller
Initialize the controller for request execution
27 28 29 |
# File 'lib/flame/controller.rb', line 27 def initialize(dispatcher) @dispatcher = dispatcher end |
Class Method Details
.actions ⇒ Object
Shortcut for not-inherited public methods: actions
15 16 17 |
# File 'lib/flame/controller.rb', line 15 def self.actions public_instance_methods(false) end |
.default_path ⇒ Object
Default root path of the controller for requests
176 177 178 179 180 181 182 183 |
# 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 |
.with_actions(mod = nil) ⇒ Object
Re-define public instance method from parent
195 196 197 198 |
# 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 |
Instance Method Details
#attachment(filename = nil, disposition = :attachment) ⇒ Object
Set the Content-Disposition to “attachment” with the specified filename, instructing the user agents to prompt to save, and set Content-Type by filename.
97 98 99 100 101 102 103 104 |
# File 'lib/flame/controller.rb', line 97 def (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 |
#path_to(*args) ⇒ Object
Helpers
32 33 34 35 |
# File 'lib/flame/controller.rb', line 32 def path_to(*args) add_controller_class(args) @dispatcher.path_to(*args) end |
#redirect(path, status) ⇒ nil #redirect(uri, status) ⇒ nil #redirect(*args, status) ⇒ nil
Redirect for response
78 79 80 81 82 83 84 85 86 |
# 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 |
#url_to(*args, **options) ⇒ Object
Build a URI to the given controller and action, or path
38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/flame/controller.rb', line 38 def url_to(*args, **) 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: [:version]) else path_to(*args, **) end "#{request.scheme}://#{request.host_with_port}#{path}" end |
#view(path = nil, options = {}) ⇒ String Also known as: render
Render a template with ‘Flame::Render` (based on Tilt-engine)
110 111 112 113 114 115 116 117 118 119 |
# File 'lib/flame/controller.rb', line 110 def view(path = nil, = {}) cache = .delete(:cache) cache = config[:environment] == 'production' if cache.nil? template = Flame::Render.new( self, (path || caller_locations(1, 1)[0].label.to_sym), ) template.render(cache: cache) end |