class Radical::Controller

Attributes

skip_csrf_actions[RW]
options[R]
request[RW]

Public Class Methods

action_to_http_method(action) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/radical/controller.rb, line 28
def layout(name)
  View.layout name
end
new(request, options: {}) click to toggle source
# File lib/radical/controller.rb, line 78
def initialize(request, options: {})
  @request = request
  @options = options
end
prepend_view_path(path) click to toggle source
# File lib/radical/controller.rb, line 24
def prepend_view_path(path)
  View.path path
end
route_name() click to toggle source
# File lib/radical/controller.rb, line 33
def route_name
  Strings.snake_case to_s.split('::').last.gsub(/Controller$/, '')
end
skip_csrf(*actions) click to toggle source
# 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

Public Instance Methods

assets_path(type) click to toggle source
# 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) click to toggle source
# 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
flash() click to toggle source
# File lib/radical/controller.rb, line 128
def flash
  @request.env['rack.session']['__FLASH__']
end
form(options) { |f| ... } click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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
params() click to toggle source
# File lib/radical/controller.rb, line 94
def params
  @request.params
end
partial(name, locals = {}) click to toggle source
# 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) click to toggle source
# File lib/radical/controller.rb, line 89
def plain(body)
  Rack::Response.new(body, 200, { 'Content-Type' => 'text/plain' })
end
redirect(to) click to toggle source
# 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
session() click to toggle source
# File lib/radical/controller.rb, line 132
def session
  @request.env['rack.session']
end
view(name, locals = {}) click to toggle source
# File lib/radical/controller.rb, line 99
def view(name, locals = {})
  View.render(self.class.route_name, name, self, { locals: locals })
end

Private Instance Methods

capture(block) { || ... } click to toggle source
# File lib/radical/controller.rb, line 173
def capture(block)
  @output = eval '_buf', block.binding
  yield
  @output
end
emit(tag) click to toggle source
# File lib/radical/controller.rb, line 168
def emit(tag)
  @output = String.new if @output.nil?
  @output << tag.to_s
end
script_tag(src) click to toggle source
# File lib/radical/controller.rb, line 179
def script_tag(src)
  "<script type=\"application/javascript\" src=\"#{src}\"></script>"
end