Class: Flame::Controller

Inherits:
Object
  • Object
show all
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: ParentActions

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dispatcher) ⇒ Controller

Initialize the controller for request execution

Parameters:



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

def initialize(dispatcher)
  @dispatcher = dispatcher
end

Class Method Details

.actions ⇒ Object

Shortcut for not-inherited public methods: actions



13
14
15
# File 'lib/flame/controller.rb', line 13

def self.actions
  public_instance_methods(false)
end

.default_path ⇒ Object

Default root path of the controller for requests



167
168
169
170
171
172
173
174
# File 'lib/flame/controller.rb', line 167

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 ⇒ Object

Re-define public instance method from parent

Examples:

Inherit controller with parent actions by method

class MyController < BaseController.with_actions
end


180
181
182
# File 'lib/flame/controller.rb', line 180

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, 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



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

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

#path_to(*args) ⇒ Object

Helpers



30
31
32
33
# File 'lib/flame/controller.rb', line 30

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

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 'http://example.com'

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

    Redirect to 'http://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)


75
76
77
78
79
80
81
82
83
# File 'lib/flame/controller.rb', line 75

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)
  nil
end

#url_to(*args) ⇒ Object

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



36
37
38
39
40
41
42
43
44
45
# File 'lib/flame/controller.rb', line 36

def url_to(*args)
  first_arg = args.first
  path =
    if first_arg.is_a?(String) || first_arg.is_a?(Flame::Path)
      first_arg
    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)

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



107
108
109
110
111
112
113
114
115
116
# File 'lib/flame/controller.rb', line 107

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