Module: ActiveRecord::Annotate

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

Defined Under Namespace

Modules: Dumper Classes: Configurator, File, InstallGenerator, Railtie

Constant Summary collapse

VERSION =
'0.4.1'

Class Method Summary collapse

Class Method Details

.annotateObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# 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) ⇒ Object

car/hatchback -> Car::Hatchback



59
60
61
# File 'lib/active_record/annotate.rb', line 59

def class_name_for(short_path)
  short_path.camelize.constantize
end

.configure(&block) ⇒ Object



63
64
65
# File 'lib/active_record/annotate.rb', line 63

def configure(&block)
  configurator.tap(&block)
end

.modelsObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# 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) ⇒ Object

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



54
55
56
# File 'lib/active_record/annotate.rb', line 54

def short_path_for(full_path)
  full_path.sub(models_dir.to_s + '/', '').sub(/\.rb$/, '')
end