Class: AwesomeAnnotate::CLI

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/awesome_annotate/cli.rb

Instance Method Summary collapse

Instance Method Details

#model(model_name) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/awesome_annotate/cli.rb', line 10

def model(model_name)
  rails_env_file = Pathname.new('./config/environment.rb')
  abort "Rails application path is required" unless rails_env_file.exist?

  apply rails_env_file.to_s

  name = model_name.singularize.camelize
  klass = Object.const_get(name)

  return puts 'This model does not inherit activerecord' unless klass < ActiveRecord::Base

  column_names = klass.column_names
  model_dir = Pathname.new('app/models')
  file_path = "#{model_dir.to_s}/#{model_name}.rb"

  return puts "Model file not found" unless File.exist?(file_path)

  insert_into_file file_path, :before => / class #{klass} \n|class #{klass} .*\n / do
    "# Columns: #{column_names.join(', ')}\n"
  end

  puts "annotate #{model_name.pluralize} table columns in #{file_path}"
end