Skip to content

Instantly share code, notes, and snippets.

@GuilleLeopold
GuilleLeopold / config_observers.rb
Created March 11, 2020 19:22
config observers
config.active_record.observers = :'payment/user_customer_observer'
@GuilleLeopold
GuilleLeopold / observers_disable.rb
Created March 11, 2020 18:55
disable observers
ActiveRecord::Base.observers.disable :all
module Payment
class UserCustomerObserver < ActiveRecord::Observer
observe User::Customer
attr_reader :customer
def after_update(customer)
customer.update!(payments: customer.payments + 1)
end
end
end
@GuilleLeopold
GuilleLeopold / generate_engines.rb
Created March 11, 2020 18:51
generate_engines
task :generate_engine do
# Get name sent from console
name = ENV['name'].downcase
# Store useful paths
engine_path = "engines/#{name}"
dummy_path = 'spec/dummy'
lib_files_path = 'lib/tasks/files'
dummy_relative_path = "#{engine_path}/#{dummy_path}"
Dir.glob(File.expand_path('../engines/*', __FILE__)).each do |path|
engine = File.basename(path)
gem engine, path: "engines/#{engine}", require: (ENV['ENGINE'].nil? || ENV['ENGINE'] == engine)
end
Dir.glob(File.expand_path('../engines/*', __dir__)).each do |path|
engine = File.basename(path)
mount engine.classify.constantize::Engine, at: '/'
end
@GuilleLeopold
GuilleLeopold / engine_migrations.rb
Created February 3, 2020 20:18
Code block to run migrations from each engine
initializer :append_migrations do |app|
unless app.root.to_s.match? root.to_s
config.paths['db/migrate'].expanded.each do |expanded_path|
app.config.paths['db/migrate'] << expanded_path
end
end
end