mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Add helper for loading seed data for engine and application
Signed-off-by: José Valim <jose.valim@gmail.com>
This commit is contained in:
parent
0523b55ab9
commit
d475de7e25
4 changed files with 30 additions and 2 deletions
|
@ -299,8 +299,7 @@ namespace :db do
|
|||
|
||||
desc 'Load the seed data from db/seeds.rb'
|
||||
task :seed => 'db:abort_if_pending_migrations' do
|
||||
seed_file = File.join(Rails.root, 'db', 'seeds.rb')
|
||||
load(seed_file) if File.exist?(seed_file)
|
||||
Rails.application.load_seed
|
||||
end
|
||||
|
||||
namespace :fixtures do
|
||||
|
|
|
@ -380,6 +380,15 @@ module Rails
|
|||
@config ||= Engine::Configuration.new(find_root_with_flag("lib"))
|
||||
end
|
||||
|
||||
# Load data from db/seeds.rb file. It can be used in to load engines'
|
||||
# seeds, e.g.:
|
||||
#
|
||||
# Blog::Engine.load_seed
|
||||
def load_seed
|
||||
seed_file = config.paths.db.seeds.to_a.first
|
||||
load(seed_file) if File.exist?(seed_file)
|
||||
end
|
||||
|
||||
# Add configured load paths to ruby load paths and remove duplicates.
|
||||
initializer :set_load_path, :before => :bootstrap_hook do
|
||||
_all_load_paths.reverse_each do |path|
|
||||
|
|
|
@ -37,6 +37,7 @@ module Rails
|
|||
paths.vendor.plugins "vendor/plugins"
|
||||
paths.db "db"
|
||||
paths.db.migrate "db/migrate"
|
||||
paths.db.seeds "db/seeds.rb"
|
||||
paths
|
||||
end
|
||||
end
|
||||
|
|
|
@ -624,5 +624,24 @@ module RailtiesTest
|
|||
assert !File.exist?(File.join(app_path, 'public/bukkits'))
|
||||
end
|
||||
end
|
||||
|
||||
test "loading seed data" do
|
||||
@plugin.write "db/seeds.rb", <<-RUBY
|
||||
Bukkits::Engine.config.bukkits_seeds_loaded = true
|
||||
RUBY
|
||||
|
||||
app_file "db/seeds.rb", <<-RUBY
|
||||
Rails.application.config.app_seeds_loaded = true
|
||||
RUBY
|
||||
|
||||
boot_rails
|
||||
|
||||
Rails.application.load_seed
|
||||
assert Rails.application.config.app_seeds_loaded
|
||||
assert_raise(NoMethodError) do Bukkits::Engine.config.bukkits_seeds_loaded end
|
||||
|
||||
Bukkits::Engine.load_seed
|
||||
assert Bukkits::Engine.config.bukkits_seeds_loaded
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue