From 2491bca364b927b77623feb51e39be7a567a0919 Mon Sep 17 00:00:00 2001 From: Elia Schito Date: Fri, 31 Jan 2020 23:23:47 +0100 Subject: [PATCH] Make the dummy app available to bin/console Usage example: $ bin/console USAGE: dummy_app # Load the dummy app and return the Rails.application instance dummy_app.initialize! # Initialize the dummy app dummy_app.migrate! # Run all the dummy app migrations >> dummy_app Loading the dummy app...done. => # >> dummy_app.initialize! => # >> dummy_app.migrate! == 20100107141738 AddApiKeyToSpreeUsers: migrating ============================ -- add_column(:spree_users, :api_key, :string, {:limit=>40}) -> 0.0019s == 20100107141738 AddApiKeyToSpreeUsers: migrated (0.0020s) =================== ... == 20190220093635 DropSpreeStoreCreditUpdateReasons: migrating ================ -- table_exists?(:spree_store_credit_update_reasons) -> 0.0003s -- drop_table(:spree_store_credit_update_reasons) -> 0.0002s -- column_exists?(:spree_store_credit_events, :update_reason_id) -> 0.0004s -- remove_column(:spree_store_credit_events, :update_reason_id) -> 0.0075s == 20190220093635 DropSpreeStoreCreditUpdateReasons: migrated (0.0084s) ======= => # >> --- bin/console | 46 +++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 41 insertions(+), 5 deletions(-) diff --git a/bin/console b/bin/console index 1356d4a3fa7..cd3b479392b 100755 --- a/bin/console +++ b/bin/console @@ -4,12 +4,48 @@ require "bundler/setup" require "solidus" -# You can add fixtures and/or initialization code here to make experimenting -# with your gem easier. You can also use a different console, if you like. +module IRBSolidusHelpers + def dummy_app + @dummy_app ||= begin + print "Loading the dummy app..." + ENV['DATABASE_URL'] ||= 'sqlite3::memory:?pool=1' -# (If you use this, don't forget to add pry to your Gemfile!) -# require "pry" -# Pry.start + require "spree/testing_support/dummy_app" + + class << (Rails.application) + # Allow to run migrations from the console + def migrate! + ActiveRecord::Migrator.migrations_paths = + Rails.application.migration_railties.flat_map do |engine| + (engine.paths['db/migrate'] if engine.respond_to?(:paths)).to_a + end + ActiveRecord::Migration.verbose = false + ActiveRecord::Base.connection.migration_context.run(:up, '20160101010000_solidus_one_four'.to_i) + ActiveRecord::Tasks::DatabaseTasks.migrate + self + end + + # Avoid flooding the console with the original #inspect + def inspect + "#<#{self.class.name} root=#{root}>" + end + end + + puts "done." + Rails.application + end + end + + USAGE = <<~TXT + USAGE: + dummy_app # Load the dummy app and return the Rails.application instance + dummy_app.initialize! # Initialize the dummy app + dummy_app.migrate! # Run all the dummy app migrations + TXT +end + +puts IRBSolidusHelpers::USAGE require "irb" +IRB::ExtendCommandBundle.prepend IRBSolidusHelpers IRB.start(__FILE__)