Class: Radical::Controller

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/radical/controller.rb

Direct Known Subclasses

Controller

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request, options: {}) ⇒ Controller

Returns a new instance of Controller.



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

def initialize(request, options: {})
  @request = request
  @options = options
end

Class Attribute Details

.skip_csrf_actionsObject

Returns the value of attribute skip_csrf_actions.



21
22
23
# File 'lib/radical/controller.rb', line 21

def skip_csrf_actions
  @skip_csrf_actions
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#requestObject

Returns the value of attribute request.



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

def request
  @request
end

Class Method Details

.action_to_http_method(action) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/radical/controller.rb', line 61

def action_to_http_method(action)
  case action
  when :index, :show, :new, :edit
    'GET'
  when :create
    'POST'
  when :update
    'PATCH'
  when :destroy
    'DELETE'
  end
end

.action_to_url(action) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/radical/controller.rb', line 47

def action_to_url(action)
  case action
  when :index, :create
    "/#{route_name}"
  when :show, :update, :destroy
    "/#{route_name}/:id"
  when :new
    "/#{route_name}/new"
  when :edit
    "/#{route_name}/:id/edit"
  end
end

.layout(name) ⇒ Object



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

def layout(name)
  View.layout name
end

.prepend_view_path(path) ⇒ Object



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

def prepend_view_path(path)
  View.path path
end

.route_nameObject



33
34
35
# File 'lib/radical/controller.rb', line 33

def route_name
  Strings.snake_case to_s.split('::').last.gsub(/Controller$/, '')
end

.skip_csrf(*actions) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/radical/controller.rb', line 38

def skip_csrf(*actions)
  @skip_csrf_actions = [] if @skip_csrf_actions.nil?

  actions.each do |action|
    @skip_csrf_actions << "#{action_to_http_method(action)}:#{action_to_url(action)}"
  end
end

Instance Method Details

#assets_path(type) ⇒ Object



136
137
138
139
140
141
142
143
144
# File 'lib/radical/controller.rb', line 136

def assets_path(type)
  assets = options[:assets]

  if Env.production?
    compiled_assets_path(assets, type)
  else
    not_compiled_assets_path(assets, type)
  end
end

#compiled_assets_path(assets, type) ⇒ Object



146
147
148
149
150
151
152
# File 'lib/radical/controller.rb', line 146

def compiled_assets_path(assets, type)
  if type == :css
    link_tag(assets.compiled[:css])
  else
    script_tag(assets.compiled[:js])
  end
end

#flashObject



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

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

#form(options, &block) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
# File 'lib/radical/controller.rb', line 109

def form(options, &block)
  f = Form.new(options, self)

  capture(block) do
    emit f.open_tag
    emit f.csrf_tag
    emit f.rack_override_tag
    yield f
    emit f.close_tag
  end
end

#head(status) ⇒ Object



84
85
86
# File 'lib/radical/controller.rb', line 84

def head(status)
  Rack::Response.new(nil, Rack::Utils::SYMBOL_TO_STATUS_CODE[status])
end

#not_compiled_assets_path(assets, type) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
# File 'lib/radical/controller.rb', line 154

def not_compiled_assets_path(assets, type)
  if type == :css
    assets.assets[:css].map do |asset|
      link_tag("/assets/#{type}/#{asset}")
    end.join("\n")
  else
    assets.assets[:js].map do |asset|
      script_tag("/assets/#{type}/#{asset}")
    end.join("\n")
  end
end

#paramsObject



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

def params
  @request.params
end

#partial(name, locals = {}) ⇒ Object



104
105
106
# File 'lib/radical/controller.rb', line 104

def partial(name, locals = {})
  View.render(self.class.route_name, "_#{name}", self, { locals: locals, layout: false })
end

#plain(body) ⇒ Object



89
90
91
# File 'lib/radical/controller.rb', line 89

def plain(body)
  Rack::Response.new(body, 200, { 'Content-Type' => 'text/plain' })
end

#redirect(to) ⇒ Object



122
123
124
125
126
# File 'lib/radical/controller.rb', line 122

def redirect(to)
  to = self.class.action_to_url(to) if to.is_a?(Symbol)

  Rack::Response.new(nil, 302, { 'Location' => to })
end

#sessionObject



132
133
134
# File 'lib/radical/controller.rb', line 132

def session
  @request.env['rack.session']
end

#view(name, locals = {}) ⇒ Object



99
100
101
# File 'lib/radical/controller.rb', line 99

def view(name, locals = {})
  View.render(self.class.route_name, name, self, { locals: locals })
end