Class: RailsErrorDashboard::Application

Inherits:
ErrorLogsRecord
  • Object
show all
Defined in:
app/models/rails_error_dashboard/application.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.find_or_create_by_name(name) ⇒ Object

Class method for finding or creating with caching Caches application IDs (not objects) to avoid stale ActiveRecord references This is safer with transactional fixtures and database rollbacks



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/models/rails_error_dashboard/application.rb', line 17

def self.find_or_create_by_name(name)
  # Try to find ID in cache
  cached_id = Rails.cache.read("error_dashboard/application_id/#{name}")
  if cached_id
    # Verify the cached ID still exists in database
    # (could have been deleted in tests with transactional rollback)
    cached_record = find_by(id: cached_id)
    return cached_record if cached_record
    # Cache was stale, clear it
    Rails.cache.delete("error_dashboard/application_id/#{name}")
  end

  # Try to find existing in database
  found = find_by(name: name)
  if found
    # Cache the ID (not the object) for future lookups
    Rails.cache.write("error_dashboard/application_id/#{name}", found.id, expires_in: 1.hour)
    return found
  end

  # Create if not found
  created = create!(name: name)
  # Cache the ID (not the object)
  Rails.cache.write("error_dashboard/application_id/#{name}", created.id, expires_in: 1.hour)
  created
end

Instance Method Details

#error_countObject

Instance methods



45
46
47
# File 'app/models/rails_error_dashboard/application.rb', line 45

def error_count
  error_logs.count
end

#unresolved_error_countObject



49
50
51
# File 'app/models/rails_error_dashboard/application.rb', line 49

def unresolved_error_count
  error_logs.unresolved.count
end