Class: IprogOtpGenerator::Generators::InstallGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/install_generator.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.next_migration_number(dirname) ⇒ Object

This method is required by Rails generators to create a unique timestamp for migration files



39
40
41
42
43
44
45
# File 'lib/generators/install_generator.rb', line 39

def self.next_migration_number(dirname)
  if ActiveRecord::Base.timestamped_migrations
    Time.now.utc.strftime("%Y%m%d%H%M%S")
  else
    "%.3d" % (current_migration_number(dirname) + 1)
  end
end

Instance Method Details

#copy_migrationObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/generators/install_generator.rb', line 13

def copy_migration
  # Define table name based on the model name
  table_name = model_name.tableize

  # Generate the migration file name
  timestamp = Time.now.utc.strftime("%Y%m%d%H%M%S")
  migration_filename = "db/migrate/#{timestamp}_add_otp_fields_to_#{table_name}.rb"

  # Define the content of the migration file as a string
  migration_content = "      class AddOtpFieldsTo\#{table_name.camelize} < ActiveRecord::Migration\#{migration_version}\n        def change\n          add_column :\#{table_name}, :otp_code, :string\n          add_column :\#{table_name}, :otp_code_expires_at, :datetime\n          add_column :\#{table_name}, :otp_code_resend_interval, :integer, default: 60\n          add_column :\#{table_name}, :otp_code_confirmed, :boolean, default: false\n        end\n      end\n   RUBY\n\n  # Create the migration file manually\n  create_file migration_filename, migration_content\nend\n"