Skip to content

Instantly share code, notes, and snippets.

@matuszewskijan
Last active March 13, 2022 21:18
Show Gist options
  • Select an option

  • Save matuszewskijan/41037f5b49ae6a3c4d711df78cb827b7 to your computer and use it in GitHub Desktop.

Select an option

Save matuszewskijan/41037f5b49ae6a3c4d711df78cb827b7 to your computer and use it in GitHub Desktop.
Rails Rspec Sidekiq Postgres FactoryBot Template
run "spring stop"
gem_group :development, :test do
gem "rspec-rails"
end
run "bundle install"
rails_command "generate rspec:install"
run "rm -rf test" if yes?("Do you want to remove the /test directory?")
# Install FactoryBot
File.open("spec/rails_helper.rb", "r+") do |file|
lines = file.each_line.to_a
config_index = lines.find_index("RSpec.configure do |config|\n")
lines.insert(config_index + 1, " config.include FactoryBot::Syntax::Methods\n")
file.rewind
file.write(lines.join)
end
# Apply only for existing applications
if yes?("Would you like to generate factories for your existing models?")
Dir.glob("./app/models/*.rb").each { |file| require file }
models = ApplicationRecord.send(:subclasses).map(&:name)
models.each do |model|
run("rails generate factory_bot:model #{model} #{model.constantize.columns.map { |column| "#{column.name}:#{column.type}" }.join(" ")}")
end
end
# Use Postgres
`which postgres`
isPostgresInstalled = $?
if not isPostgresInstalled.success?
puts "Postgres doesn't appear to be installed. Please install before continuing!"
exit
end
gem 'pg'
gsub_file 'Gemfile', /gem 'sqlite3'.*/, ''
run "bundle install"
initializer 'generator.rb' do <<~RB
Rails.application.config.generators do |g|
g.orm :active_record, primary_key_type: :uuid
end
RB
end
remove_file 'config/database.yml'
create_file 'config/database.yml' do <<~EOF
default: &default
adapter: postgresql
encoding: unicode
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
development:
<<: *default
database: #{app_name}_development
username: #{app_name}
password:
host: localhost
port: 5432
test:
<<: *default
database: #{app_name}_test
username: #{app_name}
password: #{app_name}
host: localhost
port: 5432
production:
<<: *default
url: <%= ENV['DATABASE_URL'] %>
EOF
end
generate 'migration', 'enable_uuid'
migration = Dir.glob("db/migrate/*").max_by { |f| File.mtime(f) }
inject_into_file migration, after: 'def change' do <<~RB
enable_extension 'pgcrypto'
RB
end
# Install Sidekiq
run "bundle add sidekiq"
environment = ask("Which environment do you want to configure sidekiq with active jobs?\n",:blue)
environment "config.active_job.queue_adapter = :sidekiq", env: environment
say("Sidekiq Successfully Installed", :blue)
@matuszewskijan
Copy link
Author

matuszewskijan commented Mar 13, 2022

Create the app with the raw content URL:
rails new appname -m https://gist.githubusercontent.com/matuszewskijan/41037f5b49ae6a3c4d711df78cb827b7/raw/f56e1b4bf6677f0c95e9e76c44bb20305f56443e/template.rb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment