class Trestle::Auth::Otp::Generators::InstallGenerator

Public Class Methods

next_migration_number(dirname) click to toggle source
# File lib/generators/trestle/auth/otp/install/install_generator.rb, line 51
def self.next_migration_number(dirname)
  next_migration_number = current_migration_number(dirname) + 1
  ActiveRecord::Migration.next_migration_number(next_migration_number)
end

Public Instance Methods

check_trestle_installed() click to toggle source
# File lib/generators/trestle/auth/otp/install/install_generator.rb, line 17
def check_trestle_installed
  unless ::File.exist?("config/initializers/trestle.rb")
    raise Thor::Error, "The file config/initializers/trestle.rb does not appear to exist. Please run `trestle:install` first."
  end
end
devise?() click to toggle source
# File lib/generators/trestle/auth/otp/install/install_generator.rb, line 47
def devise?
  options[:devise]
end
generate_admin() click to toggle source
# File lib/generators/trestle/auth/otp/install/install_generator.rb, line 33
def generate_admin
  generate "trestle:auth:otp:admin", model, ("--devise" if devise?)
end
generate_migration() click to toggle source
# File lib/generators/trestle/auth/otp/install/install_generator.rb, line 37
def generate_migration
  filename = 'migration.rb'
  path = File.expand_path(find_in_source_paths(filename.to_s))
  text = File.read(path)
  content = text.gsub('[]', "[#{ActiveRecord::Migration.current_version}]")
  File.open(path, "w") { |file| file << content }

  migration_template(filename, "db/migrate/add_otp_fields.rb")
end
generate_model() click to toggle source
# File lib/generators/trestle/auth/otp/install/install_generator.rb, line 29
def generate_model
  generate "trestle:auth:otp:model", model unless devise?
end
insert_configuration() click to toggle source
# File lib/generators/trestle/auth/otp/install/install_generator.rb, line 23
def insert_configuration
  inject_into_file "config/initializers/trestle.rb", before: /^end/ do
    format_configuration(template_content("basic.rb.erb"))
  end
end

Private Instance Methods

capturable_erb(path) click to toggle source
# File lib/generators/trestle/auth/otp/install/install_generator.rb, line 70
def capturable_erb(path)
  match = ERB.version.match(/(\d+\.\d+\.\d+)/)

  if match && match[1] >= "2.2.0" # Ruby 2.6+
    CapturableERB.new(::File.binread(path), trim_mode: "-", eoutvar: "@output_buffer")
  else
    CapturableERB.new(::File.binread(path), nil, "-", "@output_buffer")
  end
end
format_configuration(source) click to toggle source
# File lib/generators/trestle/auth/otp/install/install_generator.rb, line 57
def format_configuration(source)
  "\n#{source.indent(2)}\n"
end
template_content(path, options={}) click to toggle source
# File lib/generators/trestle/auth/otp/install/install_generator.rb, line 61
def template_content(path, options={})
  path = File.expand_path(find_in_source_paths(path.to_s))
  context = options.delete(:context) || instance_eval("binding")

  content = capturable_erb(path).tap do |erb|
    erb.filename = path
  end.result(context)
end