module ActiveRecord::Annotate

Constants

VERSION

Public Class Methods

annotate() click to toggle source
# File lib/active_record/annotate.rb, line 10
def annotate
  processed_models = []
  
  models.each do |table_name, file_paths_and_classes|
    annotation = Dumper.dump(table_name)
    
    file_paths_and_classes.each do |path, klass|
      file = File.new(path)
      file.annotate_with(annotation.dup, configurator)
      
      if file.changed?
        file.write
        processed_models << "#{klass} (#{file.relative_path})"
      end
    end
  end
  
  unless processed_models.empty?
    puts 'Annotated models:'
    processed_models.each do |model|
      puts "  * #{model}"
    end
  end
end
class_name_for(short_path) click to toggle source

car/hatchback -> Car::Hatchback

# File lib/active_record/annotate.rb, line 59
def class_name_for(short_path)
  short_path.camelize.constantize
end
configure(&block) click to toggle source
# File lib/active_record/annotate.rb, line 63
def configure(&block)
  configurator.tap(&block)
end
models() click to toggle source
# File lib/active_record/annotate.rb, line 35
def models
  files_mask = models_dir.join('**', '*.rb')
  
  hash_with_arrays = Hash.new do |hash, key|
    hash[key] = []
  end
  
  Dir.glob(files_mask).each_with_object(hash_with_arrays) do |path, models|
    short_path = short_path_for(path)
    next if short_path.starts_with?('concerns') # skip any app/models/concerns files
    
    klass = class_name_for(short_path)
    next unless klass < ActiveRecord::Base # collect only AR::Base descendants
    
    models[klass.table_name] << [path, klass]
  end
end
short_path_for(full_path) click to toggle source

…/app/models/car/hatchback.rb -> car/hatchback

# File lib/active_record/annotate.rb, line 54
def short_path_for(full_path)
  full_path.sub(models_dir.to_s + '/', '').sub(/\.rb$/, '')
end

Private Class Methods

configurator() click to toggle source
# File lib/active_record/annotate.rb, line 72
def configurator
  @configurator ||= Configurator.new
end
models_dir() click to toggle source
# File lib/active_record/annotate.rb, line 68
def models_dir
  Rails.root.join('app/models')
end