Sha256: 73f8cc1ec28e46cfe3e62d3182204da535131d7da982d0a8997df0c76db11ca0

Contents?: true

Size: 1.25 KB

Versions: 1

Compression:

Stored size: 1.25 KB

Contents

require 'active_record/annotate/dumper'
require 'active_record/annotate/writer'
require 'active_record/annotate/version'
require 'active_record/annotate/railtie'

module ActiveRecord
  module Annotate
    class << self
      def annotate
        models.each do |table_name, file_paths|
          annotation = Dumper.dump(table_name)
          
          file_paths.each do |path|
            Writer.write(annotation, path)
          end
        end
      end
      
      def models
        models_dir = Rails.root.join('app/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|
          # .../app/models/car/hatchback.rb -> car/hatchback
          short_path = path.sub(models_dir.to_s + '/', '').sub(/\.rb$/, '')
          # skip any app/models/concerns files
          next if short_path.starts_with?('concerns')
          
          # car/hatchback -> Car::Hatchback
          klass = short_path.camelize.constantize
          # collect only AR::Base descendants
          models[klass.table_name] << path if klass < ActiveRecord::Base
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
active_record-annotate-0.1.1 lib/active_record/annotate.rb