Module: Scrivito::ControllerActions

Extended by:
ActiveSupport::Concern
Defined in:
app/cms/scrivito/controller_actions.rb

Overview

This module provides CMS controller actions. Include it in controller you want to make able to deliver +Obj+s.

Examples:

Adding Scrivito controller actions to PageCmsController:

class PageCmsController < ApplicationController
  include Scrivito::ControllerActions
end

See Also:

Instance Method Summary collapse

Instance Method Details

#deliver_fileObject (private)

Deliver a binary Obj by redirecting to its Obj#binary's url. Will respond with 404 if the Obj has no blob.



164
165
166
167
168
169
170
171
# File 'app/cms/scrivito/controller_actions.rb', line 164

def deliver_file
  if binary = @obj.binary
    binary_routing = BinaryRouting.new(request, scrivito_engine)
    redirect_to binary_routing.resolved_binary_url(binary), allow_other_host: true
  else
    render plain: 'Empty Blob', status: 404
  end
end

#indexObject

Default action. Delivers files directly if Obj is binary. Otherwise the view is rendered.



37
38
39
# File 'app/cms/scrivito/controller_actions.rb', line 37

def index
  deliver_file if @obj.binary?
end

#on_scrivito_widget_error(widget, error) ⇒ Object

How to handle widget errors in production.

When an exception is raised from within a widget, the entire page is not available. Often, this is the case in production if the developer has forgotten to handle specific content scenarios such as empty date attributes, missing titles, unmanaged enum values, etc. but also for simple coding mistakes during development.

By default, Scrivito handles exceptions raised while rendering a widget by adding a placeholder text to the HTML indicating that the widget couldn't be rendered. In addition, the exception is logged alongside with its backtrace.

Override this method to adapt this behaviour.

The overridden method allows to:

  • catch a widget error and replace it with an HTML placeholder.
  • report a widget error to an external service like Honeybadger or Airbrake.

This method is not called if Rails is in the development or test environment. In those environments, all widget errors are just raised.

Examples:

Notify external service about widget error and render an HTML placeholder:

def on_scrivito_widget_error(widget, error)
  # Report error to external service (e.g. Honeybadger or Airbrake):
  Honeybadger.notify(error)
  notify_airbrake(error)

  # Replace corrupted widget output with a placeholder:
  message = "Rendering #{widget.description_for_editor} failed with #{error.message}"
  return "<!--#{message}-->".html_safe if Rails.env.production?
  "<p>#{message}</p>".html_safe
end

Parameters:

Raises:

  • (StandardError)

    if this method is not overridden, the error passed to it is reraised.



# File 'app/cms/scrivito/controller_actions.rb', line 63