Class: YesController

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

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ YesController

Returns a new instance of YesController.



15
16
17
# File 'lib/yes_controller.rb', line 15

def initialize(env)
  @env = env
end

Class Attribute Details

.assetsObject

Returns the value of attribute assets.



3
4
5
# File 'lib/yes_controller.rb', line 3

def assets
  @assets
end

.before_actionsObject

Returns the value of attribute before_actions.



3
4
5
# File 'lib/yes_controller.rb', line 3

def before_actions
  @before_actions
end

Class Method Details

.before_action(*symbols) ⇒ Object



5
6
7
# File 'lib/yes_controller.rb', line 5

def before_action(*symbols)
  @before_actions = symbols
end

.inherited(subclass) ⇒ Object



9
10
11
12
# File 'lib/yes_controller.rb', line 9

def inherited(subclass)
  subclass.before_actions = @before_actions
  super
end

Instance Method Details

#csrf_nameObject



59
60
61
# File 'lib/yes_controller.rb', line 59

def csrf_name
  Rack::Csrf.field
end

#csrf_valueObject



55
56
57
# File 'lib/yes_controller.rb', line 55

def csrf_value
  Rack::Csrf.token(@env) if @env['rack.session']
end

#flashObject



19
20
21
# File 'lib/yes_controller.rb', line 19

def flash
  @flash ||= @env['rack.session']['__FLASH__']
end

#json(body = nil) ⇒ Object



39
40
41
# File 'lib/yes_controller.rb', line 39

def json(body = nil)
  ok body, { "content-type" => "application/json; charset=utf-8" }
end

#not_foundObject



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

def not_found
  [404, { "content-type" => "text/plain" }, ["not found"]]
end

#not_found!Object

Raises:

  • (NotFoundError)


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

def not_found!
  raise NotFoundError
end

#ok(body = nil, headers = { "content-type" => "text/html; charset=utf-8" }) ⇒ Object



31
32
33
# File 'lib/yes_controller.rb', line 31

def ok(body = nil, headers = { "content-type" => "text/html; charset=utf-8" })
  response(200, body, headers)
end

#paramsObject



51
52
53
# File 'lib/yes_controller.rb', line 51

def params
  @env["params"]
end

#path(*args) ⇒ Object



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

def path(*args)
  Object.const_get(:App).path(*args)
end

#redirect(controller_or_url, method_name = nil, params = {}) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/yes_controller.rb', line 43

def redirect(controller_or_url, method_name = nil, params = {})
  if method_name
    response 302, nil, { "Location" => path(controller_or_url, method_name, params) }
  else
    response 302, nil, { "Location" => controller_or_url }
  end
end

#render(view, layout: true) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/yes_controller.rb', line 83

def render(view, layout: true)
  view.csrf_name = csrf_name
  view.csrf_value = csrf_value
  view.assets = self.class.assets&.to_h || {}
  view.session = session
  view.ajax = @env.key?("HTTP_YES_AJAX")
  content = view.render(view.template)
  content = view.render(view.class.superclass.new.template, content:) if layout

  ok content
end

#response(status, body = nil, headers = { "content-type" => "text/html; charset=utf-8" }) ⇒ Object



27
28
29
# File 'lib/yes_controller.rb', line 27

def response(status, body = nil, headers = { "content-type" => "text/html; charset=utf-8" })
  [status, headers, [body].compact]
end

#server_errorObject



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

def server_error
  [500, { "content-type" => "text/plain" }, ["internal server error"]]
end

#server_error!Object

Raises:

  • (ServerError)


75
76
77
# File 'lib/yes_controller.rb', line 75

def server_error!
  raise ServerError
end

#sessionObject



23
24
25
# File 'lib/yes_controller.rb', line 23

def session
  @session ||= @env['rack.session']
end

#xml(body = nil) ⇒ Object



35
36
37
# File 'lib/yes_controller.rb', line 35

def xml(body = nil)
  ok body, { "content-type" => "text/xml; charset=utf-8" }
end