Class: ScaffoldPlus::Generators::FriendlyIdGenerator

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

Instance Method Summary collapse

Instance Method Details

#add_migrationObject



21
22
23
24
# File 'lib/generators/scaffold_plus/friendly_id/friendly_id_generator.rb', line 21

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

#update_controllerObject



40
41
42
43
44
# File 'lib/generators/scaffold_plus/friendly_id/friendly_id_generator.rb', line 40

def update_controller
  return if options.finders?
  file = "app/controllers/#{table_name}_controller.rb"
  gsub_file file, /(#{class_name})\.find/, "\\1.friendly.find"
end

#update_modelObject



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

def update_model
  inject_into_class "app/models/#{name}.rb", class_name do
    text = options.before? ? "\n" : ""
    text << "  extend FriendlyId\n"
    if options.finders?
      text << "  friendly_id :#{attribute}, use: [:slugged, :finders]\n"
    else
      text << "  friendly_id :#{attribute}, use: :slugged\n"
    end
    text << "\n" if options.after?
    text
  end
end