forked from sevenwire/bootstrapper
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Nathan Sutton
committed
Aug 31, 2008
1 parent
17b0cbc
commit aa42c47
Showing
11 changed files
with
194 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
Copyright (c) 2008 [name of plugin creator] | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining | ||
a copy of this software and associated documentation files (the | ||
"Software"), to deal in the Software without restriction, including | ||
without limitation the rights to use, copy, modify, merge, publish, | ||
distribute, sublicense, and/or sell copies of the Software, and to | ||
permit persons to whom the Software is furnished to do so, subject to | ||
the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
Bootstrapper | ||
============ | ||
|
||
A rather simple plugin that executes a block to load seed/bootstrap data | ||
into a database. | ||
|
||
|
||
Example | ||
======= | ||
|
||
Run the bootstrapper generator: | ||
|
||
ruby script/generate bootstrapper | ||
|
||
This will place a bootstrap.rb file in the db directory within your project. | ||
|
||
The file will have something like this in it: | ||
|
||
Bootstrapper.for :development do |b| | ||
end | ||
|
||
Bootstrapper.for :production do |b| | ||
end | ||
|
||
Bootstrapper.for :test do |b| | ||
end | ||
|
||
Bootstrapper.for :staging do |b| | ||
end | ||
|
||
Using things like Factory Girl and Forgery you can quickly and easily | ||
generate fake, random, and/or seed data. | ||
|
||
An example using Factory Girl: | ||
|
||
require File.join(RAILS_ROOT, 'test', 'factories') | ||
|
||
Bootstrapper.for :development do |b| | ||
b.truncate_tables :addresses | ||
b.run :users | ||
|
||
Factory(:us_address, :state => "ME") | ||
Factory(:us_address, :state => "IL") | ||
Factory(:us_address, :state => "CA") | ||
end | ||
|
||
Bootstrapper.for :production do |b| | ||
end | ||
|
||
Bootstrapper.for :test do |b| | ||
end | ||
|
||
Bootstrapper.for :staging do |b| | ||
end | ||
|
||
Bootstrapper.for :users do |b| | ||
3.times{ Factory(:user) } | ||
Factory(:user, :login => "admin", | ||
:password => "sekret", | ||
:password_confirmation => "sekret") | ||
end | ||
|
||
With that file, you could run: | ||
|
||
rake db:bootstrap | ||
|
||
Which will run the development (default) bootstrap. You can specify which | ||
bootstrap task to run by specifying: | ||
|
||
rake db:bootstrap BOOTSTRAP=users | ||
|
||
You can also run bootstrap for another environment: | ||
|
||
rake db:bootstrap RAILS_ENV=production | ||
|
||
You can even run a specific bootstrap for another environment: | ||
|
||
rake db:bootstrap BOOTSTRAP=users RAILS_ENV=production | ||
|
||
The variable passed into a Bootstrapper block is actually Bootstrapper | ||
itself, so you can use any methods it has. | ||
|
||
You can delete all records from tables using 'truncate_tables': | ||
|
||
b.truncate_tables :users | ||
b.truncate_tables :users, :addresses | ||
|
||
You can run other bootstrap tasks using 'run': | ||
|
||
b.run :users | ||
b.run :production | ||
|
||
Factory Girl <http://github.com/thoughtbot/factory_girl/tree/master> | ||
Forgery <http://github.com/sevenwire/forgery/tree/master> | ||
|
||
Copyright (c) 2008 [name of plugin creator], released under the MIT license |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
require 'rake' | ||
require 'rake/testtask' | ||
require 'rake/rdoctask' | ||
|
||
desc 'Default: run unit tests.' | ||
task :default => :test | ||
|
||
desc 'Test the bootstrapper plugin.' | ||
Rake::TestTask.new(:test) do |t| | ||
t.libs << 'lib' | ||
t.pattern = 'test/**/*_test.rb' | ||
t.verbose = true | ||
end | ||
|
||
desc 'Generate documentation for the bootstrapper plugin.' | ||
Rake::RDocTask.new(:rdoc) do |rdoc| | ||
rdoc.rdoc_dir = 'rdoc' | ||
rdoc.title = 'Bootstrapper' | ||
rdoc.options << '--line-numbers' << '--inline-source' | ||
rdoc.rdoc_files.include('README') | ||
rdoc.rdoc_files.include('lib/**/*.rb') | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
class BootstrapperGenerator < Rails::Generator::Base | ||
def manifest | ||
record do |m| | ||
m.directory 'db' | ||
m.template 'bootstrap.erb', 'db/bootstrap.rb' | ||
end | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<%- ActiveRecord::Base.configurations.keys.each do |env| -%> | ||
Bootstrapper.for :<%= env %> do |b| | ||
end | ||
|
||
<%- end -%> |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
require File.dirname(__FILE__) + '/lib/bootstrapper.rb' |
Empty file.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
class Bootstrapper | ||
class_inheritable_accessor :tasks | ||
write_inheritable_attribute :tasks, HashWithIndifferentAccess.new | ||
|
||
def self.for(key, &block) | ||
tasks[key] = block | ||
end | ||
|
||
def self.run(key) | ||
puts ">> Started executing bootstrap for #{key}" | ||
tasks[key].call(self) | ||
puts ">> Finished executing bootstrap for #{key}" | ||
end | ||
|
||
def self.truncate_tables(*tables) | ||
tables.each do |table| | ||
table.to_s.classify.constantize.delete_all | ||
end | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
namespace :db do | ||
desc "Bootstraps the database for the given environment. BOOTSTRAP option lets you run a specific bootstrap task in the given environment." | ||
task :bootstrap => :environment do | ||
require File.join(RAILS_ROOT, 'db', 'bootstrap') | ||
Bootstrapper.run(ENV['BOOTSTRAP'] || RAILS_ENV) | ||
end | ||
|
||
namespace :bootstrap do | ||
desc "Resets the database and bootstraps it for the given environment. BOOTSTRAP option lets you run a specific bootstrap task in the given environment." | ||
task :reset => 'db:migrate:reset' do | ||
Rake::Task['db:bootstrap'].invoke | ||
end | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
require 'test/unit' | ||
|
||
class BootstrapperTest < Test::Unit::TestCase | ||
# Replace this with your real tests. | ||
def test_this_plugin | ||
flunk | ||
end | ||
end |
Empty file.