Sha256: f5be66f5711d2cf9f68e9e827a0d3dc993757d71c72269d57065c5d54579c924

Contents?: true

Size: 1.54 KB

Versions: 20

Compression:

Stored size: 1.54 KB

Contents

class DatabasesController < OnboardingController

  helper_method :user, :password, :database

  def index; end;

  def create
    info ||= {
      'adapter' => 'postgresql',
      'database' => database,
      'user' => user,
      'password' => password,
      'encoding' => 'utf8'
    }
    begin
      ActiveRecord::Base.configurations = {
        'development' => info,
        'test' => info,
        'production' => info
      }
      ActiveRecord::Base.establish_connection
      ActiveRecord::Base.connection
      migrate!
    rescue Exception => e
      @exception = e
      ActiveRecord::Base.clear_all_connections!
      render 'index' and return
    end

    redirect_to '/users'
  end

  protected

  private

  def migrate!
    ActiveRecord::Tasks::DatabaseTasks.database_configuration = ActiveRecord::Base.configurations
    ActiveRecord::Migrator.migrations_paths = ActiveRecord::Tasks::DatabaseTasks.migrations_paths
    ActiveRecord::Migration.verbose = ENV["VERBOSE"] ? ENV["VERBOSE"] == "true" : true
    ActiveRecord::Migrator.migrate(ActiveRecord::Migrator.migrations_paths, ENV["VERSION"] ? ENV["VERSION"].to_i : nil) do |migration|
      ENV["SCOPE"].blank? || (ENV["SCOPE"] == migration.scope)
    end
  end

  def db_params
    params.fetch(:database, {})
  end

  def user
    @user ||= "ecrire#{SecureRandom.hex(2)}"
    db_params.fetch(:user, @user)
  end

  def password
    @password ||= SecureRandom.hex(16)
    db_params.fetch(:password, @password)
  end

  def database
    @database ||= "ecrire"
    db_params.fetch(:name, @database)
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
ecrire-0.31.2 lib/ecrire/onboarding/controllers/databases_controller.rb
ecrire-0.31.1 lib/ecrire/onboarding/controllers/databases_controller.rb
ecrire-0.31.0 lib/ecrire/onboarding/controllers/databases_controller.rb
ecrire-0.30.3 lib/ecrire/onboarding/controllers/databases_controller.rb
ecrire-0.30.2 lib/ecrire/onboarding/controllers/databases_controller.rb
ecrire-0.30.1 lib/ecrire/onboarding/controllers/databases_controller.rb
ecrire-0.30.0 lib/ecrire/onboarding/controllers/databases_controller.rb
ecrire-0.29.0 lib/ecrire/onboarding/controllers/databases_controller.rb
ecrire-0.28.3 lib/ecrire/onboarding/controllers/databases_controller.rb
ecrire-0.28.2 lib/ecrire/onboarding/controllers/databases_controller.rb
ecrire-0.28.1 lib/ecrire/onboarding/controllers/databases_controller.rb
ecrire-0.28.0 lib/ecrire/onboarding/controllers/databases_controller.rb
ecrire-0.27.0 lib/ecrire/onboarding/controllers/databases_controller.rb
ecrire-0.26.3 lib/ecrire/onboarding/controllers/databases_controller.rb
ecrire-0.26.2 lib/ecrire/onboarding/controllers/databases_controller.rb
ecrire-0.26.1 lib/ecrire/onboarding/controllers/databases_controller.rb
ecrire-0.26.0 lib/ecrire/onboarding/controllers/databases_controller.rb
ecrire-0.25.2 lib/ecrire/onboarding/controllers/databases_controller.rb
ecrire-0.25.1 lib/ecrire/onboarding/controllers/databases_controller.rb
ecrire-0.25.0 lib/ecrire/onboarding/controllers/databases_controller.rb