Class: Knitsearch::MultisearchInstallGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Includes:
Rails::Generators::Migration
Defined in:
lib/generators/knitsearch/multisearch_install/multisearch_install_generator.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.next_migration_number(dirname) ⇒ Object



18
19
20
# File 'lib/generators/knitsearch/multisearch_install/multisearch_install_generator.rb', line 18

def self.next_migration_number(dirname)
  ::ActiveRecord::Generators::Base.next_migration_number(dirname)
end

Instance Method Details

#create_migrationObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/generators/knitsearch/multisearch_install/multisearch_install_generator.rb', line 41

def create_migration
  timestamp = self.class.next_migration_number("db/migrate")
  migration_filename = "#{timestamp}_create_knitsearches_multisearch.rb"
  migration_path = File.join(destination_root, "db/migrate", migration_filename)

  migration_content = <<~RUBY
    class CreateKnitsearchesMultisearch < ActiveRecord::Migration#{migration_version}
      include Knitsearch::Migration

      def up
        create_multisearch_table
      end

      def down
        drop_multisearch_table
      end
    end
  RUBY

  create_file migration_path, migration_content

  puts "\nMigration created: #{migration_path}"
  puts "\nNext steps:"
  puts "  1. bin/rails db:migrate"
  puts "  2. Add `multisearchable against: [:column1, :column2]` to your models"
  puts "  3. Run backfill for existing records: Model.knitsearch_multisearch_backfill!"
end

#verify_sqlite_adapterObject

Raises:

  • (::Thor::Error)


22
23
24
25
26
27
28
29
# File 'lib/generators/knitsearch/multisearch_install/multisearch_install_generator.rb', line 22

def verify_sqlite_adapter
  adapter = primary_adapter_from_configuration
  return if adapter == "sqlite3"

  raise ::Thor::Error,
        "knitsearch multi-model search requires SQLite. Detected adapter: #{adapter.inspect}. " \
        "Use a different full-text search solution for your database."
end

#verify_table_not_existsObject

Raises:

  • (::Thor::Error)


31
32
33
34
35
36
37
38
39
# File 'lib/generators/knitsearch/multisearch_install/multisearch_install_generator.rb', line 31

def verify_table_not_exists
  return if !table_exists?("knitsearches")

  return if options[:force]

  raise ::Thor::Error,
        "Table `knitsearches` already exists. " \
        "Run `bin/rails generate knitsearch:multisearch_install --force` to overwrite."
end