bootstrapper 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.markdown +105 -0
- data/lib/bootstrapper.rb +3 -0
- data/lib/bootstrapper/bootstrapper.rb +64 -0
- data/lib/bootstrapper/railtie.rb +18 -0
- data/lib/generators/bootstrapper_generator.rb +11 -0
- data/lib/generators/templates/bootstrap.erb +5 -0
- data/lib/tasks/bootstrap.rake +29 -0
- metadata +102 -0
data/README.markdown
ADDED
@@ -0,0 +1,105 @@
|
|
1
|
+
Bootstrapper
|
2
|
+
============
|
3
|
+
|
4
|
+
A rather simple gem that executes a block to load seed/bootstrap data
|
5
|
+
into a database.
|
6
|
+
|
7
|
+
|
8
|
+
Example
|
9
|
+
=======
|
10
|
+
|
11
|
+
gem install bootstrapper
|
12
|
+
|
13
|
+
Run the bootstrapper generator:
|
14
|
+
|
15
|
+
Rails 3:
|
16
|
+
|
17
|
+
./script/rails g bootstrapper
|
18
|
+
|
19
|
+
Rails 2:
|
20
|
+
|
21
|
+
ruby script/generate bootstrapper
|
22
|
+
|
23
|
+
|
24
|
+
This will place a bootstrap.rb file in the db directory within your project.
|
25
|
+
|
26
|
+
The file will have something like this in it:
|
27
|
+
|
28
|
+
Bootstrapper.for :development do |b|
|
29
|
+
end
|
30
|
+
|
31
|
+
Bootstrapper.for :production do |b|
|
32
|
+
end
|
33
|
+
|
34
|
+
Bootstrapper.for :test do |b|
|
35
|
+
end
|
36
|
+
|
37
|
+
Bootstrapper.for :staging do |b|
|
38
|
+
end
|
39
|
+
|
40
|
+
Using things like Factory Girl and Forgery you can quickly and easily
|
41
|
+
generate fake, random, and/or seed data.
|
42
|
+
|
43
|
+
An example using Factory Girl:
|
44
|
+
|
45
|
+
Bootstrapper.for :development do |b|
|
46
|
+
b.truncate_tables :addresses
|
47
|
+
b.run :users
|
48
|
+
|
49
|
+
Factory(:us_address, :state => "ME")
|
50
|
+
Factory(:us_address, :state => "IL")
|
51
|
+
Factory(:us_address, :state => "CA")
|
52
|
+
end
|
53
|
+
|
54
|
+
Bootstrapper.for :production do |b|
|
55
|
+
end
|
56
|
+
|
57
|
+
Bootstrapper.for :test do |b|
|
58
|
+
end
|
59
|
+
|
60
|
+
Bootstrapper.for :staging do |b|
|
61
|
+
end
|
62
|
+
|
63
|
+
Bootstrapper.for :users do |b|
|
64
|
+
3.times{ Factory(:user) }
|
65
|
+
Factory(:user, :login => "admin",
|
66
|
+
:password => "sekret",
|
67
|
+
:password_confirmation => "sekret")
|
68
|
+
end
|
69
|
+
|
70
|
+
With that file, you could run:
|
71
|
+
|
72
|
+
rake db:bootstrap
|
73
|
+
|
74
|
+
Which will run the development (default) bootstrap. You can specify which
|
75
|
+
bootstrap task to run by specifying:
|
76
|
+
|
77
|
+
rake db:bootstrap BOOTSTRAP=users
|
78
|
+
|
79
|
+
You can also run bootstrap for another environment:
|
80
|
+
|
81
|
+
rake db:bootstrap RAILS_ENV=production
|
82
|
+
|
83
|
+
You can even run a specific bootstrap for another environment:
|
84
|
+
|
85
|
+
rake db:bootstrap BOOTSTRAP=users RAILS_ENV=production
|
86
|
+
|
87
|
+
The variable passed into a Bootstrapper block is actually Bootstrapper
|
88
|
+
itself, so you can use any methods it has.
|
89
|
+
|
90
|
+
You can delete all records from tables using 'truncate_tables':
|
91
|
+
|
92
|
+
b.truncate_tables :users
|
93
|
+
b.truncate_tables :users, :addresses
|
94
|
+
|
95
|
+
You can run other bootstrap tasks using 'run':
|
96
|
+
|
97
|
+
b.run :users
|
98
|
+
b.run :production
|
99
|
+
|
100
|
+
FactoryGirl [http://github.com/thoughtbot/factory_girl/tree/master](http://github.com/thoughtbot/factory_girl/tree/master)
|
101
|
+
|
102
|
+
Forgery [http://github.com/sevenwire/forgery/tree/master](http://github.com/sevenwire/forgery/tree/master)
|
103
|
+
|
104
|
+
Copyright (c) 2008 Sevenwire, released under the MIT license
|
105
|
+
Contributions from Jared Mehle (jrmehle), Joel Meador (janxious), Jan Schwenzien (jeanmartin), Ken Pratt (kenpratt)
|
data/lib/bootstrapper.rb
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
class Bootstrapper
|
2
|
+
class_inheritable_accessor :tasks
|
3
|
+
write_inheritable_attribute :tasks, ActiveSupport::HashWithIndifferentAccess.new
|
4
|
+
|
5
|
+
def self.for(key, &block)
|
6
|
+
tasks[key] = block
|
7
|
+
end
|
8
|
+
|
9
|
+
@depth = 0
|
10
|
+
|
11
|
+
def self.run_start(key)
|
12
|
+
print "\033[1;31;40mBootstrapping\e[0m >>" if @depth == 0
|
13
|
+
print " [\033[1;33;40m:#{key}\e[0m"
|
14
|
+
@depth += 1
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.run_end(key)
|
18
|
+
@depth -= 1
|
19
|
+
print "]"
|
20
|
+
puts "\r\n\033[1;31;40mBootstrapping\e[0m >> \033[1;32;40mDone\e[0m" if @depth == 0
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.run(key)
|
24
|
+
self.run_start(key)
|
25
|
+
tasks[key].call(self)
|
26
|
+
self.run_end(key)
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.truncate_tables(*tables)
|
30
|
+
options = tables.last.is_a?(Hash) ? tables.pop : {}
|
31
|
+
if tables == [:all]
|
32
|
+
except = options[:except] || []
|
33
|
+
except = except.is_a?(Array) ? except.collect { |x| x.to_s } : [except.to_s]
|
34
|
+
|
35
|
+
tables = ActiveRecord::Base.connection.tables.select do |table|
|
36
|
+
table !~ /schema_(info|migrations)/ && !except.include?(table)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
tables.each do |table|
|
41
|
+
self.truncate_table(table)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def self.truncate_table(table_name)
|
46
|
+
conn = ActiveRecord::Base.connection
|
47
|
+
sql = case conn.adapter_name.downcase
|
48
|
+
when /mysql/
|
49
|
+
"TRUNCATE TABLE #{conn.quote_table_name(table_name)};"
|
50
|
+
else
|
51
|
+
"DELETE FROM #{table_name}"
|
52
|
+
end
|
53
|
+
conn.execute(sql)
|
54
|
+
end
|
55
|
+
|
56
|
+
def self.fixtures(*fixtures)
|
57
|
+
Fixtures.create_fixtures(File.join(Rails.root, 'db', 'populate'), fixtures)
|
58
|
+
|
59
|
+
end
|
60
|
+
|
61
|
+
def self.sql(sql)
|
62
|
+
ActiveRecord::Base.connection.execute(sql)
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
class Bootstrapper
|
2
|
+
class Railtie < Rails::Railtie
|
3
|
+
config.bootstrapper = ActiveSupport::OrderedOptions.new
|
4
|
+
|
5
|
+
# configure our plugin on boot. other extension points such
|
6
|
+
# as configuration, rake tasks, etc, are also available
|
7
|
+
initializer "bootstrapper.initialize" do |app|
|
8
|
+
end
|
9
|
+
|
10
|
+
ActiveSupport.on_load :active_record do
|
11
|
+
require 'bootstrapper/bootstrapper'
|
12
|
+
end
|
13
|
+
|
14
|
+
rake_tasks do
|
15
|
+
load "tasks/bootstrap.rake"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
namespace :db do
|
2
|
+
desc "Bootstraps the database for the given environment. BOOTSTRAP option lets you run a specific bootstrap task in the given environment."
|
3
|
+
task :bootstrap => :environment do
|
4
|
+
require 'active_record/base'
|
5
|
+
require File.join(Rails.root, 'db', 'bootstrap')
|
6
|
+
Bootstrapper.run(ENV['BOOTSTRAP'] || Rails.env)
|
7
|
+
end
|
8
|
+
|
9
|
+
namespace :bootstrap do
|
10
|
+
desc "Resets the database and bootstraps it for the given environment. BOOTSTRAP option lets you run a specific bootstrap task in the given environment."
|
11
|
+
task :reset => ['db:migrate:reset', 'reset_environment'] do
|
12
|
+
Rake::Task['db:bootstrap'].invoke
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
desc "Resets the rails environment"
|
18
|
+
task :reset_environment do
|
19
|
+
# Ripped directly out of railties/lib/console_app.rb in rails and
|
20
|
+
# only changed slightly.
|
21
|
+
puts "Reloading environment..."
|
22
|
+
if ActionController::Dispatcher.respond_to?(:cleanup_application)
|
23
|
+
dispatcher = ActionController::Dispatcher
|
24
|
+
else
|
25
|
+
dispatcher = ActionController::Dispatcher.new($stdout)
|
26
|
+
end
|
27
|
+
dispatcher.cleanup_application
|
28
|
+
dispatcher.reload_application
|
29
|
+
end
|
metadata
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: bootstrapper
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 19
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 3
|
9
|
+
- 0
|
10
|
+
version: 0.3.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Jared Mehle
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-06-03 00:00:00 Z
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: activerecord
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
hash: 3
|
29
|
+
segments:
|
30
|
+
- 2
|
31
|
+
- 3
|
32
|
+
- 0
|
33
|
+
version: 2.3.0
|
34
|
+
type: :runtime
|
35
|
+
version_requirements: *id001
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: activesupport
|
38
|
+
prerelease: false
|
39
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
hash: 3
|
45
|
+
segments:
|
46
|
+
- 2
|
47
|
+
- 3
|
48
|
+
- 0
|
49
|
+
version: 2.3.0
|
50
|
+
type: :runtime
|
51
|
+
version_requirements: *id002
|
52
|
+
description:
|
53
|
+
email: jrmehle@gmail.com
|
54
|
+
executables: []
|
55
|
+
|
56
|
+
extensions: []
|
57
|
+
|
58
|
+
extra_rdoc_files:
|
59
|
+
- README.markdown
|
60
|
+
files:
|
61
|
+
- lib/bootstrapper.rb
|
62
|
+
- lib/bootstrapper/bootstrapper.rb
|
63
|
+
- lib/bootstrapper/railtie.rb
|
64
|
+
- lib/generators/bootstrapper_generator.rb
|
65
|
+
- lib/generators/templates/bootstrap.erb
|
66
|
+
- lib/tasks/bootstrap.rake
|
67
|
+
- README.markdown
|
68
|
+
homepage: http://github.com/jrmehle/bootstrapper
|
69
|
+
licenses: []
|
70
|
+
|
71
|
+
post_install_message:
|
72
|
+
rdoc_options: []
|
73
|
+
|
74
|
+
require_paths:
|
75
|
+
- lib
|
76
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
77
|
+
none: false
|
78
|
+
requirements:
|
79
|
+
- - ">="
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
hash: 3
|
82
|
+
segments:
|
83
|
+
- 0
|
84
|
+
version: "0"
|
85
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
|
+
none: false
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
hash: 3
|
91
|
+
segments:
|
92
|
+
- 0
|
93
|
+
version: "0"
|
94
|
+
requirements: []
|
95
|
+
|
96
|
+
rubyforge_project:
|
97
|
+
rubygems_version: 1.8.1
|
98
|
+
signing_key:
|
99
|
+
specification_version: 3
|
100
|
+
summary: The better way to load seed data (than seeds.rb)
|
101
|
+
test_files: []
|
102
|
+
|