Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove crash when no ActiveRecord #47

Merged
merged 1 commit into from
May 6, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions app/models/exception_handler/exception.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,20 @@ module ExceptionHandler
# => Attributes
# => Determine schema etc
ATTRS = %i(class_name status message trace target referrer params user_agent)

# => Exceptions to be rescued by ExceptionHandler
EXCEPTIONS_TO_BE_RESCUED = [ActionController::RoutingError, AbstractController::ActionNotFound].tap do |list|
list << ActiveRecord::RecordNotFound if defined?(ActiveRecord)
list << Mongoid::Errors::DocumentNotFound if defined?(Mongoid)
end

############################################################
############################################################

# => Class (inheritance dependent on whether db option is available)
self::Exception = Class.new (ExceptionHandler.config.try(:db) ? ActiveRecord::Base : Object) do
self::Exception = Class.new(
(ExceptionHandler.config.try(:db) && defined?(ActiveRecord)) ? ActiveRecord::Base : Object
) do

# => Include individual elements
# => Only required if no db present (no ActiveRecord)
Expand All @@ -31,7 +39,7 @@ def initialize attributes={}

else

# => AciveModel
# => ActiveModel
include ActiveModel::Model
include ActiveModel::Validations

Expand Down Expand Up @@ -96,7 +104,7 @@ def self.table_name
attr_accessor *ATTRS unless ExceptionHandler.config.try(:db)

# => Validations
validates :klass, exclusion: { in: [ActionController::RoutingError, AbstractController::ActionNotFound, ActiveRecord::RecordNotFound], message: "%{value}" }, if: -> { referer.blank? } # => might need full Proc syntax
validates :klass, exclusion: { in: EXCEPTIONS_TO_BE_RESCUED, message: "%{value}" }, if: -> { referer.blank? } # => might need full Proc syntax
validates :user_agent, format: { without: Regexp.new( BOTS.join("|"), Regexp::IGNORECASE ) }

##################################
Expand Down