rails_console_pro 0.1.3 → 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 (30) hide show
  1. checksums.yaml +4 -4
  2. data/.rspec_status +259 -232
  3. data/CHANGELOG.md +3 -0
  4. data/QUICK_START.md +9 -0
  5. data/README.md +27 -0
  6. data/docs/MODEL_INTROSPECTION.md +371 -0
  7. data/docs/QUERY_BUILDER.md +385 -0
  8. data/lib/rails_console_pro/commands/compare_command.rb +151 -0
  9. data/lib/rails_console_pro/commands/introspect_command.rb +220 -0
  10. data/lib/rails_console_pro/commands/query_builder_command.rb +43 -0
  11. data/lib/rails_console_pro/commands.rb +15 -0
  12. data/lib/rails_console_pro/compare_result.rb +81 -0
  13. data/lib/rails_console_pro/configuration.rb +12 -0
  14. data/lib/rails_console_pro/format_exporter.rb +24 -0
  15. data/lib/rails_console_pro/global_methods.rb +12 -0
  16. data/lib/rails_console_pro/initializer.rb +18 -1
  17. data/lib/rails_console_pro/introspect_result.rb +101 -0
  18. data/lib/rails_console_pro/printers/compare_printer.rb +138 -0
  19. data/lib/rails_console_pro/printers/introspect_printer.rb +282 -0
  20. data/lib/rails_console_pro/printers/query_builder_printer.rb +81 -0
  21. data/lib/rails_console_pro/query_builder.rb +197 -0
  22. data/lib/rails_console_pro/query_builder_result.rb +66 -0
  23. data/lib/rails_console_pro/serializers/compare_serializer.rb +66 -0
  24. data/lib/rails_console_pro/serializers/introspect_serializer.rb +99 -0
  25. data/lib/rails_console_pro/serializers/query_builder_serializer.rb +35 -0
  26. data/lib/rails_console_pro/services/introspection_collector.rb +420 -0
  27. data/lib/rails_console_pro/snippets/collection_result.rb +1 -0
  28. data/lib/rails_console_pro/snippets.rb +1 -0
  29. data/lib/rails_console_pro/version.rb +1 -1
  30. metadata +17 -1
data/QUICK_START.md CHANGED
@@ -59,6 +59,14 @@ 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
+
62
70
  ### Snippet Library
63
71
  ```ruby
64
72
  snippets(:add, "User.where(active: true).count", description: "Active users", tags: %w[users metrics])
@@ -108,6 +116,7 @@ rails generate rails_console_pro:install
108
116
  - [SQL Explain](docs/SQL_EXPLAIN.md)
109
117
  - [Model Statistics](docs/MODEL_STATISTICS.md)
110
118
  - [Association Navigation](docs/ASSOCIATION_NAVIGATION.md)
119
+ - [Model Introspection](docs/MODEL_INTROSPECTION.md)
111
120
  - [Object Diffing](docs/OBJECT_DIFFING.md)
112
121
  - [Export](docs/EXPORT.md)
113
122
  - [Snippets](docs/SNIPPETS.md)
data/README.md CHANGED
@@ -17,6 +17,9 @@ Rails Console Pro transforms your Rails console into a powerful debugging enviro
17
17
  - 🔬 **Adaptive Profiling** - Profile blocks or relations with query, cache, and performance metrics
18
18
  - 🧵 **ActiveJob Insights** - Inspect and manage queues across adapters (Sidekiq, SolidQueue, Test, Async) with filters and inline actions
19
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
20
23
  - 💾 **Export Capabilities** - Export to JSON, YAML, and HTML formats
21
24
  - 📄 **Smart Pagination** - Automatic pagination for large collections
22
25
  - 📝 **Snippet Library** - Capture, search, and reuse console snippets across sessions
@@ -74,6 +77,26 @@ jobs(limit: 10, queue: 'mailers')
74
77
  jobs status=retry class=ReminderJob
75
78
  jobs retry=abcdef123456
76
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
77
100
  ```
78
101
 
79
102
  See [QUICK_START.md](QUICK_START.md) for more examples and detailed documentation for each feature.
@@ -91,6 +114,8 @@ RailsConsolePro.configure do |config|
91
114
  config.explain_command_enabled = true
92
115
  config.stats_command_enabled = true
93
116
  config.queue_command_enabled = true
117
+ config.compare_command_enabled = true
118
+ config.query_builder_command_enabled = true
94
119
 
95
120
  # Color scheme
96
121
  config.color_scheme = :dark # or :light
@@ -118,12 +143,14 @@ end
118
143
  - [SQL Explain](docs/SQL_EXPLAIN.md) - Analyze query performance
119
144
  - [Model Statistics](docs/MODEL_STATISTICS.md) - Get model statistics
120
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
121
147
  - [Object Diffing](docs/OBJECT_DIFFING.md) - Compare objects
122
148
  - [Export](docs/EXPORT.md) - Export to JSON, YAML, HTML
123
149
  - [Snippets](docs/SNIPPETS.md) - Build a reusable console snippet library
124
150
  - [Formatting](docs/FORMATTING.md) - Beautiful console output
125
151
  - [Adaptive Profiling](docs/PROFILING.md) - Measure queries, cache hits, and potential N+1 issues
126
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
127
154
 
128
155
  ## 🤝 Contributing
129
156
 
@@ -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
+