Module: ActiveRecord::Annotate

Defined in:
lib/active_record/annotate.rb,
lib/active_record/annotate/dumper.rb,
lib/active_record/annotate/writer.rb,
lib/active_record/annotate/railtie.rb,
lib/active_record/annotate/version.rb

Defined Under Namespace

Modules: Dumper, Writer Classes: Railtie

Constant Summary collapse

VERSION =
'0.1'

Class Method Summary collapse

Class Method Details

.annotateObject



9
10
11
12
13
14
# File 'lib/active_record/annotate.rb', line 9

def annotate
  models.each do |table_name, file_path|
    annotation = Dumper.dump(table_name)
    Writer.write(annotation, file_path)
  end
end

.modelsObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/active_record/annotate.rb', line 16

def models
  models_dir = Rails.root.join('app/models')
  files_mask = models_dir.join('**', '*.rb')
  
  Dir.glob(files_mask).each_with_object(Hash.new) 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