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)


52
53
54
# File 'lib/racket/controller.rb', line 52

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)


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

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

.contextModule

Returns the current context.

Returns:

  • (Module)


68
69
70
# File 'lib/racket/controller.rb', line 68

def self.context
  Controller.instance_variable_get(:@context)
end

.context=(context) ⇒ Object

Injects context in Controller class. Context represents the current application state.

Parameters:

  • context (Module)


76
77
78
79
# File 'lib/racket/controller.rb', line 76

def self.context=(context)
  raise 'Context should only be set on Controller class' unless self == Controller
  @context = context
end

.get_route(action = nil, *params) ⇒ Object

Returns the route representing the parameters.

Parameters:

  • action (Symbol|nil) (defaults to: nil)
  • params (Array)


85
86
87
# File 'lib/racket/controller.rb', line 85

def self.get_route(action = nil, *params)
  context.get_route(self, action, params)
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)


99
100
101
102
103
104
105
106
107
108
109
# File 'lib/racket/controller.rb', line 99

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

.inherited(klass) ⇒ Object

:@private



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

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

.layout_settingsHash

Returns the layout settings for the current controller.

Returns:

  • (Hash)


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

def self.layout_settings
  template_settings = settings.fetch(:template_settings)
  template_settings[:common].merge(template_settings[:layout])
end

.setting(key, val) ⇒ Object

Add a setting for the current controller class

Parameters:

  • key (Symbol)
  • val (Object)


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

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

.settingsRacket::Settings::Controller

Returns the settings for the current controller class



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

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

.template_setting(key, value, type = :common) ⇒ Object

Add a setting used by Tilt when rendering views/layouts.

Parameters:

  • key (Symbol)
  • value (Object)
  • type (Symbol) (defaults to: :common)

    One of :common, :layout or :view



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/racket/controller.rb', line 144

def self.template_setting(key, value, type = :common)
  # If controller has no template settings on its own, copy the template settings
  # from its "closest" parent (might even be application settings)
  # @todo - How about template options that are unmarshallable?
  settings.store(
    :template_settings, Marshal.load(Marshal.dump(settings.fetch(:template_settings)))
  ) unless settings.present?(:template_settings)

  # Fetch current settings (guaranteed to be in controller by now)
  template_settings = settings.fetch(:template_settings)

  # Update settings
  template_settings[type][key] = value
  settings.store(:template_settings, template_settings)
end

.view_settingsHash

Returns the view settings for the current controller.

Returns:

  • (Hash)


163
164
165
166
# File 'lib/racket/controller.rb', line 163

def self.view_settings
  template_settings = settings.fetch(:template_settings)
  template_settings[:common].merge(template_settings[:view])
end

Instance Method Details

#__runString

Calls hooks, action and renderer.

Returns:

  • (String)


241
242
243
244
245
246
# File 'lib/racket/controller.rb', line 241

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

#layout_settingsObject

Returns layout settings associated with the current controller



183
184
185
# File 'lib/racket/controller.rb', line 183

def layout_settings
  self.class.layout_settings
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)


192
193
194
195
# File 'lib/racket/controller.rb', line 192

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)


202
203
204
205
# File 'lib/racket/controller.rb', line 202

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: '')


213
214
215
216
# File 'lib/racket/controller.rb', line 213

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: '')


224
225
226
# File 'lib/racket/controller.rb', line 224

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

#settingsObject

Returns settings associated with the current controller



229
230
231
# File 'lib/racket/controller.rb', line 229

def settings
  self.class.settings
end

#view_settingsObject

Returns view settings associated with the current controller



234
235
236
# File 'lib/racket/controller.rb', line 234

def view_settings
  self.class.view_settings
end