Class: Flame::Controller

Inherits:
Object
  • Object
show all
Defined in:
lib/flame/controller.rb

Overview

Class initialize when Dispatcher found route with it For new request and response

Defined Under Namespace

Modules: ParentActions

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dispatcher) ⇒ Controller

Initialize the controller for request execution

Parameters:



16
17
18
# File 'lib/flame/controller.rb', line 16

def initialize(dispatcher)
	@dispatcher = dispatcher
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object

Call helpers methods from ‘Flame::Dispatcher`



96
97
98
99
# File 'lib/flame/controller.rb', line 96

def method_missing(m, *args, &block)
	return super unless @dispatcher.respond_to?(m)
	@dispatcher.send(m, *args, &block)
end

Class Method Details

.actionsObject

Shortcut for not-inherited public methods: actions



10
11
12
# File 'lib/flame/controller.rb', line 10

def self.actions
	public_instance_methods(false)
end

.default_pathObject

Default root path of the controller for requests



117
118
119
120
121
122
# File 'lib/flame/controller.rb', line 117

def default_path
	modules = name.underscore.split('/')
	parts = modules[-1].split('_') - %w(index controller ctrl)
	return modules[-2] if parts.empty?
	parts.join('_')
end

.with_actionsObject

Re-define public instance method from parent

Examples:

Inherit controller with parent actions by method

class MyController < BaseController.with_actions
end


128
129
130
# File 'lib/flame/controller.rb', line 128

def with_actions
	@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.



51
52
53
54
55
56
57
58
# File 'lib/flame/controller.rb', line 51

def attachment(filename = nil, disposition = :attachment)
	content_dis = 'Content-Disposition'.freeze
	response[content_dis] = disposition.to_s
	return unless filename
	response[content_dis] << "; filename=\"#{File.basename(filename)}\""
	ext = File.extname(filename)
	content_type(ext) unless response[Rack::CONTENT_TYPE] || ext.empty?
end

#execute(method) ⇒ Object

Execute the method of the controller with hooks (may be overloaded)

Parameters:

  • method (Symbol)

    name of the controller method



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/flame/controller.rb', line 78

def execute(method)
	# send method
	body send(
		method,
		*params.values_at(
			*self.class.instance_method(method).parameters.map { |par| par[1] }
		)
	)
rescue => exception
	# p 'rescue from controller'
	status 500
	dump_error(exception)

	## Re-raise exception for inherited controllers or `Flame::Dispatcher`
	raise exception
end

#path_to(*args) ⇒ Object

Helpers



21
22
23
24
# File 'lib/flame/controller.rb', line 21

def path_to(*args)
	add_controller_class(args)
	@dispatcher.path_to(*args)
end

#redirect(path) ⇒ Object #redirect(*args) ⇒ Object

Redirect for response

Overloads:

  • #redirect(path) ⇒ Object

    Redirect to the string path

    Examples:

    Redirect to ‘/hello’

    redirect '/hello'

    Parameters:

    • path (String)

      path

  • #redirect(*args) ⇒ Object

    Redirect to the path of ‘path_to` method

    Examples:

    Redirect to ‘show` method of `ArticlesController` with id = 2

    redirect ArticlesController, :show, id: 2

    Parameters:

    • args

      arguments for ‘path_to` method



43
44
45
46
47
# File 'lib/flame/controller.rb', line 43

def redirect(*params)
	response.redirect(
		params[0].is_a?(String) ? params[0] : path_to(*params)
	)
end

#respond_to_missing?(m) ⇒ Boolean

Respond to Dispatcher methods

Returns:

  • (Boolean)


102
103
104
# File 'lib/flame/controller.rb', line 102

def respond_to_missing?(m, *)
	@dispatcher.respond_to?(m) || super
end

#url_to(*args) ⇒ Object

Build a URI to the given controller and action, or path



27
28
29
30
# File 'lib/flame/controller.rb', line 27

def url_to(*args)
	path = args.first.is_a?(String) ? args.first : path_to(*args)
	"#{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)

Parameters:

  • path (Symbol, nil) (defaults to: nil)

    path to the template file

  • options (Hash) (defaults to: {})

    options for the ‘Flame::Render` rendering

Returns:

  • (String)

    rendered template



64
65
66
67
68
69
70
71
72
73
# File 'lib/flame/controller.rb', line 64

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