Class: ErrorStalker::Store::Mongoid::ExceptionGroup::PaginationHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/error_stalker/store/mongoid.rb

Overview

When we display the list of grouped recent exceptions, we paginate them. We also need to display information about the most recent exception report. This helper class wraps paginate, doing a hacked-in :include to get the most recent reports for the requested exception groups without running into the N+1 problem.

Instance Method Summary collapse

Constructor Details

#initialize(criteria) ⇒ PaginationHelper

Wraps criteria in a new PaginationHelper, which will include the most recent exception reports when paginate is called.



234
235
236
# File 'lib/error_stalker/store/mongoid.rb', line 234

def initialize(criteria)
  @criteria = criteria
end

Instance Method Details

#paginate(pagination_opts = {}) ⇒ Object

Override the built-in pagination to support preloading the associated exception reports.



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/error_stalker/store/mongoid.rb', line 240

def paginate(pagination_opts = {})
  recent = @criteria.paginate(pagination_opts)
  exceptions = ErrorStalker::Store::Mongoid::ExceptionReport.where(:_id.in => recent.map(&:most_recent_report_id))
  # Fake association preloading
  id_map = {}.tap do |h|
    exceptions.each do |ex|
      h[ex.id] = ex
    end
  end
  
  recent.each do |r|
    r.most_recent_report = id_map[r.most_recent_report_id]
  end
  
  recent
end