Class: ScaffoldPlus::Generators::AncestryGenerator

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

Instance Method Summary collapse

Instance Method Details

#add_migrationObject



23
24
25
26
# File 'lib/generators/scaffold_plus/ancestry/ancestry_generator.rb', line 23

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

#update_controllerObject



39
40
41
42
43
44
45
46
# File 'lib/generators/scaffold_plus/ancestry/ancestry_generator.rb', line 39

def update_controller
  return unless options.permit?
  text = ":ancestry"
  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



28
29
30
31
32
33
34
35
36
37
# File 'lib/generators/scaffold_plus/ancestry/ancestry_generator.rb', line 28

def update_model
  inject_into_class "app/models/#{name}.rb", class_name do
    text = options.before? ? "\n" : ""
    text << "  has_ancestry"
    text << ", cache_depth: true" if options.depth?
    text << "\n"
    text << "\n" if options.after?
    text
  end
end