class ActiveRecord::Generators::CsvImportMagicGenerator

Public Instance Methods

generate_model() click to toggle source
# File lib/generators/active_record/csv_import_magic_generator.rb, line 12
def generate_model
  invoke 'active_record:model', [name], migration: false unless model_exists? && behavior == :invoke

  class_path = if name.match(/\:\:/).present?
                 name.to_s.gsub('::', '/')
               else
                 name
  end

  template 'csv_parser.rb', "app/csv_parsers/#{class_path}_parser.rb"
end
inject_csv_import_magic_content() click to toggle source
# File lib/generators/active_record/csv_import_magic_generator.rb, line 24
def inject_csv_import_magic_content
  content = model_contents(options)
  class_path = [class_name]

  indent_depth = class_path.size - 1
  content = content.split("\n").map { |line| '  ' * indent_depth + line } .join("\n") << "\n"

  inject_into_class(model_path, class_path.last, content) if model_exists?
end