Class: ScaffoldPlus::Generators::HabtmGenerator

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

Instance Method Summary collapse

Instance Method Details

#add_migrationObject



21
22
23
24
# File 'lib/generators/scaffold_plus/habtm/habtm_generator.rb', line 21

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

#add_to_modelsObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/generators/scaffold_plus/habtm/habtm_generator.rb', line 26

def add_to_models
  inject_into_class "app/models/#{name}.rb", class_name do
    text = before_array.include?(name) ? "\n" : ""
    text << "  has_and_belongs_to_many :#{other.pluralize}\n"
    text << "\n" if after_array.include?(name)
    text
  end
  inject_into_class "app/models/#{other}.rb", other.camelize do
    text = before_array.include?(other) ? "\n" : ""
    text << "  has_and_belongs_to_many :#{table_name}\n"
    text << "\n" if after_array.include?(other)
    text
  end
end

#add_to_permitObject



41
42
43
44
45
46
47
48
# File 'lib/generators/scaffold_plus/habtm/habtm_generator.rb', line 41

def add_to_permit
  return unless options.permit?
  text = "{ :#{other}_ids => [] }"
  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