Sha256: 25cd2fa4174add4edc0a19787dcf79d949ae8b9eccb859e03dc52426f61b4ec9

Contents?: true

Size: 1.36 KB

Versions: 3

Compression:

Stored size: 1.36 KB

Contents

module RailsContrib
  module ActionController
    module Base

      def self.included(base)
        base.class_eval do
          if Rails.env == 'development'
            rescue_from Exception, :with => :error
          end
        end
      end

      protected
      
      def error(exception)
        logger.error exception.message
        exception.backtrace.each { |line| logger.error line }
        render :file => Rails.root.join('public', '500.html'), :status => 500, :layout => false
      end

      def not_found
        render :file => Rails.root.join('public', '404.html'), :status => 404, :layout => false
      end  
      
      def forbidden
        render :file => Rails.root.join('public', '422.html'), :status => 422, :layout => false
      end

      def redirect_with_flash(path, type, flash, params=nil)
        path = (params.nil? ? path : (params[:back].present? ? params[:back] : path))
        redirect_to path, { :flash => { type => (flash.is_a?(Array) ? flash : [flash]) } }
      end

      def flash_errors(source)
        flash[:error] = [] unless flash[:error].is_a? Array
        case source
        when String
          flash[:error] << source
        when Array
          source.each { |error| flash[:errors] << error }
        else
          source.errors.full_messages.each { |error| flash[:error] << error }
        end
      end

    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rails_contrib-0.0.4 lib/rails_contrib/action_controller/base.rb
rails_contrib-0.0.3 lib/rails_contrib/action_controller/base.rb
rails_contrib-0.0.2 lib/rails_contrib/action_controller/base.rb