Showing posts with label Gems. Show all posts
Showing posts with label Gems. Show all posts

Monday, April 13, 2015

How to Load Dependencies in a Rails Engine

In a Rails project, generally all you have to do is add a gem to your Gemfile and the dependencies will automagically get taken care of. Unfortunately, for Rails Engines this is not the case. You will have to take the additional step of requiring the gem's library file.

# engine.gemspec s.add_dependency 'jquery-rails' s.add_dependency 'your-foo-gem' # lib/your_engine/engine.rb
require 'jquery-rails'
require 'your-foo-gem'


This is related to the problem of needing to require modules from gems to get them to load properly.

Thursday, April 9, 2015

How to specify a local gem in your Gemfile

If you're developing a gem and you want to test it out locally in another project, this is easily accomplished by specifying the path to the local gem in your Gemfile:

gem 'bears', path: '/path/to/bears/gem'

and boom, it's there. One bundle install later and you'll be good to go.

Friday, January 16, 2015

Rake Database Tasks Outside Rails

My first content post is, ironically, not directly about Rails. It is about using a very common Rails feature - rake database tasks - in a Ruby project that utilizes ActiveRecord but not Rails.

By default, tasks like rake db:migrate are available in Rails but not in a Ruby project that includes ActiveRecord. Fortunately, with a couple of lines added to your Rakefile this issue can be fixed.
 // Rakefile
 DatabaseTasks.env = ENV['YOUR_ENV_VAR'] || 'development'  
 DatabaseTasks.db_dir = 'db'  
 DatabaseTasks.database_configuration = YAML.load_file('config/database.yml')  
 DatabaseTasks.migrations_paths = "#{DatabaseTasks.db_dir}/migrate"  
 task :environment do  
  ActiveRecord::Base.configurations = DatabaseTasks.database_configuration  
  ActiveRecord::Base.establish_connection DatabaseTasks.env.to_sym  
 end  
 load 'active_record/railties/databases.rake'  
And voila. Now, you can use create, migrate, and all of the other rake db tasks you're used to in a Rails project.