class ScaffoldPlus::Generators::ManyToManyGenerator

Public Instance Methods

add_counter() click to toggle source
# File lib/generators/scaffold_plus/many_to_many/many_to_many_generator.rb, line 36
def add_counter
  return unless options.counter?
  migration_template 'counter_migration.rb', "db/migrate/#{counter_migration}.rb"
end
add_migration() click to toggle source
# File lib/generators/scaffold_plus/many_to_many/many_to_many_generator.rb, line 31
def add_migration
  return unless options.migration?
  migration_template 'many_to_many_migration.rb', "db/migrate/#{migration_name}.rb"
end
add_to_models() click to toggle source
# File lib/generators/scaffold_plus/many_to_many/many_to_many_generator.rb, line 41
def add_to_models
  [[one, two], [two, one]].each do |pair|
    current, partner = pair
    inject_into_class "app/models/#{current}.rb", current.camelize do
      text = before_array.include?(current) ? "\n" : ""
      text << "  has_many :#{table_name}"
      text << ", dependent: :#{dependent}" if options.dependent.present?
      text << "\n"
      text << "  has_many :#{partner.pluralize}, through: :#{table_name}\n"
      if current == one
        text << "  accepts_nested_attributes_for :#{table_name}\n" if options.nested.present?
      end
      text << "\n" if after_array.include?(current)
      text
    end
  end

  template 'many_to_many_model.rb', "app/models/#{name}.rb"
end
add_to_permit() click to toggle source
# File lib/generators/scaffold_plus/many_to_many/many_to_many_generator.rb, line 61
def add_to_permit
  return unless options.nested.present?
  list = options.nested.map{|n| ":#{n}"}.join(', ')
  text = "#{table_name}_attributes: [ #{list} ]"
  file = "app/controllers/#{one.pluralize}_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

Protected Instance Methods

added_fields() click to toggle source
# File lib/generators/scaffold_plus/many_to_many/many_to_many_generator.rb, line 73
def added_fields
  list = options.add_attr || []
  array = []
  list.each do |entry|
    name, type, index = entry.split(':')
    type, index = ["string", type] if %w(index uniq).include? type
    array << [name, type || "string", index]
  end
  array
end
after_array() click to toggle source
# File lib/generators/scaffold_plus/many_to_many/many_to_many_generator.rb, line 88
def after_array
  options.after || []
end
before_array() click to toggle source
# File lib/generators/scaffold_plus/many_to_many/many_to_many_generator.rb, line 84
def before_array
  options.before || []
end
counter_cache() click to toggle source
# File lib/generators/scaffold_plus/many_to_many/many_to_many_generator.rb, line 108
def counter_cache
  options.counter? ? ", counter_cache: true" : ""
end
counter_migration() click to toggle source
# File lib/generators/scaffold_plus/many_to_many/many_to_many_generator.rb, line 104
def counter_migration
  "add_#{table_name}_count_to_#{one}"
end
dependent() click to toggle source
# File lib/generators/scaffold_plus/many_to_many/many_to_many_generator.rb, line 92
def dependent
  if options.dependent.present? and options.dependent == "restrict"
    "restrict_with_exception"
  else
    options.dependent
  end
end
migration_name() click to toggle source
# File lib/generators/scaffold_plus/many_to_many/many_to_many_generator.rb, line 100
def migration_name
  "create_#{table_name}"
end