module Turboboost::Controller

Constants

CATCHABLE_ERRORS

Public Instance Methods

_turboboost_get_flash_messages(response_status_and_flash = {}) click to toggle source
# File lib/turboboost/controller.rb, line 90
def _turboboost_get_flash_messages(response_status_and_flash = {})
  turboboost_flash = {}
  flash_types = defined?(self.class._flash_types) ? self.class._flash_types : [:alert, :notice]
  flash_types.each do |flash_type|
    if type = flash.delete(type)
      turboboost_flash.update(type)
    end
    if type = response_status_and_flash.delete(flash_type)
      turboboost_flash[flash_type] = type
    end
  end
  if other_flashes = response_status_and_flash.delete(:flash)
    turboboost_flash.update(other_flashes)
  end
  turboboost_flash
end
head_turboboost_success(turboboost_flash = {}) click to toggle source
# File lib/turboboost/controller.rb, line 45
def head_turboboost_success(turboboost_flash = {})
  turboboost_flash = _turboboost_get_flash_messages(turboboost_flash)
  head :ok, 'X-Flash' => turboboost_flash.to_json, 'X-Turboboosted' => '1'
end
redirect_to(options = {}, response_status_and_flash = {}) click to toggle source
Calls superclass method
# File lib/turboboost/controller.rb, line 68
def redirect_to(options = {}, response_status_and_flash = {})
  turboboost_request? ? turboboost_redirect_to(options, response_status_and_flash) : super
end
render(*args, &block) click to toggle source
Calls superclass method
# File lib/turboboost/controller.rb, line 50
def render(*args, &block)
  turboboost_request? ? turboboost_render(*args, &block) : super
end
render_turboboost_errors_for(record) click to toggle source
# File lib/turboboost/controller.rb, line 41
def render_turboboost_errors_for(record)
  render json: record.errors.full_messages.to_a, status: :unprocessable_entity, root: false
end
turboboost_error_handler(error) click to toggle source
# File lib/turboboost/controller.rb, line 25
def turboboost_error_handler(error)
  if turboboost_request?
    error_status = CATCHABLE_ERRORS[error.class.name]
    response.headers['X-Turboboosted'] = '1'
    if defined?(error.record)
      render_turboboost_errors_for(error.record)
    else
      translation = I18n.t("turboboost.errors.#{error.class.name}")
      message = translation.match('translation missing: (.+)') ? error.class.name : translation
      render json: [message], status: error_status || 500, root: false
    end
  else
    raise error
  end
end
turboboost_redirect_to(options = {}, response_status_and_flash = {}) click to toggle source
# File lib/turboboost/controller.rb, line 72
def turboboost_redirect_to(options = {}, response_status_and_flash = {})
  raise ActionControllerError.new('Cannot redirect to nil!') unless options
  raise AbstractController::DoubleRenderError if response_body

  # set flash for turbo redirect headers
  turboboost_flash = _turboboost_get_flash_messages(response_status_and_flash)

  if Rails.version < '4.2'
    self.location = _compute_redirect_to_location(options)
  else
    self.location = _compute_redirect_to_location(request, options)
  end

  head :ok, 'X-Flash' => turboboost_flash.to_json

  flash.update(turboboost_flash) # set flash for rendered view
end
turboboost_render(*args, &block) click to toggle source
# File lib/turboboost/controller.rb, line 58
def turboboost_render(*args, &block)
  options = _normalize_render(*args, &block)
  [:replace, :within, :append, :prepend, :before, :after].each do |h|
    response.headers['X-Turboboost-Render'] = { h => options[h] }.to_json if options[h]
  end
  response.headers['X-Flash'] = _turboboost_get_flash_messages(options).to_json
  response.headers['X-Turboboosted'] = '1'
  self.response_body = render_to_body(options)
end
turboboost_request?() click to toggle source
# File lib/turboboost/controller.rb, line 54
def turboboost_request?
  request.xhr? && request.headers['HTTP_X_TURBOBOOST']
end