Sha256: 84b03060c1b71ad3420121290400135975af8985079c312dc7798993f31ab499

Contents?: true

Size: 1.85 KB

Versions: 4

Compression:

Stored size: 1.85 KB

Contents

require 'rails/generators/active_record'

module ScaffoldPlus
  module Generators
    class FriendlyIdGenerator < ActiveRecord::Generators::Base
      desc "Add friendly_id to resource"
      argument :name, type: :string,
               desc: 'The object that uses friendly_id'
      argument :attribute, type: :string, default: 'name',
               desc: 'The model attribute to be slugged'
      class_option :migration, type: :boolean, default: true,
               desc: 'Create migration for the slug attribute'
      class_option :finders, type: :boolean, default: false,
               desc: 'Perform friendly finds all the time'
      class_option :before, type: :boolean, default: false,
               desc: 'Add a line before generated text in model'
      class_option :after, type: :boolean, default: false,
               desc: 'Add a line after generated text in model'
      source_root File.expand_path('../templates', __FILE__)

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

      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

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

      protected

      def migration_name
        "add_friendly_id_to_#{table_name}"
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
scaffold_plus-2.1.4 lib/generators/scaffold_plus/friendly_id/friendly_id_generator.rb
scaffold_plus-2.1.3 lib/generators/scaffold_plus/friendly_id/friendly_id_generator.rb
scaffold_plus-2.1.2 lib/generators/scaffold_plus/friendly_id/friendly_id_generator.rb
scaffold_plus-2.1.1 lib/generators/scaffold_plus/friendly_id/friendly_id_generator.rb