Require a gem in all migrations - ruby-on-rails

I have this gem in my gemfile (gem "lhm", "~> 2.2.0", require: false) that I want required in all migrations. Behavior similar to that of requiring spec_helper in all spec files.
I considered doing something with bin/rails or bin/rake but I don't want it required in all tasks, just migrations. Ie. rake db:migrate or the now alias rails db:migrate

As you probably don't have all the migrations using lhm I would suggest adding require "lhm" only to the migrations you're using. But to answer your question you could use rake's enhance method:
add the below to lib/tasks/something.rake:
namespace :load do
namespace :lhm do
desc "This just loads lhm"
task lhm: :environment do
require "lhm"
end
end
end
Rake::Task['db:migrate'].enhance(['load:lhm'])
As an example see here

Related

Don't know how to build task 'db:seed_fu'

I have just added the seed-fu gem to my app for seeding my test-database:
group :test do
gem 'seed-fu'
end
I made a custom rake task (in /lib/tasks/db.rake) for seeding only my test-database:
namespace :db do
desc "seed_fu only in test-database"
task seed_fu_test: :environment do
Rails.env = 'test'
puts "Seeding will be made in test-base ONLY!"
Rake::Task["db:seed_fu"].execute
end
end
If I do rake -T | grep seed then my new custom-made task is shown amongst other seed-tasks:
rake db:seed # Load the seed data from db/seeds.rb
rake db:seed_fu # Loads seed data for the current environment
rake db:seed_fu_test # seed_fu only in test-database
Now when I do rake db:seed_fu_test I get
rake aborted!
Don't know how to build task 'db:seed_fu'
But when I do
rake db:seed_fu RAILS_ENV='test'
then seed_fu seeds my test-database well.
Figured it out- the problem was in my Gemfile. Because I added the seed-fu gem into test-group then in development-environment, which was my default for running also the rake db:seed_fu_test task, the seed_fu gem was not seen.
Therefore when moving gem 'seed-fu' line into my :development-group in Gemfile, the problem was solved.

Heroku could not detect rake tasks (LoadError: cannot load such file -- rspec/core/rake_task)

I'm using travisCI to deploy to heroku and I am getting this error. It has only just started happening.
I have the basic rails Rakefile and I have a file that looks like this as otherwise travis cannot detect the rake tasks:
# lib\tasks\spec.rake
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new
task :default => :spec
Why would this error be displaying specifically for heroku?
EDIT - I had a similar version to the (better) answer given:
begin
require 'rspec/core/rake_task'
desc "Run all examples"
RSpec::Core::RakeTask.new(:spec) do |t|
t.rspec_opts = %w[--color]
t.pattern = 'spec/*_spec.rb'
end
rescue LoadError
end
If rspec isn't in the production group (it generally isn't) then the code you posted would fail when run in a production environment like heroku.
In the rspec docs they recommend doing this:
begin
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec)
rescue LoadError
end
So that the absence of rspec doesn't stop your rakefile loading.
FWIW, I just faced the same issue. I fixed it by moving rspec-rails to production as shown below. I don't know why it works, but it works for me.
group :production, :development, :test do
gem 'rspec-rails', '~> 3.8'
end

Including gems in a rake file

I'm trying to write a rakefile outside of a rails project.
I've created a new directory, added a rakefile and set a basic default task. It works.
I want to use the premailer gem so in my default task I've added this -
premailer = Premailer.new('http://localhost/email.html', :warn_level => Premailer::Warnings::SAFE)
This doesn't work, I get the following error.
uninitialized constant Object::Premailer
How do I refer to the premailer gem in my task? Should I be including it in a gemfile of sorts?
You can work without a Gemfile like this:
require 'rubygems' # only needed for ruby 1.8.7
require 'premailer'
desc "My Task"
task :my_task do
..
premailer = Premailer.new(...)
...
end
or with a Gemfile:
require 'rubygems' # only needed for ruby 1.8.7
require 'bundler'
Bundler.setup
Bundler.require
desc "My Task"
task :my_task do
..
premailer = Premailer.new(...)
...
end
I hope this helps.

rufus-scheduler fails with rake tasks (Don't know how to build task)

I'm using Rails3 (Windows, Ruby 1.8.7) with rufus-scheduler gem. Gem works fine, but if I'm trying to run some standard rake task, error occurs:
Don't know how to build task 'db:version' # ofc, db:version is just example
Terminal command
rake -T
works
If I'm trying to define own simple rake commands, they works fine too:
# /lib/my_scheduler.rb
require 'rubygems'
require 'rake'
require 'rufus/scheduler'
load File.join( Rails.root, 'lib', 'tasks', 'my_own_tasks.rake')
scheduler = Rufus::Scheduler.start_new
scheduler.every '5s' do
Rake::Task["my_own_namespace:test"].invoke
end
end
# /lib/tasks/my_own_tasks.rb
namespace :my_own_namespace do
task :test do
puts "Some scheduler task"
end
end
... but using standard rake tasks *in my_own_tasks* throws the same error.
Some help would be appreciated
PS. I'm newbie, so sorry, if that was dumb question
Maybe someone will need solution:
system("rake namespace:task")
f.e:
system("rake db:version")

Using Rake on a gem with dependencies

I have a gem that requires a 'Cms' namespace to be present when running.
However, when running rake tasks, nothing works as this Cms namespace isn't present. How do I get my rake tasks to work?
You can either, load your project source into the Rakefile (like Rails would do) or define a dummy module with the name Cms on your project.
# Rakefile
module Cms; end
task :my_task do
# ..
end
If you are on rails, and this gem is a dependency, you just have to make your task dependent of the :environment rails' task.
# some_task.rake
task :my_task => :environment do
# ..
end
Hope this helps.

Resources