rails_console_pro 0.1.2 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. checksums.yaml +4 -4
  2. data/.rspec_status +288 -240
  3. data/CHANGELOG.md +7 -0
  4. data/QUICK_START.md +17 -0
  5. data/README.md +43 -0
  6. data/docs/FORMATTING.md +5 -0
  7. data/docs/MODEL_INTROSPECTION.md +371 -0
  8. data/docs/MODEL_STATISTICS.md +4 -0
  9. data/docs/OBJECT_DIFFING.md +6 -0
  10. data/docs/PROFILING.md +91 -0
  11. data/docs/QUERY_BUILDER.md +385 -0
  12. data/docs/QUEUE_INSIGHTS.md +82 -0
  13. data/docs/SCHEMA_INSPECTION.md +5 -0
  14. data/docs/SNIPPETS.md +71 -0
  15. data/lib/rails_console_pro/commands/compare_command.rb +151 -0
  16. data/lib/rails_console_pro/commands/introspect_command.rb +220 -0
  17. data/lib/rails_console_pro/commands/jobs_command.rb +212 -0
  18. data/lib/rails_console_pro/commands/profile_command.rb +84 -0
  19. data/lib/rails_console_pro/commands/query_builder_command.rb +43 -0
  20. data/lib/rails_console_pro/commands/snippets_command.rb +141 -0
  21. data/lib/rails_console_pro/commands.rb +30 -0
  22. data/lib/rails_console_pro/compare_result.rb +81 -0
  23. data/lib/rails_console_pro/configuration.rb +51 -0
  24. data/lib/rails_console_pro/format_exporter.rb +32 -0
  25. data/lib/rails_console_pro/global_methods.rb +24 -0
  26. data/lib/rails_console_pro/initializer.rb +41 -1
  27. data/lib/rails_console_pro/introspect_result.rb +101 -0
  28. data/lib/rails_console_pro/model_validator.rb +1 -1
  29. data/lib/rails_console_pro/printers/compare_printer.rb +138 -0
  30. data/lib/rails_console_pro/printers/introspect_printer.rb +282 -0
  31. data/lib/rails_console_pro/printers/profile_printer.rb +180 -0
  32. data/lib/rails_console_pro/printers/query_builder_printer.rb +81 -0
  33. data/lib/rails_console_pro/printers/queue_insights_printer.rb +150 -0
  34. data/lib/rails_console_pro/printers/snippet_collection_printer.rb +68 -0
  35. data/lib/rails_console_pro/printers/snippet_printer.rb +64 -0
  36. data/lib/rails_console_pro/profile_result.rb +109 -0
  37. data/lib/rails_console_pro/pry_commands.rb +106 -0
  38. data/lib/rails_console_pro/query_builder.rb +197 -0
  39. data/lib/rails_console_pro/query_builder_result.rb +66 -0
  40. data/lib/rails_console_pro/queue_insights_result.rb +110 -0
  41. data/lib/rails_console_pro/serializers/compare_serializer.rb +66 -0
  42. data/lib/rails_console_pro/serializers/introspect_serializer.rb +99 -0
  43. data/lib/rails_console_pro/serializers/profile_serializer.rb +73 -0
  44. data/lib/rails_console_pro/serializers/query_builder_serializer.rb +35 -0
  45. data/lib/rails_console_pro/services/introspection_collector.rb +420 -0
  46. data/lib/rails_console_pro/services/profile_collector.rb +245 -0
  47. data/lib/rails_console_pro/services/queue_action_service.rb +176 -0
  48. data/lib/rails_console_pro/services/queue_insight_fetcher.rb +600 -0
  49. data/lib/rails_console_pro/services/snippet_repository.rb +191 -0
  50. data/lib/rails_console_pro/snippets/collection_result.rb +45 -0
  51. data/lib/rails_console_pro/snippets/single_result.rb +30 -0
  52. data/lib/rails_console_pro/snippets/snippet.rb +112 -0
  53. data/lib/rails_console_pro/snippets.rb +13 -0
  54. data/lib/rails_console_pro/version.rb +1 -1
  55. data/rails_console_pro.gemspec +1 -1
  56. metadata +42 -8
data/CHANGELOG.md CHANGED
@@ -7,6 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ### Added
11
+ - Model introspection suite with callbacks, enums, concerns, scopes, validations, and method source lookup
12
+ - Introspect command with filtered views and programmatic access
13
+ - Comprehensive introspection documentation
14
+ - Adaptive profiling command with SQL, cache, and duplicate query analysis
15
+ - Profile result printer, serializer, and documentation
16
+
10
17
  ## [0.1.0] - 2025-01-XX
11
18
 
12
19
  ### Added
data/QUICK_START.md CHANGED
@@ -59,6 +59,21 @@ export schema(User) user_schema.json
59
59
  ```
60
60
  [Learn more →](docs/EXPORT.md)
61
61
 
62
+ ### Model Introspection
63
+ ```ruby
64
+ introspect User
65
+ introspect User, :callbacks
66
+ introspect User, :enums
67
+ ```
68
+ [Learn more →](docs/MODEL_INTROSPECTION.md)
69
+
70
+ ### Snippet Library
71
+ ```ruby
72
+ snippets(:add, "User.where(active: true).count", description: "Active users", tags: %w[users metrics])
73
+ snippets(:list)
74
+ ```
75
+ [Learn more →](docs/SNIPPETS.md)
76
+
62
77
  ### Beautiful Formatting
63
78
  ```ruby
64
79
  User.first # Automatically formatted with colors
@@ -101,8 +116,10 @@ rails generate rails_console_pro:install
101
116
  - [SQL Explain](docs/SQL_EXPLAIN.md)
102
117
  - [Model Statistics](docs/MODEL_STATISTICS.md)
103
118
  - [Association Navigation](docs/ASSOCIATION_NAVIGATION.md)
119
+ - [Model Introspection](docs/MODEL_INTROSPECTION.md)
104
120
  - [Object Diffing](docs/OBJECT_DIFFING.md)
105
121
  - [Export](docs/EXPORT.md)
122
+ - [Snippets](docs/SNIPPETS.md)
106
123
  - [Formatting](docs/FORMATTING.md)
107
124
 
108
125
  ## Need Help?
data/README.md CHANGED
@@ -14,9 +14,15 @@ Rails Console Pro transforms your Rails console into a powerful debugging enviro
14
14
  - 🔍 **SQL Explain** - Analyze query execution plans with performance recommendations
15
15
  - 🧭 **Association Navigator** - Interactive navigation through model associations
16
16
  - 📈 **Model Statistics** - Record counts, growth rates, table sizes, and index usage
17
+ - 🔬 **Adaptive Profiling** - Profile blocks or relations with query, cache, and performance metrics
18
+ - 🧵 **ActiveJob Insights** - Inspect and manage queues across adapters (Sidekiq, SolidQueue, Test, Async) with filters and inline actions
17
19
  - 🔄 **Object Diffing** - Compare ActiveRecord objects and highlight differences
20
+ - 🔍 **Model Introspection** - Deep dive into callbacks, enums, concerns, scopes, validations, and method sources
21
+ - ⚖️ **Query Comparison** - Compare multiple query strategies side-by-side to find optimal approaches
22
+ - 🔧 **Query Builder** - Interactive DSL for building and analyzing ActiveRecord queries
18
23
  - 💾 **Export Capabilities** - Export to JSON, YAML, and HTML formats
19
24
  - 📄 **Smart Pagination** - Automatic pagination for large collections
25
+ - 📝 **Snippet Library** - Capture, search, and reuse console snippets across sessions
20
26
 
21
27
  ## 🚀 Installation
22
28
 
@@ -62,6 +68,35 @@ diff user1, user2
62
68
 
63
69
  # Export
64
70
  export schema(User) user_schema.json
71
+
72
+ # Profiling
73
+ profile('Load users') { User.active.includes(:posts).limit(10).to_a }
74
+
75
+ # Queue insights
76
+ jobs(limit: 10, queue: 'mailers')
77
+ jobs status=retry class=ReminderJob
78
+ jobs retry=abcdef123456
79
+ jobs details=abcdef123456
80
+
81
+ # Model introspection
82
+ introspect User
83
+ introspect User, :callbacks
84
+ introspect User, :enums
85
+ introspect User, :method_name # Find where method is defined
86
+
87
+ # Query comparison
88
+ compare do |c|
89
+ c.run("Eager loading") { User.includes(:posts).to_a }
90
+ c.run("N+1") { User.all.map(&:posts) }
91
+ end
92
+
93
+ # Query builder
94
+ query User do
95
+ where(active: true)
96
+ includes(:posts)
97
+ order(:created_at)
98
+ limit(10)
99
+ end.analyze # Shows SQL + explain
65
100
  ```
66
101
 
67
102
  See [QUICK_START.md](QUICK_START.md) for more examples and detailed documentation for each feature.
@@ -78,6 +113,9 @@ RailsConsolePro.configure do |config|
78
113
  config.schema_command_enabled = true
79
114
  config.explain_command_enabled = true
80
115
  config.stats_command_enabled = true
116
+ config.queue_command_enabled = true
117
+ config.compare_command_enabled = true
118
+ config.query_builder_command_enabled = true
81
119
 
82
120
  # Color scheme
83
121
  config.color_scheme = :dark # or :light
@@ -105,9 +143,14 @@ end
105
143
  - [SQL Explain](docs/SQL_EXPLAIN.md) - Analyze query performance
106
144
  - [Model Statistics](docs/MODEL_STATISTICS.md) - Get model statistics
107
145
  - [Association Navigation](docs/ASSOCIATION_NAVIGATION.md) - Navigate model associations
146
+ - [Model Introspection](docs/MODEL_INTROSPECTION.md) - Deep dive into callbacks, enums, concerns, and more
108
147
  - [Object Diffing](docs/OBJECT_DIFFING.md) - Compare objects
109
148
  - [Export](docs/EXPORT.md) - Export to JSON, YAML, HTML
149
+ - [Snippets](docs/SNIPPETS.md) - Build a reusable console snippet library
110
150
  - [Formatting](docs/FORMATTING.md) - Beautiful console output
151
+ - [Adaptive Profiling](docs/PROFILING.md) - Measure queries, cache hits, and potential N+1 issues
152
+ - [Queue Insights](docs/QUEUE_INSIGHTS.md) - Inspect jobs across ActiveJob adapters
153
+ - [Query Builder & Comparator](docs/QUERY_BUILDER.md) - Compare query strategies and build optimized queries
111
154
 
112
155
  ## 🤝 Contributing
113
156
 
data/docs/FORMATTING.md CHANGED
@@ -84,3 +84,8 @@ end
84
84
  - **Clean Layout**: Well-organized, readable output
85
85
  - **Dark/Light Themes**: Choose your preferred color scheme
86
86
 
87
+ ## Screenshots
88
+
89
+ <img width="1117" height="551" alt="Screenshot 2025-11-07 at 11 42 52 AM" src="https://github.com/user-attachments/assets/2e77e0d5-8fa1-4249-8b0f-f8d16e829d99" />
90
+
91
+
@@ -0,0 +1,371 @@
1
+ # Model Introspection
2
+
3
+ Deep dive into your ActiveRecord models with comprehensive introspection of callbacks, enums, concerns, scopes, validations, and lifecycle hooks.
4
+
5
+ ## Overview
6
+
7
+ The `introspect` command provides a complete view of your model's internal structure, making it easy to understand complex models without digging through code files. Perfect for onboarding, debugging, and understanding model behavior.
8
+
9
+ ## Usage
10
+
11
+ ```ruby
12
+ # Full introspection of a model
13
+ introspect User
14
+
15
+ # Get specific information
16
+ introspect User, :callbacks # Show only callbacks
17
+ introspect User, :enums # Show only enums
18
+ introspect User, :concerns # Show only concerns/modules
19
+ introspect User, :scopes # Show only scopes
20
+ introspect User, :validations # Show only validations
21
+
22
+ # Find where a method is defined
23
+ introspect User, :full_name
24
+ ```
25
+
26
+ ## Features
27
+
28
+ ### 1. Callbacks
29
+
30
+ View all callbacks with their execution order, conditions, and types:
31
+
32
+ ```ruby
33
+ introspect User, :callbacks
34
+ ```
35
+
36
+ **Output includes:**
37
+ - `before_validation`, `after_validation`
38
+ - `before_save`, `around_save`, `after_save`
39
+ - `before_create`, `around_create`, `after_create`
40
+ - `before_update`, `around_update`, `after_update`
41
+ - `before_destroy`, `around_destroy`, `after_destroy`
42
+ - `after_commit`, `after_rollback`
43
+ - `after_find`, `after_initialize`, `after_touch`
44
+ - Conditional callbacks (`:if`, `:unless`)
45
+
46
+ ### 2. Enums
47
+
48
+ See all enum definitions with their mappings and types:
49
+
50
+ ```ruby
51
+ introspect User, :enums
52
+ ```
53
+
54
+ **Shows:**
55
+ - Enum name and values
56
+ - Type (integer or string)
57
+ - Value mappings
58
+ - All possible states
59
+
60
+ ### 3. Concerns & Modules
61
+
62
+ Discover all included modules and concerns:
63
+
64
+ ```ruby
65
+ introspect User, :concerns
66
+ ```
67
+
68
+ **Displays:**
69
+ - All concerns included in the model
70
+ - Parent classes in inheritance chain
71
+ - Modules mixed in
72
+ - Source file locations
73
+ - Whether it's a concern, class, or module
74
+
75
+ ### 4. Scopes
76
+
77
+ View all scopes with their SQL:
78
+
79
+ ```ruby
80
+ introspect User, :scopes
81
+ ```
82
+
83
+ **Provides:**
84
+ - Scope name
85
+ - Generated SQL
86
+ - Scope conditions and values
87
+ - WHERE, ORDER, LIMIT, INCLUDES clauses
88
+
89
+ ### 5. Validations
90
+
91
+ Comprehensive validation information:
92
+
93
+ ```ruby
94
+ introspect User, :validations
95
+ ```
96
+
97
+ **Includes:**
98
+ - Validation type (presence, uniqueness, length, etc.)
99
+ - Attributes being validated
100
+ - Options (allow_nil, allow_blank, etc.)
101
+ - Conditions (if, unless)
102
+ - Validator-specific options
103
+
104
+ ### 6. Method Source Location
105
+
106
+ Find where any method is defined:
107
+
108
+ ```ruby
109
+ introspect User, :full_name
110
+ ```
111
+
112
+ **Returns:**
113
+ - File path
114
+ - Line number
115
+ - Owner class/module
116
+ - Type (model, concern, gem, parent)
117
+
118
+ ## Full Introspection Example
119
+
120
+ ```ruby
121
+ introspect User
122
+ ```
123
+
124
+ **Output:**
125
+
126
+ ```
127
+ ═══════════════════════════════════════════════════════════
128
+ 🔍 MODEL INTROSPECTION: User
129
+ ═══════════════════════════════════════════════════════════
130
+
131
+ 📊 Lifecycle Summary:
132
+ Callbacks: 12
133
+ Validations: 8
134
+ ✓ Has state machine
135
+
136
+ 🔔 Callbacks:
137
+
138
+ before_validation (2):
139
+ 1. normalize_email (if: :email_changed?)
140
+ 2. strip_whitespace
141
+
142
+ after_create (3):
143
+ 1. send_welcome_email
144
+ 2. create_default_profile
145
+ 3. track_signup
146
+
147
+ after_commit (1):
148
+ 1. flush_cache
149
+
150
+ ✅ Validations:
151
+
152
+ email:
153
+ - PresenceValidator
154
+ - UniquenessValidator (case_sensitive: false)
155
+ - FormatValidator (with: /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i)
156
+
157
+ password:
158
+ - PresenceValidator (on: :create)
159
+ - LengthValidator (minimum: 8)
160
+
161
+ 🔢 Enums:
162
+
163
+ status [Integer]:
164
+ active, inactive, suspended, deleted
165
+ Mapping: active => 0, inactive => 1, suspended => 2, deleted => 3
166
+
167
+ role [String]:
168
+ user, admin, moderator
169
+
170
+ 🔭 Scopes:
171
+
172
+ active:
173
+ └─ SQL: SELECT "users".* FROM "users" WHERE "users"."status" = 0
174
+ └─ where: status = 0
175
+
176
+ recent:
177
+ └─ SQL: SELECT "users".* FROM "users" ORDER BY "users"."created_at" DESC LIMIT 10
178
+ └─ order: created_at DESC
179
+ └─ limit: 10
180
+
181
+ 📦 Concerns & Modules:
182
+
183
+ Concerns:
184
+ ● Authenticatable app/models/concerns/authenticatable.rb:3
185
+ ● Trackable app/models/concerns/trackable.rb:1
186
+
187
+ Classes:
188
+ ▪ ApplicationRecord app/models/application_record.rb:1
189
+
190
+ ═══════════════════════════════════════════════════════════
191
+ Generated: 2025-11-13 10:30:45
192
+
193
+ 💡 Tip: Use introspect User, :callbacks to see only callbacks
194
+ Use introspect User, :method_name to find where a method is defined
195
+ ```
196
+
197
+ ## Export Results
198
+
199
+ Export introspection data to various formats:
200
+
201
+ ```ruby
202
+ result = introspect User
203
+
204
+ # Export to JSON
205
+ result.to_json
206
+ result.to_json(pretty: true)
207
+
208
+ # Export to YAML
209
+ result.to_yaml
210
+
211
+ # Export to HTML
212
+ result.to_html
213
+
214
+ # Export to file
215
+ result.export_to_file('user_introspection.json')
216
+ result.export_to_file('user_introspection.html', format: :html)
217
+
218
+ # Or use the export command
219
+ export introspect(User), 'user_introspection.json'
220
+ ```
221
+
222
+ ## Programmatic Access
223
+
224
+ Access introspection data programmatically:
225
+
226
+ ```ruby
227
+ result = introspect User
228
+
229
+ # Check what's available
230
+ result.has_callbacks? # => true
231
+ result.has_enums? # => true
232
+ result.has_concerns? # => true
233
+ result.has_scopes? # => true
234
+ result.has_validations? # => true
235
+
236
+ # Get specific data
237
+ result.callbacks_by_type(:before_save)
238
+ result.validations_for(:email)
239
+ result.enum_values(:status)
240
+ result.scope_sql(:active)
241
+
242
+ # Find method source
243
+ result.method_source(:full_name)
244
+ # => { file: "app/models/user.rb", line: 42, owner: "User", type: :model }
245
+ ```
246
+
247
+ ## Use Cases
248
+
249
+ ### 1. **Onboarding New Developers**
250
+ ```ruby
251
+ # Quickly understand a complex model
252
+ introspect Order
253
+ ```
254
+
255
+ ### 2. **Debugging Callback Issues**
256
+ ```ruby
257
+ # See callback execution order
258
+ introspect User, :callbacks
259
+ ```
260
+
261
+ ### 3. **Understanding State Machines**
262
+ ```ruby
263
+ # View all enum states
264
+ introspect Order, :enums
265
+ ```
266
+
267
+ ### 4. **Audit Model Structure**
268
+ ```ruby
269
+ # Check what validations are in place
270
+ introspect User, :validations
271
+ ```
272
+
273
+ ### 5. **Finding Method Definitions**
274
+ ```ruby
275
+ # Where is this method coming from?
276
+ introspect User, :authenticate
277
+ # Shows if it's in the model, a concern, or a gem
278
+ ```
279
+
280
+ ### 6. **Scope Analysis**
281
+ ```ruby
282
+ # See what SQL a scope generates
283
+ introspect Product, :scopes
284
+ ```
285
+
286
+ ## Edge Cases Handled
287
+
288
+ The introspection system handles:
289
+ - **Abstract classes** - Validates model is not abstract
290
+ - **Missing tables** - Checks table existence
291
+ - **STI models** - Works with Single Table Inheritance
292
+ - **Polymorphic associations** - Handles correctly
293
+ - **Proc callbacks** - Shows as `<Proc>`
294
+ - **Conditional validations** - Displays if/unless conditions
295
+ - **State machine gems** - Detects AASM, StateMachines, Workflow
296
+ - **Namespaced models** - Works with module namespaces
297
+ - **Complex inheritance** - Shows full ancestry chain
298
+
299
+ ## Configuration
300
+
301
+ Enable/disable the introspect command:
302
+
303
+ ```ruby
304
+ # config/initializers/rails_console_pro.rb
305
+ RailsConsolePro.configure do |config|
306
+ config.introspect_command_enabled = true # default
307
+ end
308
+ ```
309
+
310
+ ## Tips & Best Practices
311
+
312
+ 1. **Start with full introspection** - Get the complete picture first
313
+ 2. **Use filters for focused analysis** - Add `:callbacks`, `:enums`, etc. when you know what you need
314
+ 3. **Export for documentation** - Generate HTML reports for team reference
315
+ 4. **Method source is powerful** - Use it to track down mysterious behavior
316
+ 5. **Combine with other commands** - Use `schema` for database structure, `introspect` for Ruby logic
317
+
318
+ ## Limitations
319
+
320
+ - Callbacks that are added dynamically at runtime may not appear
321
+ - Private methods in concerns may not have accurate source locations
322
+ - Some gems that modify ActiveRecord may interfere with introspection
323
+ - Very large models (100+ callbacks) may take a moment to process
324
+
325
+ ## Related Commands
326
+
327
+ - `schema Model` - Database schema and structure
328
+ - `stats Model` - Model statistics and metrics
329
+ - `diff obj1, obj2` - Compare two instances
330
+ - `explain Query` - SQL query analysis
331
+
332
+ ## Troubleshooting
333
+
334
+ **Q: Why don't I see any callbacks?**
335
+ A: Ensure your model has callbacks defined. Abstract classes and tableless models may not have callbacks.
336
+
337
+ **Q: Method source shows nil**
338
+ A: Some methods (built-in Rails methods, dynamically defined) don't have accessible source locations.
339
+
340
+ **Q: Scopes appear empty**
341
+ A: Only scopes that don't require arguments are shown. Parameterized scopes are skipped.
342
+
343
+ **Q: Concerns are missing**
344
+ A: Only concerns/modules with identifiable names are shown. Anonymous modules are excluded.
345
+
346
+ ## Examples
347
+
348
+ ### Simple Model
349
+ ```ruby
350
+ introspect BlogPost
351
+ # Shows validations, scopes, associations
352
+ ```
353
+
354
+ ### Complex Model with State Machine
355
+ ```ruby
356
+ introspect Order
357
+ # Shows state machine enums, callbacks, validations
358
+ ```
359
+
360
+ ### Finding Gem Methods
361
+ ```ruby
362
+ introspect User, :authenticate
363
+ # Shows it comes from Devise gem
364
+ ```
365
+
366
+ ### Debugging Callback Order
367
+ ```ruby
368
+ introspect Payment, :callbacks
369
+ # See exact order of before_save, after_commit, etc.
370
+ ```
371
+
@@ -70,3 +70,7 @@ export stats(User) user_stats.json
70
70
  - **Index Usage**: Which indexes are used frequently
71
71
  - **Column Statistics**: Unique values, distributions, ranges (for smaller tables)
72
72
 
73
+ ## Screenshots
74
+
75
+ <img width="1119" height="714" alt="Screenshot 2025-11-07 at 11 44 01 AM" src="https://github.com/user-attachments/assets/a175cb8c-1bea-4819-a80b-3f0bbb0d1a75" />
76
+
@@ -85,3 +85,9 @@ diff(user1, user2).to_json
85
85
  - **Multiple Object Types**: Works with ActiveRecord, Hash, and more
86
86
  - **Export Support**: Export diff results to JSON/YAML/HTML
87
87
 
88
+ ## Screenshots
89
+
90
+ <img width="1057" height="650" alt="Screenshot 2025-11-07 at 11 27 23 AM" src="https://github.com/user-attachments/assets/ca9fce82-8ed8-4506-907b-75e07735ec66" />
91
+
92
+
93
+
data/docs/PROFILING.md ADDED
@@ -0,0 +1,91 @@
1
+ # Adaptive Profiling
2
+
3
+ Rails Console Pro includes an adaptive profiler that wraps any block, callable, or ActiveRecord relation and reports real-time performance metrics without leaving the console.
4
+
5
+ ## Why Use It?
6
+
7
+ - Understand how long a console experiment takes end-to-end
8
+ - See how much time is spent inside SQL queries
9
+ - Detect cache hits/misses and duplicated queries (potential N+1s)
10
+ - Keep a sample of executed SQL statements with bind values
11
+ - Capture errors while still getting timing information
12
+
13
+ ## Basic Usage
14
+
15
+ ```ruby
16
+ # Profile a block
17
+ profile { User.active.limit(25).to_a }
18
+
19
+ # Add a label for the session
20
+ profile('Load active users') { User.active.includes(:posts).limit(25).to_a }
21
+
22
+ # Profile a relation (loads it automatically)
23
+ relation = User.includes(:posts).limit(10)
24
+ profile relation
25
+
26
+ # Profile any callable object
27
+ profile -> { HeavyService.call(user) }
28
+ ```
29
+
30
+ `profile` returns a `RailsConsolePro::ProfileResult` instance, so you can further inspect or export the collected metrics.
31
+
32
+ ## Sample Output
33
+
34
+ ```
35
+ 🧪 PROFILE: Load active users
36
+ ⏱ Execution Summary:
37
+ Total time 35.42 ms
38
+ SQL time 28.12 ms
39
+ (79.36% of total time spent in SQL)
40
+
41
+ 🗂 Query Breakdown:
42
+ Total queries 4
43
+ Read queries 4
44
+ Write queries 0
45
+ Cached queries 1
46
+
47
+ 🐢 Slow Queries (100.0ms+):
48
+ 1. 120.44 ms SELECT "users".* FROM ...
49
+ ```
50
+
51
+ The printer also highlights cache activity, sample queries, potential N+1 issues, and any error raised during execution.
52
+
53
+ ## Configuration
54
+
55
+ Tune profiling behaviour via the initializer:
56
+
57
+ ```ruby
58
+ RailsConsolePro.configure do |config|
59
+ # Enable/disable the profile command
60
+ config.profile_command_enabled = true
61
+
62
+ # Flag queries above this threshold (milliseconds)
63
+ config.profile_slow_query_threshold = 120.0
64
+
65
+ # Minimum occurrences before a query is treated as a possible N+1
66
+ config.profile_duplicate_query_threshold = 3
67
+
68
+ # Number of query samples to keep in memory for reporting
69
+ config.profile_max_saved_queries = 10
70
+ end
71
+ ```
72
+
73
+ ## Exporting
74
+
75
+ Profile results can be exported like any other value object:
76
+
77
+ ```ruby
78
+ result = profile { User.active.limit(10).to_a }
79
+
80
+ result.to_json
81
+ result.to_yaml
82
+ result.export_to_file('profile.html', format: :html)
83
+ ```
84
+
85
+ ## Tips
86
+
87
+ - Combine profiling with Rails scopes to analyse real data paths
88
+ - Lower `profile_slow_query_threshold` when testing on local databases
89
+ - Use labels to differentiate multiple runs in the same console session
90
+ - Errors are captured and displayed; you still get timings even when the block fails
91
+