lib/scrivito/controller_actions.rb in scrivito_sdk-0.66.0 vs lib/scrivito/controller_actions.rb in scrivito_sdk-0.70.0.rc1

- old
+ new

@@ -18,14 +18,15 @@ included do before_filter :require_authenticated_editor, only: [ :show_widget, :widget_details, :page_details, - :resource_details, ] before_filter :load_object + + helper_method :scrivito_in_editable_view? end # # Default action. # Delivers files directly if +Obj+ is binary. @@ -58,17 +59,76 @@ render @obj.details_view_path, layout: 'scrivito_dialog' rescue ActionView::MissingTemplate render 'scrivito/page_details', layout: 'scrivito_dialog' end - def resource_details - assert_dialog_layout - @scrivito_resource = editing_context.selected_workspace.objs - .find_including_deleted(params[:resource_id]) - render text: '', layout: 'scrivito_dialog' + # 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. + # + # Override this method to prevent the entire page from being unavailable due to widget errors. + # + # 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. + # + # By default, this method just reraises the given error. + # + # @param widget [Scrivito::BasicWidget] the flawed widget + # @param error [StandardError] the error that occurred + # @raise [StandardError] if this method is not overridden, the +error+ passed to it is reraised. + # + # @example 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 + # @api public + def on_scrivito_widget_error(widget, error) + raise error end + # + # Returns whether the GUI is in the +editable+ view. + # + # +scrivito_in_editable_view?+ is also a helper method. + # + # @api public + # @return +true+ if the current visitor is an authenticated editor, the selected workspace is + # editable and the display mode is +editing+. + # @return +false+ otherwise. + # + def scrivito_in_editable_view? + editing_context.editable_display_mode? + end + + # + # Returns the current user. + # + # +scrivito_user+ is also a helper method. + # + # @api public + # @return [Scrivito::User] if the {Scrivito::Configuration.editing_auth} callback returns an + # instance of {Scrivito::User}. + # @return +nil+ otherwise. + # + def scrivito_user + editing_context.editor + end + module ClassMethods # # This method indicates if this controller should be used automatically when an +Obj+ is # requested via the SDK's standard routes. It returns +true+ by default. # @@ -124,10 +184,11 @@ # Will respond with 404 if the +Obj+ has no +blob+. # # @api public # def deliver_file - if url = @obj.binary_url + if binary = @obj.binary + url = BinaryRouting.new(request, scrivito_engine).resolved_binary_obj_url(@obj, binary) redirect_to CmsRouting.match_protocol(url, request) else render text: 'Empty Blob', status: 404 end end