Class: Reins::Controller

Inherits:
Object
  • Object
show all
Includes:
Routes::UrlHelpers, View::Forms, View::Helpers
Defined in:
lib/reins/controller.rb

Defined Under Namespace

Classes: FormatCollector

Constant Summary

Constants included from View::Helpers

View::Helpers::VOID_ELEMENTS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from View::Forms

#form_with, #hidden_field, #label, #submit, #text_area, #text_field

Methods included from View::Helpers

#image_tag, #javascript_include_tag, #link_to, #stylesheet_link_tag, #tag, #url_for

Methods included from Routes::UrlHelpers

define_for, expand_path, reset!

Constructor Details

#initialize(env) ⇒ Controller

Returns a new instance of Controller.



93
94
95
96
# File 'lib/reins/controller.rb', line 93

def initialize(env)
  @env = env
  @routing_params = {}
end

Instance Attribute Details

#envObject (readonly)

Returns the value of attribute env.



91
92
93
# File 'lib/reins/controller.rb', line 91

def env
  @env
end

Class Method Details

.action(act, routing_params = {}) ⇒ Object



57
58
59
# File 'lib/reins/controller.rb', line 57

def action(act, routing_params = {})
  proc { |env| new(env).dispatch(act, routing_params) }
end

.after_action(name, only: nil, except: nil) ⇒ Object



20
21
22
# File 'lib/reins/controller.rb', line 20

def after_action(name, only: nil, except: nil)
  own_filters[:after] << build_filter(name, only, except)
end

.all_filters(kind) ⇒ Object



53
54
55
# File 'lib/reins/controller.rb', line 53

def all_filters(kind)
  controller_ancestors.flat_map { |a| a.own_filters[kind] }
end

.around_action(name, only: nil, except: nil) ⇒ Object



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

def around_action(name, only: nil, except: nil)
  own_filters[:around] << build_filter(name, only, except)
end

.before_action(name, only: nil, except: nil) ⇒ Object



16
17
18
# File 'lib/reins/controller.rb', line 16

def before_action(name, only: nil, except: nil)
  own_filters[:before] << build_filter(name, only, except)
end

.filters_for(kind, action) ⇒ Object



45
46
47
# File 'lib/reins/controller.rb', line 45

def filters_for(kind, action)
  all_filters(kind).select { |f| filter_applies?(f, action) }
end

.layout(name, only: nil, except: nil) ⇒ Object



28
29
30
# File 'lib/reins/controller.rb', line 28

def layout(name, only: nil, except: nil)
  own_layouts << { name: name, only: array_or_nil(only), except: array_or_nil(except) }
end

.layout_for(action) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/reins/controller.rb', line 32

def layout_for(action)
  controller_ancestors.each do |a|
    a.own_layouts.each do |entry|
      return entry[:name] if filter_applies?(entry, action)
    end
  end
  :default
end

.own_filtersObject



49
50
51
# File 'lib/reins/controller.rb', line 49

def own_filters
  @own_filters ||= { before: [], after: [], around: [] }
end

.own_layoutsObject



41
42
43
# File 'lib/reins/controller.rb', line 41

def own_layouts
  @own_layouts ||= []
end

Instance Method Details

#controller_nameObject



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

def controller_name
  Reins.to_underscore(self.class.to_s.gsub(/Controller$/, ""))
end

#dispatch(action, routing_params = {}) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/reins/controller.rb', line 98

def dispatch(action, routing_params = {})
  @routing_params = routing_params
  @action = action.to_sym

  around_filters = self.class.filters_for(:around, @action)
  run_around(around_filters) do
    run_before
    if @response.nil?
      send(@action)
      run_after
    end
  end

  auto_render if @response.nil?
  sweep_flash
  rack_tuple
end

#flashObject



137
138
139
# File 'lib/reins/controller.rb', line 137

def flash
  @flash ||= Reins::Flash.new(session)
end

#head(status, **headers) ⇒ Object



156
157
158
159
160
161
# File 'lib/reins/controller.rb', line 156

def head(status, **headers)
  ensure_no_response!
  hdrs = { "content-type" => "text/html" }
  headers.each { |k, v| hdrs[k.to_s.tr("_", "-")] = v.to_s }
  build_response(status_code(status), "", hdrs)
end

#paramsObject



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

def params
  @params ||= Reins::Parameters.new(request.params.merge(@routing_params))
end

#redirect_to(url, status: 302) ⇒ Object



151
152
153
154
# File 'lib/reins/controller.rb', line 151

def redirect_to(url, status: 302)
  ensure_no_response!
  build_response(status_code(status), "", "location" => url.to_s)
end

#render(view = nil, plain: nil, html: nil, json: nil, template: nil, status: nil, locals: {}, layout: :inherit) ⇒ Object



141
142
143
144
145
146
147
148
149
# File 'lib/reins/controller.rb', line 141

def render(view = nil, plain: nil, html: nil, json: nil, template: nil, status: nil, locals: {},
           layout: :inherit)
  ensure_no_response!
  layout_choice = layout == :inherit ? self.class.layout_for(@action) : layout
  body, content_type = render_body_and_type(view, plain: plain, html: html, json: json,
                                                  template: template, locals: locals,
                                                  layout: layout_choice)
  build_response(status_code(status || 200), body, "content-type" => content_type)
end

#requestObject



116
117
118
# File 'lib/reins/controller.rb', line 116

def request
  @request ||= Rack::Request.new(@env)
end

#reset_sessionObject



133
134
135
# File 'lib/reins/controller.rb', line 133

def reset_session
  session.clear
end

#respond_to(&block) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
# File 'lib/reins/controller.rb', line 163

def respond_to(&block)
  collector = FormatCollector.new
  block.call(collector)

  format = pick_format(@env["HTTP_ACCEPT"], collector)
  if format
    invoke_format_handler(collector, format)
  else
    head 406
  end
end

#response(text, status = 200, headers = {}) ⇒ Object



175
176
177
178
# File 'lib/reins/controller.rb', line 175

def response(text, status = 200, headers = {})
  ensure_no_response!
  build_response(status, [text].flatten, headers)
end

#sessionObject



124
125
126
127
128
129
130
131
# File 'lib/reins/controller.rb', line 124

def session
  unless @env["rack.session"]
    raise Reins::SessionMiddlewareMissing,
          'Rack session middleware is required. Add `use Rack::Session::Cookie, secret: ...` to config.ru.'
  end

  @env["rack.session"]
end