Sha256: b9eb7df46950c2bebb47b39cc935f50bf6e44a94299b7e5bd8160c3205c898b6

Contents?: true

Size: 1.41 KB

Versions: 2

Compression:

Stored size: 1.41 KB

Contents

module Errdo
  class Error < ActiveRecord::Base

    paginates_per 20

    self.table_name = Errdo.error_name

    enum status: [:active, :wontfix, :resolved]

    serialize :backtrace

    has_many :error_occurrences
    belongs_to :last_experiencer, polymorphic: true

    before_validation :create_unique_string

    validates :backtrace_hash, uniqueness: true

    def self.find_or_create(params)
      params = clean_backtrace(params)
      unique_string = create_unique_string_from_params(params)

      @error = Errdo::Error.find_by(backtrace_hash: unique_string)
      @error = Errdo::Error.create(params) if @error.nil?

      return @error
    end

    # I need a more elegant way to do this
    def self.create_unique_string_from_params(params)
      params[:backtrace][0].to_s.last(50) +
        params[:exception_message].to_s.last(20) +
        params[:exception_class_name].to_s.last(20)
    end

    def short_backtrace
      backtrace.first if backtrace.respond_to?(:first)
    end

    def self.clean_backtrace(params)
      unless params[:backtrace].empty?
        params[:backtrace][0] = params[:backtrace][0].gsub(/[_]{1,}[0-9]+/, "")
      end
      return params
    end

    private

    def create_unique_string
      self.backtrace_hash = backtrace.to_a[0].to_s.last(50) +
                            exception_message.to_s.last(20) +
                            exception_class_name.to_s.last(20)
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
errdo-0.12.5 app/models/errdo/error.rb
errdo-0.12.2 app/models/errdo/error.rb