Sha256: 593908843d5f079f2d3d363ab3a06b7087714630e709299895265c8e90814917

Contents?: true

Size: 1.09 KB

Versions: 1

Compression:

Stored size: 1.09 KB

Contents

module HTTPStatus
  
  class Base < Exception
    attr_accessor :status
    
    cattr_reader :template_path
    @@template_path = 'shared/http_status'
    
    def initialize(message)
      @status = self.class.to_s.split("::").last.underscore.to_sym
      super(message)
    end
    
    def status_code
      ActionController::StatusCodes::SYMBOL_TO_STATUS_CODE[@status]
    end
    
    def template
      "#{@@template_path}/#{@status_code}"
    end
  end
  
  def self.included(base)
    # create all the classed
    ActionController::StatusCodes::STATUS_CODES.each do |code, name|
      const_set("#{name.to_s.gsub(/[^A-z]/, '').camelize}", Class.new(HTTPStatus::Base))
    end
    
    base.send(:rescue_from, HTTPStatus::Base, :with => :http_status_exception)
  end

  # The default handler for raised HTTP status exceptions
  def http_status_exception(exception)
    @exception = exception
    begin
      render(:template => exception.template, :status => exception.status)
    rescue ActionView::MissingTemplate
      head(exception.status)
    end
  end
end

ActionController::Base.send(:include, HTTPStatus)

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
wvanbergen-http_status_exceptions-0.1.0 lib/http_status_exceptions.rb