6

I have a rails application using rake db:seed too fill in some of the basics in my DB to start working. This works through a little infrastructure and a fixtures.rb someone else wrote to load a YAML into the DB.
What I'm trying to do is take this infrastructure and move it into my own gem so that I can use it again elsewhere, this requires me both to have some of my own gem's models be inserted through the gem's seed task (which I want to have run from calling db:seed or something similar in the main app) and that the main app push some of the gem's models using it's own seed. The second part of this I already have working, it was a simple fix in the fixtures.rb file I was given. The things I want to do now:

  • Move fixtures.rb into the gem: I still don't have any of the source running this in the gem. Now to do this I can probably require the file from the [MyGem::Engine.root, 'lib', ...].join then call a method there with a path to load YAML files from into the DB, which I don't see why it shouldn't work.
  • Get rake db:seed to run a task defined in my gem. I've added .rake files under lib/tasks of my gem (it's an engine) and I can't seem to call them from the main app rakefile though.
    To make myself clear, what I want to do is through the gem (not the main app - or with 1 line of code in the main app) add a dependency onto the main apps seed task, so that when someone runs rake db:seed in the main app the gem will run additional seeding without the main app developer even having to know about them.

The dirty solution that I want to avoid is loading the .rake files from the gem inside the main app, or loading a seeds.rb in the gem from the one in the main app.

So what I'm asking is basically how to make the rake db:seed task do things defined within my gemified engine just by having the gem required in the gemfile?

2 Answers 2

3

So shortly after asking this I figured it out. First step was taken from here: How to add a gems tasks to the main app

Then inside a task file

#lib/task/some_task.rake
Rake::Task['db:seed'].enhance ['my_seed_task']

#lib/tasks/my_seed_task.rake
task 'my_seed_task' do
  ...
end

And now when in the main app I run rake db:seed it runs whatever my_seed_task defines as a perquisite.

5
  • It's brittle with the dependency on Rakefile additions. Rails has a reflector built-in to the rails::generator. Aug 28, 2012 at 16:02
  • Why do you say that it's brittle?
    – Shrewd
    Aug 29, 2012 at 7:04
  • If one forgets to add it to a rakefile then the gem assets will be misplaced. Extended the Rails::Generators::Base ensures these assets will be included. If you wanted a conditional inclusion then that should be a condition of including the gem itself. My first thought, for an asset gem with some assets you may not want to include, is that 1. there are too many assets in the gem, or 2. an asset gem is the wrong mechanism. Aug 29, 2012 at 12:53
  • The point is that I'm using enhance. This way no one adds it to the rakefile, rather installing the gem into the rails application automatically make my rake task a perquisite of db:seed which needs to be run anyway. And this has to be done in order to work with the gem. A generator would have to be run through the host application as well and then a db:seed run just the same wouldn't it?
    – Shrewd
    Aug 29, 2012 at 13:51
  • I'll have to think about that: the difference of responsibility between Rails generator components, and Rake take targets. In app-centric thinking, the app could not be aware of the additional assets, even though it is aware of the gem. Aug 29, 2012 at 18:29
1

Try instead to extend Rails::Generators::Base.

This is the mechanism as given in the docs:

"Each public method in the generator is executed when a generator is invoked"

so

class DbGenerator < Rails::Generators::Base
  source_root File.expand_path('../../../../db', __FILE__)

  def copy_seeds_file
    copy_file 'seeds.rb', 'db/seeds.rb'
  end

  def copy_seeds_dir
    Dir.foreach seeds_dir do |file|
      copy_file "seeds/#{file}", "db/seeds/#{file}"  unless file.match /^\./
    end
  end

  def seeds_dir
    File.expand_path 'seeds', self.class.source_root
  end
end

This approach will allow all gem-based seed data to copy into the app dir for the db:seed run

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.