Class: Flame::Controller

Inherits:
Object
  • Object
show all
Extended by:
Actions, Forwardable
Includes:
PathTo, Memery
Defined in:
lib/flame/controller.rb,
lib/flame/controller/actions.rb,
lib/flame/controller/cookies.rb,
lib/flame/controller/path_to.rb

Overview

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

Defined Under Namespace

Modules: Actions, PathTo Classes: Cookies

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Actions

actions, inherit_actions, with_actions

Methods included from PathTo

#path_to, #path_to_back, #url_to

Constructor Details

#initialize(dispatcher) ⇒ Controller

Initialize the controller for request execution

Parameters:


56
57
58
# File 'lib/flame/controller.rb', line 56

def initialize(dispatcher)
	@dispatcher = dispatcher
end

Class Attribute Details

.path_argumentsObject

Returns the value of attribute path_arguments


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

def path_arguments
  @path_arguments
end

Class Method Details

.pathObject


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

def path
	return self::PATH if const_defined?(:PATH)

	default_path
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.

Examples:

Set Content-Disposition header without filename

attachment

Set Content-Disposition header with filename and Content-Type

attachment 'style.css'

Parameters:

  • filename (String, nil) (defaults to: nil)

    filename of attachment

  • disposition (Symbol, String) (defaults to: :attachment)

    main content for Content-Disposition


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

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

#cookiesObject

Cookies object as Hash


61
62
63
# File 'lib/flame/controller.rb', line 61

memoize def cookies
	Cookies.new(request.cookies, response)
end

#redirect(path, status) ⇒ nil #redirect(uri, status) ⇒ nil #redirect(*args, status) ⇒ nil

Redirect for response

Overloads:

  • #redirect(path, status) ⇒ nil

    Redirect to the string path

    Examples:

    Redirect to '/hello'

    redirect '/hello'

    Redirect to '/hello' with status 301

    redirect '/hello', 301

    Parameters:

    • path (String)

      path

    • status (Ingeter, nil)

      HTTP status

    Returns:

    • (nil)
  • #redirect(uri, status) ⇒ nil

    Redirect to the URI location

    Examples:

    Redirect to 'example.com'

    redirect URI::HTTP.build(host: 'example.com')

    Redirect to 'example.com' with status 301

    redirect URI::HTTP.build(host: 'example.com'), 301

    Parameters:

    • uri (URI)

      URI object

    • status (Ingeter, nil)

      HTTP status

    Returns:

    • (nil)
  • #redirect(*args, status) ⇒ nil

    Redirect to the path of `path_to` method

    Examples:

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

    redirect ArticlesController, :show, id: 2

    Redirect to method of controller with status 301

    redirect ArticlesController, :show, { id: 2 }, 301

    Parameters:

    • args

      arguments for `path_to` method

    • status (Ingeter, nil)

      HTTP status

    Returns:

    • (nil)

95
96
97
98
99
100
101
102
103
# File 'lib/flame/controller.rb', line 95

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

#view(path = nil, options = {}, &block) ⇒ 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


128
129
130
131
132
133
134
135
136
137
# File 'lib/flame/controller.rb', line 128

def view(path = nil, options = {}, &block)
	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, &block)
end