Class: Para::OrderableGenerator

Inherits:
Generators::NamedBase show all
Includes:
Rails::Generators::Migration
Defined in:
lib/generators/para/orderable/orderable_generator.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Generators::NameHelpers

#namespaced_class_name, #plural_namespaced_path, #singular_namespaced_path

Class Method Details

.next_migration_number(dir) ⇒ Object



41
42
43
# File 'lib/generators/para/orderable/orderable_generator.rb', line 41

def self.next_migration_number(dir)
  Time.now.utc.strftime("%Y%m%d%H%M%S")
end

Instance Method Details

#add_field_to_modelObject



15
16
17
18
19
20
# File 'lib/generators/para/orderable/orderable_generator.rb', line 15

def add_field_to_model
  migration_template(
    'orderable_migration.rb',
    "db/migrate/add_orderable_position_to_#{ table_name }.rb"
  )
end

#add_orderable_to_modelObject



22
23
24
25
26
27
28
# File 'lib/generators/para/orderable/orderable_generator.rb', line 22

def add_orderable_to_model
  class_definition = /class #{ class_name } < (ActiveRecord::Base|ApplicationRecord)\n/

  inject_into_file "app/models/#{ singular_namespaced_path }.rb", after: class_definition do
    "  acts_as_orderable\n"
  end
end

#fianl_messageObject



34
35
36
37
38
39
# File 'lib/generators/para/orderable/orderable_generator.rb', line 34

def fianl_message
  message = "The #{ class_name } model is now orderable.\n"
  message << "Please migrate to update your model's table\n" unless options[:migrate]

  say(message)
end

#migrateObject



30
31
32
# File 'lib/generators/para/orderable/orderable_generator.rb', line 30

def migrate
  rake 'db:migrate' if options[:migrate]
end

#welcomeObject



11
12
13
# File 'lib/generators/para/orderable/orderable_generator.rb', line 11

def welcome
  say "Making #{ class_name } model orderable ..."
end