Class: Happy::Controller

Inherits:
Object
  • Object
show all
Includes:
Actions, Configurable, Permissions, Rackable, Routing, Helpers
Defined in:
lib/happy/controller.rb,
lib/happy/controller/actions.rb,
lib/happy/controller/routing.rb,
lib/happy/controller/rackable.rb,
lib/happy/controller/permissions.rb,
lib/happy/controller/configurable.rb

Overview

Base class for Happy controllers. A controller's primary job is to act upon an incoming request, navigating the request URL's path, and finally deciding on a course of action (eg. rendering something, redirecting the client, passing control over to another controller, and so on.)

Defined Under Namespace

Modules: Actions, Configurable, Permissions, Rackable, Routing

Constant Summary collapse

CASCADING_SETTINGS =
[:views]

Instance Attribute Summary collapse

Attributes included from Helpers::Rendering

#output_buffer

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers::I18n

#localize, #translate

Methods included from Helpers::Rendering

#capture_template_block, #concat_output, #render, #render_resource, #render_template, #with_output_buffer

Methods included from Helpers::Html

#escape_html, #html_tag, #html_tag_attributes, #link_to, #preserve, #url_for

Methods included from Permissions

#permissions

Methods included from Configurable

included, #set, #settings

Methods included from Rackable

#handle_request, included

Methods included from Actions

#cache_control, #content_type, #halt!, #header, #layout, #max_age, #only_if_path_matches, #redirect!, #run, #serve!

Methods included from Routing

#on, #path_to_regexp

Constructor Details

#initialize(env_or_parent = {}, opts = {}, &blk) ⇒ Controller

Creates a new instance of Happy::Controller. When a block is provided, it is run against the new instance, allowing custom controller classes to provide DSL-like configuration.

Parameters:

  • env_or_parent (Hash, Controller) (defaults to: {})

    Rack environment hash or parent controller.

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

    Options to be merged with the controller's default (class-level) settings.


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/happy/controller.rb', line 37

def initialize(env_or_parent = {}, opts = {}, &blk)
  if env_or_parent.is_a?(Happy::Controller)
    @parent_controller = env_or_parent
    @env = @parent_controller.env
    @unprocessed_path = env_or_parent.unprocessed_path
    @processed_path = env_or_parent.processed_path
  else
    @env = env_or_parent
    @unprocessed_path = request.path.split('/').reject {|s| s.empty? }
    @processed_path  = []
  end

  # Augment this instance's settings hash with the hash given to this constructor
  settings.merge!(opts)

  # Copy missing settings from our parent
  if @parent_controller
    CASCADING_SETTINGS.each do |name|
      settings[name] ||= @parent_controller.settings[name]
    end
  end

  # Save a copy of the current path as this controller's root path.
  @root_url = processed_path.join('/')

  # Execute block against this instance, allowing the controller to
  # provide a DSL for configuration.
  instance_exec(&blk) if blk
end

Instance Attribute Details

#envObject (readonly)

Returns the value of attribute env


24
25
26
# File 'lib/happy/controller.rb', line 24

def env
  @env
end

#processed_pathObject (readonly, protected)

Returns the value of attribute processed_path


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

def processed_path
  @processed_path
end

#unprocessed_pathObject (readonly, protected)

Returns the value of attribute unprocessed_path


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

def unprocessed_path
  @unprocessed_path
end

Class Method Details

.helpers(*args, &blk) ⇒ Object (private)

Adds helper methods to the base class for all controllers. Use this to define application-level helper methods that you want to have available in all views, even if they're begin rendered by a controller different from your application controller.

Modules passed to this method will be included into the base controller class. Example:

helpers MyApp::Helpers

Alternatively, you can specify a block that will be executed against the base controller class like this:

helpers do
  def my_little_helper
    "something useful"
  end
end

111
112
113
114
115
116
117
118
119
120
# File 'lib/happy/controller.rb', line 111

def self.helpers(*args, &blk)
  args.flatten.each do |arg|
    case arg
      when Module then Happy::Controller.send(:include, arg)
      else raise "Invalid helper specified."
    end
  end

  Happy::Controller.class_exec(&blk) if blk
end

Instance Method Details

#appObject (protected)

Returns the application controller (ie, the root controller running this application.)


86
87
88
# File 'lib/happy/controller.rb', line 86

def app
  @parent_controller ? @parent_controller.app : self
end

#current_url(*extras) ⇒ Object (private)


122
123
124
# File 'lib/happy/controller.rb', line 122

def current_url(*extras)
  url_for(processed_path, extras)
end

#paramsObject (private)


130
# File 'lib/happy/controller.rb', line 130

def params; request.params; end

#requestObject


67
68
69
# File 'lib/happy/controller.rb', line 67

def request
  @env['happy.request'] ||= Happy::Request.new(@env)
end

#responseObject


71
72
73
# File 'lib/happy/controller.rb', line 71

def response
  @env['happy.response'] ||= Happy::Response.new
end

#root_url(*extras) ⇒ Object (protected)


79
80
81
# File 'lib/happy/controller.rb', line 79

def root_url(*extras)
  url_for(@root_url, extras)
end

#routeObject (private)


126
127
128
# File 'lib/happy/controller.rb', line 126

def route  # override this in subclasses

end

#sessionObject (private)


131
# File 'lib/happy/controller.rb', line 131

def session; request.session; end