Created
October 25, 2012 11:15
-
-
Save cpetschnig/3952049 to your computer and use it in GitHub Desktop.
Proposal for better structure of seeds in Rails engines
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Engine | |
====== | |
File structure: | |
EngineFoo | |
- app | |
- db | |
- seeds.rb | |
- lib | |
- seeds | |
- my_seeds.rb | |
- engine_foo.rb | |
engine_foo/lib/engine_foo.rb | |
============================ | |
module EngineFoo | |
autoload :MySeeds, 'seeds/my_seeds' | |
end | |
engine_foo/lib/seeds/my_seeds.rb | |
================================ | |
module EngineFoo | |
class MySeeds | |
def self.create_this_data | |
... | |
end | |
def self.create_that_data | |
... | |
end | |
def self.create | |
create_this_data | |
create_that_data | |
end | |
end | |
end | |
engine_foo/db/seeds.rb | |
====================== | |
EngineFoo::MySeeds.create | |
HostApp | |
======= | |
host_app/db/seeds.rb | |
==================== | |
# make the engine load its seed class | |
EngineFoo::MySeeds | |
# modify the seeds | |
class EngineFoo::MySeeds | |
def self.create_that_data | |
# modified implementation | |
end | |
end | |
# run the seeds | |
EngineFoo::Engine.load_seed | |
Advantages | |
========== | |
* Better structured in the engine itself | |
* Easily extendable | |
* Testable | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment