Using Rake on a gem with dependencies - ruby-on-rails

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.

Related

How do you include tests from a lib directory in Rails rake tasks

How do you include tests that are in a lib folder in your rake tasks?
For example, you have a foo library you are building in the /lib/foo directory of your Rails project and would like to keep all of your foo tests in the lib/foo/tests directory.
It took me a while to consolidate from all the different sources, so I wanted to post here for anyone looking!
This imports the rake file from my lib/foo/test directory:
# Rakefile:
Dir.glob('lib/foo/tasks/*.rake').each { |r| load r}
This adds the test:foo_tests task to my rake tasks:
# lib/foo/tasks/test.rake
require "rake/testtask"
namespace :test do
Rake::TestTask.new(foo_tests: 'test:prepare') do |t|
t.pattern = 'lib/foo/test/**/*_test.rb'
end
end
I hope this helps someone else!

Require a gem in all migrations

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

rake task not found when create rails plugin

I'm trying to create a gem plugin with a rake task. I just commented out pre-generated file in tasks folder but when I run rake my_plugin, error came out that
Don't know how to build task...
here is tasks/my_plugin.rake file
desc "Explaining what the task does"
task :my_plugin do
# Task goes here
end
You run rake myplugin, but define task :my_plugin. You miss the underscore.
You need to load the custom tasks in your plugin's Railtie:
module MyPlugin
class Railtie < ::Rails::Railtie
rake_tasks do
load 'tasks/my_plugin.rake'
end
end
end
https://api.rubyonrails.org/classes/Rails/Railtie.html

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")

Rails plugin require issues

I have a problem creating a Rails plugin, lets call it Mplug. The plugin is pretty much only a rake task, but with a library that the rake task uses.
The problem is to require files. Lets say that this is the rake task:
namespace :mplug do
task :create do
Mplug::Indexer.new
end
end
This will not recognize the constant Mplug. So I thought I needed to require it.
require 'mplug'
namespace :mplug do
task :create do
Mplug::Indexer.new
end
end
But then I get this message.
no such file to load -- mplug
So, ok. Lets try to give the path to the plugin then.
require 'vendor/plugins/mplug/lib/mplug'
namespace :mplug do
task :create do
Mplug::Indexer.new
end
end
This actually works. However, except that I guess that this is a bad way to do it, I now have to require the files in my plugin as if I was in the rails root. For example:
module Mplug
end
require 'mplug/indexer'
Now has to be:
module Mplug
end
require 'vendor/plugins/mplug/lib/mplug/indexer'
Which I do not want to do of course.
Is there any neat way to solve this?
Thanks!
The easiest solution to this problem would be to register the rake task using the Rails::Railtie API. In lib/mplug.rb, define your Railtie:
module Mplug
class Railtie < ::Rails::Railtie
rake_tasks do
load "mplug/rails.rake"
end
end
end
Then, in lib/mplug/rails.rake:
namespace :mplug do
task :create do
Mplug::Indexer.new
end
end
Then, make sure your plugin is defined in your Gemfile. If your plugin is in vendor/plugins, add this line to your Gemfile:
gem "mplug", :path => "vendor/plugins/mplug"
If you push the plugin to a git repo, use :git.
Now, rake mplug:create will be available! If you want it to show up in rake -T, make sure you add a description:
namespace :mplug do
desc "creating an mplug"
task :create do
Mplug::Indexer.new
end
end
One option is to use the FILE constant, and then provide the rest of the path relative to the current file:
require File.join(File.expand_path(File.dirname(__FILE__)), '..', 'lib', 'mplug')
(if your rake task file is in your plugin_root/tasks...)

Resources