Skip to content

Commit

Permalink
Make template work for existing applications
Browse files Browse the repository at this point in the history
Not every RES adopter wants to generate new app from scratch. Some of
them potentially wants to put it in the existing application.

Template can be used in that manner:
  - cd to your rails app directory
  - bin/rails app:template LOCATION=https://railseventstore.org/new
  - that's it, RES is installed and preconfigured in existing app

run_after_bundle_callbacks had to be used explicitly since if the bundle
is present (it is in exising app), the after_bundle hook won't be run.

Conditional run_bundle is also necessary to install RES after adding it
to Gemfile in existing app, so we can generate the migration and execute
it.

Determining database kind based on options only makes sense for the new
app, since it lives in generator options (e.g. --database=postgresql),
but for the existing app, we need to figure it out based on ActiveRecord
connection settings. Those are available for the generator already.
Generator options will contain default value which is "sqlite3".

Co-authored-by: Łukasz Reszke <lukaszreszke93@gmail.com>
  • Loading branch information
fidel and lukaszreszke committed Jan 18, 2023
1 parent caee04c commit 3a19ac8
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions APP_TEMPLATE
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
class Profile
def initialize(options)
def initialize(options, location)
@options = options.dup
@location = location
end

private attr_reader :options
private attr_reader :options, :location

def client
postgres? ? "RailsEventStore::JSONClient.new" : "RailsEventStore::Client.new"
Expand All @@ -13,18 +14,22 @@ class Profile
postgres? ? "jsonb" : "binary"
end

def existing_app?
!!location
end

private

def postgres?
database.downcase.eql?("postgresql")
end

def database
options.fetch(:database)
existing_app? ? ActiveRecord::Base.connection_db_config.adapter : options.fetch(:database)
end
end

profile = Profile.new(options)
profile = Profile.new(options, ENV["LOCATION"])

gem "rails_event_store", "~> 2.8.1"

Expand Down Expand Up @@ -62,8 +67,12 @@ CODE

route "mount RailsEventStore::Browser => '/res' if Rails.env.development?"

run_bundle unless run "bundle check"

after_bundle do
rails_command 'db:create'
generate "rails_event_store_active_record:migration --data-type=#{profile.data_type}"
rails_command 'db:migrate'
end

run_after_bundle_callbacks if profile.existing_app?

0 comments on commit 3a19ac8

Please sign in to comment.