rakitin
rakitin

Reputation: 2185

how do i target a seeds.rb in an engine to avoid seeding my entire app?

My project is on Rails 3.2 and refinerycms v 2.0.10

I just generated a new engine and and ran my bundle and rails generate commands, and my migration. Now, per the docs, I need to run db:seed but I don't want to execute a db:seed at the app level because I have several other engines and I don't want to re-seed them.

it is related to this question: Rails engine / How to use seed? but the answer there is to run db:seed at the app level.

So how would I say something like rake myNewEngine:db:seed ? I know it can be done but my google fu is apparently too weak to dredge it up.

Upvotes: 1

Views: 705

Answers (2)

Luismejiadev
Luismejiadev

Reputation: 188

Edit the YOUR_ENGINE/lib/tasks/YOUR_ENGINE_tasks.rake

namespace :db do
  namespace :YOUR_ENGINE do
    desc "loads all seeds in db/seeds.rb"
    task :seeds  => :environment do
      YOUR_ENGINE::Engine.load_seed
    end

    namespace :seed do
        Dir[Rails.root.join('YOUR_ENGINE', 'db', 'seeds', '*.rb')].each do |filename|
          task_name = File.basename(filename, '.rb')
          desc "Seed "      task_name      ", based on the file with the same name in `db/seeds/*.rb`"
          task task_name.to_sym => :environment do
            load(filename) if File.exist?(filename)
          end
        end
      end
  end
end 

then in your main app you can execute your custom seeds commands, executing any seed file individually

$rake -T | grep YOUR_ENGINE
rake db:YOUR_ENGINE:seed:seed1            # Seed seed1, based on the file with the same name in `db/seeds/*.rb`
rake db:YOUR_ENGINE:seeds                 # loads all seeds in db/seeds.rb

Upvotes: 0

polmiro
polmiro

Reputation: 1986

You can just generate your own rake task. Create a your_engine.rake file and make sure it is loaded in your Rakefile.

namespace :your_engine do
  namespace :db do
    task :seed do
      YourEngine::Engine.load_seed
    end
  end
end

Upvotes: 2

Related Questions