Ruby on Rails full application template - ruby-on-rails

I would like to create Rails application template, so when I run rails new it should:
override Gemfile (add gems, remove gems and those shitty comments etc.)
convert application layout file to HAML and edit its content
edit assets files to import bootstrap-sass files as required
edit and set my database variables in database.yml
maybe something else, dunno atm...
I just want it to be ready to go, I hope you can understand me so far.
How could I achieve this?
I know about rails composer, but I don't want to choose from these application templates, I want the one with constant set of tools I always use. I can commit new application to GitHub and then just run git clone on it each time with just changing my new project name, but is it the best solution?

You can use the awesome Rails feature Rails Application Templates, rails new project_name -m /path/to/template.rb
Here is my own template:
# minitest, capistrano, pry
gem_group :development do
gem 'puma'
gem 'capistrano', '2.15.5'
gem 'rvm-capistrano'
gem 'pry-rails', '~> 0.3.2'
end
gem_group :test do
gem 'minitest-rails'
gem 'letter_opener'
gem 'ffaker', require: false
end
run "bundle install"
#install minitest test_helper
generate 'mini_test:install'
environment "config.generators do |g|\n g.test_framework :mini_test, spec: true, fixture: true\n end"
#Add pride to minitest config
run "sed -i '' '4 s#^#require \"minitest/pride\"#' test/test_helper.rb"
#create postgres DB in postgresql
#development and test
run "psql -c 'CREATE DATABASE #{app_path}_development;'"
run "psql -c 'CREATE DATABASE #{app_path}_test;'"
#add database yml
run "sed -i '' '4 s#^#require \"minitest/pride\"#' test/test_helper.rb"
#Add minitest features to Rake task
run %q^echo 'MiniTest::Rails::Testing.default_tasks << "features"' >> Rakefile^
#Fix README.md
run "rm README.rdoc"
run "touch README.md"
#Initialize local Git repository and Initial Commit
git :init
git add: "."
git commit: "-a -m 'Initial commit :pray:'"

Related

Run `rails db:migrate` with capistrano in a gem/submodule

I have a rails 5.2 app. I am trying to deploy it using Capistrano.
The app has a gem dependency submodule, which contains all of the models and migrations needed for this project. This submodule depends on other gems in it's .gemspec.
Therefore, I need to run rails db:migrate in the submodule root, instead of the parent project root.
I have added the following to deploy.rb:
desc 'Runs rake db:migrate if migrations are set'
task :migrate => [:set_rails_env] do
on primary fetch(:migration_role) do
within "#{release_path}/PATH/TO/SUBMODULE" do
with rails_env: fetch(:rails_env) do
execute :rake, "db:migrate"
end
end
end
end
before :starting, :migrate
Gemfile:
gem 'dependency', path: 'PATH/TO/SUBMODULE'
gem 'capistrano-git-with-submodules', '~> 2.0'
group :development do
gem 'capistrano', require: false
gem 'capistrano-rvm', require: false
gem 'capistrano-rails', require: false
gem 'capistrano-bundler', require: false
gem 'capistrano3-puma', require: false
end
When I try to run this task, I get an error message saying:
DEBUG [b4c1cf18] [31mCould not find aasm-5.0.2 in any of the sources[0m
DEBUG [b4c1cf18]
DEBUG [b4c1cf18] [33mRun `bundle install` to install missing gems.[0m
DEBUG [b4c1cf18]
It seems like the gem set (containing all parent and submodule gems) used in deploying the parent project isn't in the path or is unavailable when it comes time to run this task.
I can get the parent project running without the migrations, so I know that the parent project is loading all the correct gem set at runtime. Just not during this task.
I'm not quite sure how Capistrano works under the hood, how could I make sure that these gems are available to this task when it runs?
I figured it out, I needed 3 things:
1.execute :bundle before execute :rake, :"db:migrate"
Provide a second argument to within "#{release_path}/PATH/TO/SUBMODULE" to make it within "#{release_path}/PATH/TO/SUBMODULE", release_path - couldn't find docs on this, only this PR: https://github.com/capistrano/bundler/pull/84
symlink secrets to the engine so it doesn't prevent any tasks from running:
task :symlink_secrets do
on roles(:app) do
execute "rm -rf #{release_path}/PATH/TO/SUBMODULE/spec/dummy/config/secrets.yml"
execute "ln -nfs ~/secrets.yml #{release_path}/PATH/TO/SUBMODULE/spec/dummy/config/secrets.yml"
end
end

rails 4.2.0 with whenever gem in development env, not working

Hi everyone I have such problem:
I'm using rails 4.2.0 with rvm gemsets (using right ruby version and correct gemset with installed gems).
I'm trying to make some cron task using whenever gem, but I can't make it work.
This is what I have:
Gemfile
gem 'whenever', :require => false
Model
class SomeModel < ActiveRecord::Base
end
schedule.rb
set :environment, :development
every 1.minute do
runner "SomeModel.create"
end
after using
whenever --update-crontab store
my Crontab
# Begin Whenever generated tasks for: /home/user/project/config/schedule.rb
* * * * * /bin/bash -l -c 'cd /home/user/project && bin/rails runner -e development '\''SomeModel.create'\'''
# End Whenever generated tasks for: /home/user/project/config/schedule.rb
In cron.log I see that task was executed, but I don't see any new objects in db(I'm using mysql).
So I try create new project with model. I use mysql-lite and add whenever gem with same type of task. Then I updated crontab and whenever start to work fine, until I use mysql instead mysql-lite.
I don't find any troubleshooting using mysql and whenever in whenever docs, so I stuck with this (using console I can create object without problem).
Using output in schedule help me to find a problem.
I used specific gemset and not specify it in my schedule.
That's why I got error. Whenever gem trying to use default gemset which was different from gemset that I use for my project.
So there are two options for solving problem, use default gemset or specify yours in schedule.
Sample of setting gemset and env:
set :environment, :development
Rake:
job_type :rake, "cd :path && $HOME/.rvm/scripts/rvm && rvm use
2.2.0#somename && rake ':task' :output"
Runner:
job_type :runner, "cd :path && $HOME/.rvm/scripts/rvm && rvm use 2.2.0#somename && bin/rails runner ':task' :output"

Capistrano task git clone private gem

I am attempting to write a Capistrano task that will 'deploy' a private gem I wrote to accompany a Rails project.
In config/deploy.rb
after :updating, :retrieve_my_gem
The file lib/capistrano/tasks/retrieve_my_gem.rake contains...
desc 'Clones my_gem from Github to vendor/git'
task :retrieve_my_gem do
on roles(:app), in: :sequence do
# Create the directory that will contain my gem
gem_container_path = release_path.join('vendor/git')
debug "Gem container path: #{gem_container_path}"
if test "[ ! -d #{gem_container_path} ]"
info "Creating local gem directory"
execute 'mkdir', '-p', gem_container_path
end
gem_path = release_path.join('vendor/git/my_gem')
debug "Gem path: #{gem_path}"
if test "[ ! -d #{gem_path} ]"
within gem_container_path do
info "Cloning my gem to #{gem_path}"
execute 'git clone git#github.com:username/my_gem.git', gem_path
end
else
within gem_path do
info "Updating my gem in #{gem_path}"
execute 'git pull'
end
end
end
end
And Gemfile has...
gem 'my_gem', :path => 'vendor/git/my_gem/'
When I run my deploy task, the gem_container_path is created but it clones the repo my_gem into ~/my_gem instead of into gem_container_path. The deploy task continues and fails when attempt bundle install due to not finding 'my_gem' in 'vendor/git/my_gem/' as indicated in the Gemfile.
I am expecting the task to clone the repo to /var/www/project/releases/*/vendor/git/my_gem where * is replaced with the DateTime stamp that Capistrano generates for this release.
Why is Capistrano cloning the repo to my remote user's home directory and not to the specified path?
The issue was my execute statement:
execute 'git clone git#github.com:username/my_gem.git', gem_path
I changed it to
execute "git clone git#github.com:username/my_gem.git #{gem_path}"
and it is working as expected now.

capistrano script fails because a test-group gem is not found

I'm deploying to a staging server which is running in environment=development. My deploy script fails with the message Could not find gem 'webmock (>= 0) ruby' in the gems available on this machine. but I cannot see why the script should ever need that gem because my Gemfile only lists it in the test group.
Do you have any idea why my staging server is trying to require webmock during deployment?
My deploy script
Here is the command from my deploy script fails:
executing ["cd /var/www/clu2/staging/releases/20130429170940 && bundle exec whenever --update-crontab staging --set environment=development --roles db"]
One command earlier, the script runs bundle install, omitting the test gems:
executing ["cd /var/www/clu2/staging/current && bundle install --without=test --no-update-sources"]
My usage of webmock is only in test
You can see that webmock is only required in my spec_helper file:
$ grep -r webmock .
./Gemfile: gem "webmock"
./Gemfile.lock: webmock (1.11.0)
./Gemfile.lock: webmock
./spec/spec_helper.rb: require "webmock/rspec"
You can see that webmock is specified as a test gem in my Gemfile:
group :test do
gem 'spork-rails' # pre-load rails environment for faster test launches
gem "webmock"
end
You should explicitly set :bundle_without config option in your staging deployment configuration file (do you use it? If not take a look at capistrano-ext gem which provides multistage features):
set :bundle_without, []
That's because :bundle_without is defined like this in production deployment:
set :bundle_without, [ :development, :test ]
One more thing.
I think that you should run your stage machine with production env. Staging machine is commonly used to emulate how code is working in production.

How to deploy a custom Gem using capistrano?

I'm new to RoR. My gem does some background processing and loads a thin server so it can be checked from time to time. I need to put this on my web server using Capistrano.
Do people normally deploy gems to their servers or should the app be written in some other way?
Also is Capistrano the correct way to deploy gems?
Have you checked out Bundler before? That works very well with capistrano deployments and you can use to package your gems with your app on deployments.
Or you can use the gem Jeweler : https://github.com/technicalpickles/jeweler
I found out deploying a custom Gem is no different to any other project type. I just needed to add a new Capistrano task to my deploy.rb file so that the gem could get installed after the files get downloaded onto the server by Capsitrano. This is all I had to do.
desc "Install this gem"
task :setup_install, roles: :app do
run "cd #{release_path} && gem build zoe.gemspec"
run "cd #{release_path} && gem install YOUR-GEM-NAME.gem --quiet"
end
after "deploy:finalize_update", "deploy:setup_install"
desc "Uninstall this gem"
task :setup_uninstall, roles: :app do
run "gem uninstall -x YOUR-GEM-NAME"
end
before "deploy:setup_install", "deploy:setup_uninstall"

Resources