Skip to content

Instantly share code, notes, and snippets.

@nejdetkadir
Last active January 28, 2022 09:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nejdetkadir/4f2219dc9a6024907aefe31dcd724885 to your computer and use it in GitHub Desktop.
Save nejdetkadir/4f2219dc9a6024907aefe31dcd724885 to your computer and use it in GitHub Desktop.
require "fileutils"
# Copied from: https://github.com/mattbrictson/rails-template
# Add this template directory to source_paths so that Thor actions like
# copy_file and template resolve against our source files. If this file was
# invoked remotely via HTTP, that means the files are not present locally.
# In that case, use `git clone` to download them to a local temporary dir.
def add_template_repository_to_source_path
if __FILE__ =~ %r{\Ahttps?://}
require "tmpdir"
source_paths.unshift(tempdir = Dir.mktmpdir("cousins-factory-"))
at_exit { FileUtils.remove_entry(tempdir) }
git clone: [
"--quiet",
"https://github.com/cousins-factory/rails-boilerplate.git",
tempdir
].map(&:shellescape).join(" ")
if (branch = __FILE__[%r{cousins-factory/(.+)/template.rb}, 1])
Dir.chdir(tempdir) { git checkout: branch }
end
else
source_paths.unshift(File.dirname(__FILE__))
end
end
def copy_templates
end
def add_default_gems
end
def add_optional_gems
add_devise_gem if yes?("Do you want to use Devise? [Y/N]")
end
def rails_7_or_newer?
Gem::Requirement.new('>= 7.0.0.alpha').satisfied_by? Gem::Version.new(Rails::VERSION::STRING)
end
def do_bundle
# Custom bundle command ensures dependencies are correctly installed
Bundler.with_unbundled_env { run 'bundle install' }
end
def add_devise_gem
append_to_file 'Gemfile' do <<~EOF
# Flexible authentication solution for Rails with Warden [https://github.com/heartcombo/devise/]
gem 'devise', '~> 4.8', '>= 4.8.1'
EOF
end
do_bundle
generate 'devise:install'
gsub_file "config/initializers/devise.rb", " # config.navigational_formats = ['*/*', :html]", " config.navigational_formats = ['*/*', :html, :turbo_stream]"
inject_into_file 'config/initializers/devise.rb', after: "# frozen_string_literal: true\n" do <<~EOF
class TurboFailureApp < Devise::FailureApp
def respond
if request_format == :turbo_stream
redirect
else
super
end
end
def skip_format?
%w(html turbo_stream */*).include? request_format.to_s
end
end
EOF
end
inject_into_file 'config/initializers/devise.rb', after: "# ==> Warden configuration\n" do
<<-EOF
config.warden do |manager|
manager.failure_app = TurboFailureApp
end
EOF
end
inject_into_file 'config/initializers/devise.rb', after: "# config.parent_controller = 'DeviseController'\n" do
<<-EOF
config.parent_controller = 'Users::DeviseController'
EOF
end
inject_into_file 'app/views/layouts/application.html.erb', after: " <body>\n" do <<-EOF
<p class="notice"><%= notice %></p>
<p class="alert"><%= alert %></p>
EOF
end
environment "config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }", env: 'development'
generate 'devise', 'User'
generate 'devise:views'
copy_file 'app/controllers/users/devise_controller.rb', 'app/controllers/users/devise_controller.rb', force: true
say 'You must define root url in config/routes.rb for devise gem', :yellow
say 'Devise successfully installed!', :green
end
def apply_template!
# Check rails version for installing
return say('Please use Rails 7.0 or newer to use rails boilerplate', :red) unless rails_7_or_newer?
add_template_repository_to_source_path
add_default_gems
add_optional_gems
do_bundle
copy_templates
# After the installation
say "\nRails boilerplate successfully installed!", :green
say "Please run 'rails db:create' and 'rails db:migrate' to create database and migrate data", :yellow
say "Please run 'rails server' to start the server", :green
end
apply_template!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment