Class: RailsErrorDashboard::Helpers::UserModelDetector
- Inherits:
-
Object
- Object
- RailsErrorDashboard::Helpers::UserModelDetector
- Defined in:
- lib/rails_error_dashboard/helpers/user_model_detector.rb
Overview
Automatically detects the User model and total users count Handles both single database and separate database setups
Class Method Summary collapse
-
.calculate_user_impact(unique_users_count) ⇒ Float?
Get user impact percentage for an error Calculates percentage of users affected based on unique_users_count.
-
.detect_total_users ⇒ Integer?
Auto-detect total users count Returns the configured value if set, otherwise queries the user model.
-
.detect_user_model ⇒ String?
Auto-detect the user model name Returns the configured model if set, otherwise tries to detect User model.
-
.model_exists?(model_name) ⇒ Boolean
Check if a specific model exists and is loaded.
-
.model_file_exists?(model_name) ⇒ Boolean
Check if model file exists in app/models.
-
.query_user_count(model_name) ⇒ Integer?
Query the user count from the model Handles connection to main database even if error dashboard uses separate DB.
-
.user_model_exists? ⇒ Boolean
Check if User model exists and is loaded.
Class Method Details
.calculate_user_impact(unique_users_count) ⇒ Float?
Get user impact percentage for an error Calculates percentage of users affected based on unique_users_count
119 120 121 122 123 124 125 126 |
# File 'lib/rails_error_dashboard/helpers/user_model_detector.rb', line 119 def calculate_user_impact(unique_users_count) return nil unless unique_users_count.present? && unique_users_count.positive? total_users = detect_total_users return nil unless total_users.present? && total_users.positive? ((unique_users_count.to_f / total_users) * 100).round(2) end |
.detect_total_users ⇒ Integer?
Auto-detect total users count Returns the configured value if set, otherwise queries the user model
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/rails_error_dashboard/helpers/user_model_detector.rb', line 33 def detect_total_users # Return configured value if explicitly set configured_count = RailsErrorDashboard.configuration.total_users_for_impact return configured_count if configured_count.present? # Try to query user model count user_model_name = detect_user_model return nil unless user_model_name query_user_count(user_model_name) rescue StandardError => e # Silently return nil if query fails (DB not accessible, model doesn't have count, etc.) log_error("Failed to query user count: #{e.}") nil end |
.detect_user_model ⇒ String?
Auto-detect the user model name Returns the configured model if set, otherwise tries to detect User model
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/rails_error_dashboard/helpers/user_model_detector.rb', line 13 def detect_user_model # Return configured model if explicitly set configured_model = RailsErrorDashboard.configuration.user_model return configured_model if configured_model.present? && configured_model != "User" # Try to detect User model return "User" if user_model_exists? # Check for common alternatives %w[Account Member Person].each do |model_name| return model_name if model_exists?(model_name) end nil end |
.model_exists?(model_name) ⇒ Boolean
Check if a specific model exists and is loaded
60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/rails_error_dashboard/helpers/user_model_detector.rb', line 60 def model_exists?(model_name) # Check if model file exists return false unless model_file_exists?(model_name) # Try to constantize the model begin model_class = model_name.constantize model_class.is_a?(Class) && model_class < ActiveRecord::Base rescue NameError, LoadError false end end |
.model_file_exists?(model_name) ⇒ Boolean
Check if model file exists in app/models
77 78 79 80 81 82 83 84 85 |
# File 'lib/rails_error_dashboard/helpers/user_model_detector.rb', line 77 def model_file_exists?(model_name) return false unless defined?(Rails) # Convert to snake_case filename (e.g., "User" -> "user.rb") filename = model_name.underscore + ".rb" model_path = Rails.root.join("app", "models", filename) File.exist?(model_path) end |
.query_user_count(model_name) ⇒ Integer?
Query the user count from the model Handles connection to main database even if error dashboard uses separate DB
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/rails_error_dashboard/helpers/user_model_detector.rb', line 92 def query_user_count(model_name) model_class = model_name.constantize # Ensure we're querying the main database, not the error dashboard database # User models always connect to the primary/main database if model_class.respond_to?(:count) # Use a timeout to avoid hanging the dashboard Timeout.timeout(5) do model_class.count end end rescue NameError, LoadError => e log_error("Model not found: #{model_name} - #{e.}") nil rescue Timeout::Error => e log_error("Timeout querying #{model_name}.count - #{e.}") nil rescue StandardError => e log_error("Error querying #{model_name}.count: #{e.}") nil end |
.user_model_exists? ⇒ Boolean
Check if User model exists and is loaded
52 53 54 |
# File 'lib/rails_error_dashboard/helpers/user_model_detector.rb', line 52 def user_model_exists? model_exists?("User") end |