Class: RailsConsolePro::Services::IntrospectionCollector

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_console_pro/services/introspection_collector.rb

Overview

Service to collect model introspection data

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model_class) ⇒ IntrospectionCollector

Returns a new instance of IntrospectionCollector.



9
10
11
# File 'lib/rails_console_pro/services/introspection_collector.rb', line 9

def initialize(model_class)
  @model_class = model_class
end

Instance Attribute Details

#model_classObject (readonly)

Returns the value of attribute model_class.



7
8
9
# File 'lib/rails_console_pro/services/introspection_collector.rb', line 7

def model_class
  @model_class
end

Instance Method Details

#collectObject

Collect all introspection data



14
15
16
17
18
19
20
21
22
23
# File 'lib/rails_console_pro/services/introspection_collector.rb', line 14

def collect
  {
    callbacks: collect_callbacks,
    enums: collect_enums,
    concerns: collect_concerns,
    scopes: collect_scopes,
    validations: collect_validations,
    lifecycle_hooks: collect_lifecycle_hooks
  }
end

#collect_callbacksObject

Collect all callbacks with their order



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rails_console_pro/services/introspection_collector.rb', line 26

def collect_callbacks
  return {} unless model_class.respond_to?(:_commit_callbacks)

  callback_types = [
    :before_validation, :after_validation,
    :before_save, :around_save, :after_save,
    :before_create, :around_create, :after_create,
    :before_update, :around_update, :after_update,
    :before_destroy, :around_destroy, :after_destroy,
    :after_commit, :after_rollback,
    :after_find, :after_initialize,
    :after_touch
  ]

  callbacks = {}
  callback_types.each do |type|
    chain = get_callback_chain(type)
    next if chain.empty?

    callbacks[type] = chain
  end

  callbacks
end

#collect_concernsObject

Collect all concerns and modules



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/rails_console_pro/services/introspection_collector.rb', line 67

def collect_concerns
  return [] unless model_class.respond_to?(:ancestors)

  concerns = []
  model_class.ancestors.each do |ancestor|
    next if ancestor == model_class
    next if [ActiveRecord::Base, Object, BasicObject, Kernel].include?(ancestor)
    next if ancestor.name.nil? || ancestor.name.empty?
    next if ancestor.name.start_with?('ActiveRecord::', 'ActiveSupport::')
    
    # Check if it's a concern or module
    is_concern = ancestor.respond_to?(:included_modules) && 
                 ancestor.included_modules.include?(ActiveSupport::Concern)
    
    concerns << {
      name: ancestor.name,
      type: is_concern ? :concern : (ancestor.is_a?(Class) ? :class : :module),
      location: source_location_for(ancestor)
    }
  end

  concerns.uniq { |c| c[:name] }
rescue => e
  []
end

#collect_enumsObject

Collect all enums



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rails_console_pro/services/introspection_collector.rb', line 52

def collect_enums
  return {} unless model_class.respond_to?(:defined_enums)

  model_class.defined_enums.transform_values do |mapping|
    {
      mapping: mapping,
      values: mapping.keys,
      type: detect_enum_type(mapping)
    }
  end
rescue => e
  {}
end

#collect_lifecycle_hooksObject

Collect lifecycle hooks summary



154
155
156
157
158
159
160
161
162
163
# File 'lib/rails_console_pro/services/introspection_collector.rb', line 154

def collect_lifecycle_hooks
  {
    callbacks_count: count_callbacks,
    validations_count: count_validations,
    has_observers: has_observers?,
    has_state_machine: has_state_machine?
  }
rescue => e
  {}
end

#collect_scopesObject

Collect all scopes with their SQL



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/rails_console_pro/services/introspection_collector.rb', line 94

def collect_scopes
  return {} unless model_class.respond_to?(:scope_attributes?)

  scopes = {}
  
  # Get all singleton methods that might be scopes
  scope_methods = model_class.methods(false) - ActiveRecord::Base.methods(false)
  
  scope_methods.each do |method_name|
    next if method_name.to_s.start_with?('_')
    
    begin
      # Try to call the scope and get its SQL
      scope_result = model_class.public_send(method_name)
      
      if scope_result.is_a?(ActiveRecord::Relation)
        scopes[method_name] = {
          sql: scope_result.to_sql,
          values: extract_scope_values(scope_result),
          conditions: extract_scope_conditions(scope_result)
        }
      end
    rescue ArgumentError, NameError, NoMethodError
      # Skip if it requires arguments or is not a scope
      next
    rescue => e
      # Skip problematic scopes
      next
    end
  end

  scopes
rescue => e
  {}
end

#collect_validationsObject

Collect all validations



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/rails_console_pro/services/introspection_collector.rb', line 131

def collect_validations
  return [] unless model_class.respond_to?(:validators)

  validations = []
  
  model_class.validators.each do |validator|
    attributes = validator.attributes rescue [:unknown]
    
    validations << {
      type: validator.class.name.demodulize,
      attributes: attributes,
      options: extract_validator_options(validator),
      conditions: extract_validator_conditions(validator)
    }
  end

  # Group by attribute for better organization
  validations.group_by { |v| v[:attributes].first }
rescue => e
  []
end

#method_source_location(method_name) ⇒ Object

Find where a method is defined



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/rails_console_pro/services/introspection_collector.rb', line 166

def method_source_location(method_name)
  return nil unless model_class.respond_to?(method_name)

  method = if model_class.respond_to?(method_name)
             model_class.method(method_name)
           else
             return nil
           end

  location = method.source_location
  return nil unless location

  {
    file: location[0],
    line: location[1],
    owner: method.owner.name,
    type: determine_method_type(method.owner)
  }
rescue => e
  nil
end