Class: Racket::Controller

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

Overview

Base controller class. Your controllers should inherit this class.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.after(*methods, &blk) ⇒ nil

Adds a before hook to one or more actions. Actions should be given as a list of symbols. If no symbols are provided, all actions on the controller is affected.

Parameters:

  • methods (Array)

Returns:

  • (nil)


63
64
65
# File 'lib/racket/controller.rb', line 63

def self.after(*methods, &blk)
  __register_hook(:after, methods, blk) if block_given?
end

.before(*methods, &blk) ⇒ nil

Adds an after hook to one or more actions. Actions should be given as a list of symbols. If no symbols are provided, all actions on the controller is affected.

Parameters:

  • methods (Array)

Returns:

  • (nil)


72
73
74
# File 'lib/racket/controller.rb', line 72

def self.before(*methods, &blk)
  __register_hook(:before, methods, blk) if block_given?
end

.helper(*helpers) ⇒ nil

Adds one or more helpers to the controller. All controllers get some default helpers (:routing and :view by default), but if you have your own helpers you want to load this is the preferred method.

By default Racket will look for your helpers in the helpers directory, but you can specify another location by changing the helper_dir setting.

Parameters:

  • helpers (Array)

    An array of symbols representing classes living in the Racket::Helpers namespace.

Returns:

  • (nil)


86
87
88
89
90
91
92
93
94
95
96
# File 'lib/racket/controller.rb', line 86

def self.helper(*helpers)
  helper_modules = {}
  unless settings.fetch(:helpers)
    # No helpers has been loaded yet. Load the default helpers first.
    helper_modules.merge!(
      __helper_cache.load_helpers(Application.settings.default_controller_helpers)
    )
  end
  # Load new helpers
  __load_helpers(helpers.map(&:to_sym), helper_modules)
end

.inherited(klass) ⇒ Object

:nodoc:



111
112
113
# File 'lib/racket/controller.rb', line 111

def self.inherited(klass)
  Application.settings.fetch(:last_added_controller).push(klass)
end

.setting(key, value) ⇒ nil

Creates/updates a setting for the current controller class.

Parameters:

  • key (Symbol)
  • value (Object)

Returns:

  • (nil)


127
128
129
# File 'lib/racket/controller.rb', line 127

def self.setting(key, value)
  settings.store(key, value)
end

.settingsRacket::Settings::Controller

Returns the settings associated with the current controller class.



118
119
120
# File 'lib/racket/controller.rb', line 118

def self.settings
  @settings ||= Settings::Controller.new(self)
end

Instance Method Details

#__runString

Calls hooks, action and renderer.

Returns:

  • (String)


184
185
186
187
188
189
# File 'lib/racket/controller.rb', line 184

def __run
  __run_hook(:before)
  __run_action
  __run_hook(:after)
  Application.view_manager.render(self)
end

#redirect(target, status = 302) ⇒ Object

Redirects the client. After hooks are run.

Parameters:

  • target (String)

    URL to redirect to

  • status (Fixnum) (defaults to: 302)

    HTTP status to send

Returns:

  • (Object)


145
146
147
148
# File 'lib/racket/controller.rb', line 145

def redirect(target, status = 302)
  response.redirect(target, status)
  respond(response.status, response.headers, '')
end

#redirect!(target, status = 302) ⇒ Object

Redirects the client. After hooks are NOT run.

Parameters:

  • target (String)

    URL to redirect to

  • status (Fixnum) (defaults to: 302)

    HTTP status to send

Returns:

  • (Object)


155
156
157
158
# File 'lib/racket/controller.rb', line 155

def redirect!(target, status = 302)
  response.redirect(target, status)
  respond!(response.status, response.headers, '')
end

#respond(status = 200, headers = {}, body = '') ⇒ Object

Stop processing request and send a custom response. After calling this method, after hooks (but no rendering) will be run.

Parameters:

  • status (Fixnum) (defaults to: 200)
  • headers (Hash) (defaults to: {})
  • body (String) (defaults to: '')


166
167
168
169
# File 'lib/racket/controller.rb', line 166

def respond(status = 200, headers = {}, body = '')
  __run_hook(:after)
  respond!(status, headers, body)
end

#respond!(status = 200, headers = {}, body = '') ⇒ Object

Stop processing request and send a custom response. After calling this method, no further processing of the request is done.

Parameters:

  • status (Fixnum) (defaults to: 200)
  • headers (Hash) (defaults to: {})
  • body (String) (defaults to: '')


177
178
179
# File 'lib/racket/controller.rb', line 177

def respond!(status = 200, headers = {}, body = '')
  throw :response, [status, headers, body]
end

#settingsRacket::Settings::Controller

Returns the settings for a controller instance.



136
137
138
# File 'lib/racket/controller.rb', line 136

def settings
  self.class.settings
end