Class: ScaffoldPlus::Generators::EnumerizeGenerator

Inherits:
ActiveRecord::Generators::Base
  • Object
show all
Defined in:
lib/generators/scaffold_plus/enumerize/enumerize_generator.rb

Instance Method Summary collapse

Instance Method Details

#add_migrationObject



25
26
27
28
# File 'lib/generators/scaffold_plus/enumerize/enumerize_generator.rb', line 25

def add_migration
  return unless options.migration?
  migration_template "enum_migration.rb", "db/migrate/#{migration_name}.rb"
end

#update_controllerObject



40
41
42
43
44
45
46
47
# File 'lib/generators/scaffold_plus/enumerize/enumerize_generator.rb', line 40

def update_controller
  return unless options.permit?
  text = ":#{column}"
  file = "app/controllers/#{table_name}_controller.rb"
  gsub_file file, /(permit\(.*)\)/, "\\1, #{text})"
  # Special case: no previous permit
  gsub_file file, /^(\s*params)\[:#{name}\]$/, "\\1.require(:#{name}).permit(#{text})"
end

#update_modelObject



30
31
32
33
34
35
36
37
38
# File 'lib/generators/scaffold_plus/enumerize/enumerize_generator.rb', line 30

def update_model
  inject_into_class "app/models/#{name}.rb", class_name do
    text = options.before? ? "\n" : ""
    text << "  extend Enumerize\n"
    text << "  enumerize :#{column}, in: #{values}\n"
    text << "\n" if options.after?
    text
  end
end