Class: GemActivityTracker::Tracker

Inherits:
Object
  • Object
show all
Defined in:
lib/gem_activity_tracker/tracker.rb

Class Method Summary collapse

Class Method Details

.analyze_models(path) ⇒ Object



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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/gem_activity_tracker/tracker.rb', line 98

def self.analyze_models(path)
  result = { count: 0, files: [], detailed: {} }

  ActiveSupport::Dependencies.autoload_paths += Dir["#{path}/app/models/**/"]

  Dir.glob("#{path}/app/models/**/*.rb").each do |file|
    relative_path = file.gsub("#{path}/", '')
    model_name = relative_path.sub('app/models/', '').sub('.rb', '').camelize

    begin
      require_dependency file
      model_class = model_name.constantize
      next unless model_class < ActiveRecord::Base

      result[:count] += 1
      result[:files] << relative_path

      # Validations
      validations = Hash.new { |hash, key| hash[key] = [] }
      model_class.validators.each do |validator|
        validator.attributes.each do |attr|
          validations[attr] << validator.class.name.demodulize.underscore
        end
      end

      # Callbacks
      callbacks = {}
      ActiveRecord::Callbacks::CALLBACKS.each do |cb|
        if model_class.respond_to?("_#{cb}_callbacks")
          methods = model_class.send("_#{cb}_callbacks").map(&:filter).uniq
          callbacks[cb] = methods.map(&:to_s) if methods.any?
        end
      end

      # Enums
      # debugger
      enums = model_class.defined_enums.transform_values { |v| v.keys }

      # Associations
      associations = model_class.reflect_on_all_associations.group_by(&:macro).transform_values { |a| a.map(&:name) }

      # Scopes
      scopes = model_class.methods(false).select do |method|
        model_class.method(method).source_location&.first&.include?('/models/')
      end.select { |m| model_class.respond_to?(m) && model_class.send(m).is_a?(ActiveRecord::Relation) rescue false }

      # Class methods
      class_methods = (model_class.methods(false) - ActiveRecord::Base.methods)
      instance_methods = (model_class.instance_methods(false) - ActiveRecord::Base.instance_methods).map(&:to_s)

      # # Comments
      # comment = File.foreach(file).first if File.read(file).lines.first.strip.start_with?('#')

      result[:detailed][model_class.name] = {
        table_name: model_class.table_name,
        file: relative_path,
        # comment: comment&.strip,
        attributes: model_class.columns.map(&:name),
        associations: associations,
        validations: validations,
        enums: enums,
        callbacks: callbacks,
        scopes: scopes.map(&:to_s),
        class_methods: class_methods.map(&:to_s),
        instance_methods: instance_methods,
        methods_count: model_class.instance_methods(false).count
      }

    rescue => e
      result[:detailed][model_name] = { error: "Failed to load: #{e.message}" }
    end
  end

  result
end

.collect_data(path) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/gem_activity_tracker/tracker.rb', line 29

def self.collect_data(path)
  {
    project_name: File.basename(path),
    ruby_version: `ruby -v`.strip,
    # rails_version: get_rails_version(path),
    rails_version: Rails.version,
    database: detect_database(path),
    models: analyze_models(path),
    controllers: list_and_count_files(path, "app/controllers"),
    jobs: list_and_count_files(path, "app/jobs"),
    mailers: list_and_count_files(path, "app/mailers"),
    services: list_and_count_files(path, "app/services"),
    migrations: migration_changes(path),
    schema_hash: schema_hash(path),
    routes: get_routes(path),
    git_log: get_git_log(path)
  }
end

.detect_database(path) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/gem_activity_tracker/tracker.rb', line 64

def self.detect_database(path)
  db_file = File.join(path, "config/database.yml")
  return 'Unknown' unless File.exist?(db_file)

  config = YAML.load_file(db_file, aliases: true)
  config["development"]["adapter"] rescue "Unknown"
end

.export(path, format = :json) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/gem_activity_tracker/tracker.rb', line 176

def self.export(path, format = :json)
  file = "#{path}/activity_tracker/report.yml"
  return puts "❌ Report not found." unless File.exist?(file)

  data = YAML.load_file(file)

  case format
  when :json
    File.write("#{path}/activity_tracker/report.json", JSON.pretty_generate(data))
    puts "✅ Exported to report.json"
  when :csv
    CSV.open("#{path}/activity_tracker/report.csv", "w") do |csv|
      csv << ["Key", "Value"]
      data.each do |key, value|
        if value.is_a?(Hash)
          csv << [key.to_s, ""]
          value.each { |k, v| csv << ["  #{k}", v.to_json] }
        elsif value.is_a?(Array)
          csv << [key.to_s, "#{value.count} items"]
          value.each { |item| csv << ["", item] }
        else
          csv << [key.to_s, value]
        end
      end
    end
    puts "✅ Exported to report.csv"
  else
    puts "❌ Unsupported format: #{format}"
  end
end

.get_git_log(path) ⇒ Object



91
92
93
94
95
96
# File 'lib/gem_activity_tracker/tracker.rb', line 91

def self.get_git_log(path)
  Dir.chdir(path) do
    log = `git log --pretty=format:'%h - %an (%ad): %s' --date=short -n 20`
    log.split("\n")
  end
end

.get_rails_version(path) ⇒ Object



59
60
61
62
# File 'lib/gem_activity_tracker/tracker.rb', line 59

def self.get_rails_version(path)
  gemfile = File.join(path, 'Gemfile.lock')
  File.exist?(gemfile) ? File.read(gemfile)[/rails \((.*?)\)/, 1] : 'Not a Rails project'
end

.get_routes(path) ⇒ Object



72
73
74
75
# File 'lib/gem_activity_tracker/tracker.rb', line 72

def self.get_routes(path)
  output = `cd #{path} && RAILS_ENV=development bundle exec rails routes 2>/dev/null`
  output.empty? ? "No routes found or Rails not installed" : output
end

.list_and_count_files(base, dir) ⇒ Object



53
54
55
56
57
# File 'lib/gem_activity_tracker/tracker.rb', line 53

def self.list_and_count_files(base, dir)
  path = File.join(base, dir)
  files = Dir.glob("#{path}/**/*.rb").map { |f| f.gsub(base + '/', '') }
  { count: files.count, files: files }
end

.migration_changes(path) ⇒ Object



77
78
79
80
81
82
83
84
# File 'lib/gem_activity_tracker/tracker.rb', line 77

def self.migration_changes(path)
  files = Dir.glob("#{path}/db/migrate/*.rb")
  recent = files.sort_by { |f| File.mtime(f) }.last(10)
  {
    count: files.count,
    recent_changes: recent.map { |f| File.basename(f, ".rb").gsub(/^\d+_/, '') }
  }
end

.reportObject



48
49
50
51
# File 'lib/gem_activity_tracker/tracker.rb', line 48

def self.report
  file = "activity_tracker/report.yml"
  puts File.exist?(file) ? YAML.load_file(file).to_yaml : "❌ No report found."
end

.schema_hash(path) ⇒ Object



86
87
88
89
# File 'lib/gem_activity_tracker/tracker.rb', line 86

def self.schema_hash(path)
  file = File.join(path, "db/schema.rb")
  File.exist?(file) ? Digest::MD5.hexdigest(File.read(file)) : nil
end

.track(path) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/gem_activity_tracker/tracker.rb', line 12

def self.track(path)
  puts "🔍 Tracking project at: #{path}"

  if !defined?(Rails) || Rails.root.to_s != path
    # Only require environment if we're not already inside a Rails app
    $LOAD_PATH.unshift(path)
    ENV['RAILS_ENV'] ||= 'development'
    require File.join(path, 'config', 'environment')
  end

  data = collect_data(path)
  FileUtils.mkdir_p("#{path}/activity_tracker")
  File.write("#{path}/activity_tracker/report.yml", data.to_yaml)

  puts "✅ Report generated at: #{path}/activity_tracker/report.yml"
end