Skip to content

Commit

Permalink
Added a post_install_message and bumped the version.
Browse files Browse the repository at this point in the history
  • Loading branch information
james2m committed Apr 20, 2011
1 parent 14e7b78 commit d68c999
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 46 deletions.
18 changes: 9 additions & 9 deletions README.md
Expand Up @@ -5,11 +5,11 @@ Seedbank allows you to structure your Rails seed data instead of having it all d

Seedbank assumes common seed data is under db/seeds and any directories under db/seeds/ are specific to an environment, so db/seeds/development is contains all your development only seed data.

The reason behind Seedbank is laziness. When I checkout or re-visit a project I don't want to mess around getting my environment setup I just want the code and a database loaded with data in a known state. Since the Rails core team were good enough to give us rake db:setup it would be rude not to use it.
The reason behind Seedbank is laziness. When I checkout or re-visit a project I don't want to mess around getting my environment setup I just want the code and a database loaded with data in a known state. Since the Rails core team were good enough to give us rake db:setup it would be rude not to use it.

rake db:setup # Create the database, load the schema, and initialize with the seed data (use db:reset to also drop the db first)

To achieve this slothful aim Seedbank renames the original db:seed rake task to db:seed:original, makes it a dependency for all the Seedbank seeds and adds a new db:seed task that loads all the common seeds in db/seeds plus all the seeds for the current Rails environment.
To achieve this slothful aim Seedbank renames the original db:seed rake task to db:seed:original, makes it a dependency for all the Seedbank seeds and adds a new db:seed task that loads all the common seeds in db/seeds plus all the seeds for the current Rails environment.

Example
=======
Expand All @@ -21,7 +21,7 @@ Seedbank seeds follow this structure;
develpment/
users.seeds.rb
foo.seeds.rb

This would generate the following Rake tasks

rake db:seed # Load the seed data from db/seeds.rb, db/seeds/*.seeds.rb and db/seeds/ENVIRONMENT/*.seeds.rb. ENVIRONMENT is the current environment in Rails.env.
Expand All @@ -35,11 +35,11 @@ This would generate the following Rake tasks
Therefor assuming RAILS_ENV is not set or is 'development'

$ rake db:seed
would load the seeds in db/seeds.rb, db/seeds/bar.seeds.rb, db/seeds/foo.seeds/rb and db/seeds/development/users.seeds.rb. Whereas

would load the seeds in db/seeds.rb, db/seeds/bar.seeds.rb, db/seeds/foo.seeds/rb and db/seeds/development/users.seeds.rb. Whereas

$ RAILS_ENV=production db:seed

would load the seeds in db/seeds.rb, db/seeds/bar.seeds.rb and db/seeds/foo.seeds/rb

Installation
Expand All @@ -58,7 +58,7 @@ That's it!
Add to your config/environment.rb

config.gem 'seedbank'

Install the gem;

$ rake gems:install
Expand All @@ -67,7 +67,7 @@ Then in the bottom of your applications Rakefile:

require 'seedbank'
Seedbank.load_tasks if defined?(Seedbank)

If you vendor the gem you'll need to change the require to the specific path.

Note on Patches/Pull Request
Expand All @@ -76,7 +76,7 @@ Note on Patches/Pull Request
* Fork the project.
* Make your feature addition or bug fix.
* Add tests for it (when I have some). This is important so I don't break it in a future version unintentionally.
* Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but
* Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but
bump version in a commit by itself I can ignore it when I pull)
* Send me a pull request. Bonus points for topic branches.

Expand Down
12 changes: 6 additions & 6 deletions lib/seedbank.rb
Expand Up @@ -8,17 +8,17 @@
Rake::Application.send(:include, Seedbank::TaskManager)

module Seedbank

@@seeds_root = 'db/seeds'

def self.seeds_root
@@seeds_root
end

def self.load_tasks
Dir[File.expand_path("tasks/*.rake", File.dirname(__FILE__))].each { |ext| load ext }
Dir[File.expand_path("tasks/*.rake", File.dirname(__FILE__))].each { |ext| load ext }
end

require 'seedbank/railtie' if defined?(Rails) && Rails::VERSION::MAJOR >= 3

end
12 changes: 6 additions & 6 deletions lib/seedbank/dsl.rb
@@ -1,29 +1,29 @@
module Seedbank
module DSL

def override_task(*args, &block)
name, params, deps = Rake.application.resolve_args(args.dup)
fq_name = Rake.application.instance_variable_get(:@scope).dup.push(name).join(':')
new_name = "#{fq_name}:original"
Rake::Task.rename_task(fq_name, new_name)
Rake::Task.define_task(*args, &block)
end
# Creates a task namespaced in @seeds_path

# Creates a task namespaced in @seeds_path
def define_seed_task(seed_file)
relative_root = seed_file.sub(seeds_root + '/', '')
scopes = File.dirname(relative_root).gsub(/^\./, '').split('/').unshift('seed')
fq_name = scopes.push(File.basename(seed_file, '.seeds.rb')).join(':')

args = { fq_name => 'db:abort_if_pending_migrations' }
task = Rake::Task.define_task(args) { load(seed_file) if File.exist?(seed_file) }
task = Rake::Task.define_task(args) { load(seed_file) if File.exist?(seed_file) }
task.add_description "Load the seed data from #{seed_file}"
fq_name
end

def seeds_root
Seedbank.seeds_root
end

end
end
2 changes: 1 addition & 1 deletion lib/seedbank/railtie.rb
Expand Up @@ -7,6 +7,6 @@ class Railtie < Rails::Railtie
rake_tasks do
Seedbank.load_tasks
end

end
end
20 changes: 10 additions & 10 deletions lib/tasks/seed.rake
@@ -1,36 +1,36 @@
namespace :db do

include Seedbank::DSL

base_dependencies = ['db:seed:original']
override_dependency = []

common_dependencies = []
# Create seed tasks for all the seeds in seeds_path and add them to the dependency
# Create seed tasks for all the seeds in seeds_path and add them to the dependency
# list along with the original db/seeds.rb.
Dir.glob(File.join(seeds_root, '*.seeds.rb')).each do |seed_file|
common_dependencies << define_seed_task(seed_file)
end

desc "Load the seed data from db/seeds.rb and db/seeds/*.seeds.rb."
task ['seed', 'common'] => base_dependencies + common_dependencies

# Glob through the directories under seeds_path assuming they are all environments
# and create a task for each and add it to the dependency list. Then create a task
# for the environment
Dir[seeds_root + '/*/'].each do |e|
environment = File.basename(e)

environment_dependencies = []
Dir.glob(File.join(seeds_root, environment, '*.seeds.rb')).each do |seed_file|
environment_dependencies << define_seed_task(seed_file)
end

desc <<-EOT
Load the seed data from db/seeds.rb, db/seeds/*.seeds.rb and db/seeds/#{environment}/*.seeds.rb.
EOT
task ['seed', environment] => ['db:seed:common'] + environment_dependencies

override_dependency << "db:seed:#{environment}" if Rails.env == environment
end

Expand All @@ -40,5 +40,5 @@ namespace :db do
ENVIRONMENT is the current environment in Rails.env.
EOT
override_task :seed => ['db:seed:common'] + override_dependency

end
49 changes: 35 additions & 14 deletions seedbank.gemspec
@@ -1,30 +1,51 @@
Gem::Specification.new do |s|
s.name = %q{seedbank}
s.version = "0.0.6"
s.version = "0.0.7"
s.date = %q{2011-03-20}

s.required_rubygems_version = Gem::Requirement.new(">=1.2.0") if s.respond_to?(:required_rubygems_version=)
s.rubygems_version = %q{1.3.5}

s.authors = ["James McCarthy"]
s.date = %q{2011-03-20}
s.email = %q{james2mccarthy@gmail.com}
s.homepage = %q{http://github.com/james2m/seedbank}
s.summary = %q{
Extends Rails seeds to split out complex seeds into their own file
and have different seeds for each environment.
}
s.description = %q{
Extends Rails seeds to split out complex seeds into multiple
Extends Rails seeds to split out complex seeds into multiple
files and lets each environment have it's own seeds.
}
s.email = %q{james2mccarthy@gmail.com}

s.files = Dir.glob('**/*') - Dir.glob('seedbank*.gem')
s.require_paths = ["lib"]

s.rdoc_options = ["--charset=UTF-8"]
s.extra_rdoc_files = [
"MIT-LICENSE",
"README.md"
]
s.files = Dir.glob('**/*') - Dir.glob('seedbank*.gem')
s.homepage = %q{http://github.com/james2m/seedbank}
s.rdoc_options = ["--charset=UTF-8"]
s.require_paths = ["lib"]
s.rubygems_version = %q{1.3.5}
s.summary = %q{
Extends Rails seeds to split out complex seeds into their own file
and have different seeds for each environment.
}
s.test_files = Dir.glob('test/**/*')

s.test_files = Dir.glob('test/**/*')
s.add_development_dependency('test-unit')

s.post_install_message = %q{
================================================================================
Rails 2.x
---------
If you are using Seedbank with Rails 2.x you will need to place the following at
the end of your Rakefile so Rubygems can load the seedbank tasks;
require 'seedbank'
Seedbank.load_tasks if defined?(Seedbank)
Rails 3.x
---------
Your work here is done!
================================================================================
}
end

0 comments on commit d68c999

Please sign in to comment.