seed_dumper 0.1.0 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +1 -0
- data/Rakefile +1 -1
- data/lib/seed_dumper/fetcher.rb +11 -8
- data/lib/seed_dumper/railtie.rb +1 -1
- data/lib/seed_dumper/writer.rb +3 -3
- data/lib/seed_dumper.rb +15 -4
- data/lib/tasks/seed_dumper.rake +26 -0
- data/seed_dumper.gemspec +20 -12
- metadata +41 -46
- data/VERSION +0 -1
- data/lib/tasks/seed_dump.rake +0 -10
data/CHANGELOG.rdoc
CHANGED
@@ -0,0 +1 @@
|
|
1
|
+
# Fixed not loading all the models in the rake task
|
data/Rakefile
CHANGED
@@ -30,7 +30,7 @@ Rake::RDocTask.new do |rdoc|
|
|
30
30
|
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
31
31
|
|
32
32
|
rdoc.rdoc_dir = 'rdoc'
|
33
|
-
rdoc.title = "
|
33
|
+
rdoc.title = "seed_dumper #{version}"
|
34
34
|
rdoc.rdoc_files.include('README*')
|
35
35
|
rdoc.rdoc_files.include('lib/**/*.rb')
|
36
36
|
end
|
data/lib/seed_dumper/fetcher.rb
CHANGED
@@ -4,26 +4,29 @@ module SeedDumper
|
|
4
4
|
class Fetcher
|
5
5
|
|
6
6
|
def self.fetch_data(klass, options={})
|
7
|
+
ignore = ['created_at', 'updated_at']
|
7
8
|
model_name = klass.name
|
8
9
|
|
9
10
|
puts "Adding #{model_name.camelize} seeds."
|
10
11
|
|
11
|
-
|
12
|
-
records = klass.all
|
13
|
-
|
14
|
-
records.each_with_index do |record, index|
|
12
|
+
records = klass.all.map do |record|
|
15
13
|
attr_s = [];
|
16
14
|
|
17
|
-
record.attributes.each do |key, value|
|
15
|
+
record.attributes.delete_if { |k, v| ignore.include?(k) }.each do |key, value|
|
18
16
|
value = value.class == Time ? "\"#{value}\"" : value.inspect
|
19
|
-
|
17
|
+
value = nil if value.is_a?(String) && value == "\"\""
|
18
|
+
value = nil if value == 'nil' || value == "nil"
|
19
|
+
|
20
|
+
unless value.nil?
|
21
|
+
attr_s.push("#{key.to_sym.inspect} => #{value}")# unless key == 'id'
|
22
|
+
end
|
20
23
|
end
|
21
24
|
|
22
|
-
|
25
|
+
"#{model_name.camelize}.create(#{attr_s.join(', ')})"
|
23
26
|
end
|
24
27
|
# / records.each_with_index
|
25
28
|
|
26
|
-
|
29
|
+
records
|
27
30
|
end
|
28
31
|
|
29
32
|
end
|
data/lib/seed_dumper/railtie.rb
CHANGED
data/lib/seed_dumper/writer.rb
CHANGED
@@ -2,13 +2,13 @@ module SeedDumper
|
|
2
2
|
|
3
3
|
class Writer
|
4
4
|
|
5
|
-
def self.write_data(klass_name,
|
5
|
+
def self.write_data(klass_name, records)
|
6
6
|
FileUtils.mkdir("#{Rails.root}/db/seeds/") unless File.exist?("#{Rails.root}/db/seeds/")
|
7
7
|
seed_file_name = "#{Rails.root}/db/seeds/#{klass_name.tableize}.rb"
|
8
8
|
|
9
|
+
puts "Writing #{seed_file_name}."
|
9
10
|
File.open(seed_file_name, 'w') do |f|
|
10
|
-
puts
|
11
|
-
f << data_string
|
11
|
+
records.each { |record| f.puts(record) }
|
12
12
|
end
|
13
13
|
|
14
14
|
end
|
data/lib/seed_dumper.rb
CHANGED
@@ -1,6 +1,17 @@
|
|
1
|
-
require '
|
2
|
-
require '
|
1
|
+
require 'rake' unless defined?(Rake)
|
2
|
+
require 'rails' unless defined?(Rails)
|
3
3
|
|
4
4
|
module SeedDumper
|
5
|
-
|
6
|
-
|
5
|
+
VERSION = '0.1.2'
|
6
|
+
|
7
|
+
autoload :Fetcher, 'seed_dumper/fetcher'
|
8
|
+
autoload :Writer, 'seed_dumper/writer'
|
9
|
+
|
10
|
+
def self.load_tasks
|
11
|
+
Dir[File.expand_path("tasks/*.rake", File.dirname(__FILE__))].each { |ext| load ext }
|
12
|
+
end
|
13
|
+
|
14
|
+
require 'seed_dumper/railtie' if defined?(Rails) && Rails::VERSION::MAJOR >= 3
|
15
|
+
end
|
16
|
+
|
17
|
+
|
@@ -0,0 +1,26 @@
|
|
1
|
+
namespace :db do
|
2
|
+
namespace :seed do
|
3
|
+
desc 'Dump records from the database into db/seeds.rb'
|
4
|
+
task :dump => :environment do
|
5
|
+
Dir.glob(Rails.root + '/app/models/*.rb').each { |file| require file }
|
6
|
+
models = Rails::VERSION::MAJOR >= 3 ? ActiveRecord::Base.descendants : Object.subclasses_of(ActiveRecord::Base)
|
7
|
+
|
8
|
+
models.each { |model| SeedDumper::Writer.write_data(model.name, SeedDumper::Fetcher.fetch_data(model) ) }
|
9
|
+
end
|
10
|
+
|
11
|
+
desc 'Dump records from a legacy database into db/seeds.rb'
|
12
|
+
task :legacy_dump => :environment do
|
13
|
+
tables = ActiveRecord::Base.connection.tables - ["schema_migrations"]
|
14
|
+
|
15
|
+
tables.each do |table|
|
16
|
+
model_name = table.pluralize.classify
|
17
|
+
body = proc { set_table_name table }
|
18
|
+
model = Class.new(ActiveRecord::Base, &body)
|
19
|
+
Object.const_set(model_name, model)
|
20
|
+
|
21
|
+
SeedDumper::Writer.write_data model_name, SeedDumper::Fetcher.fetch_data(model)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
# /db
|
data/seed_dumper.gemspec
CHANGED
@@ -1,18 +1,18 @@
|
|
1
1
|
#!/usr/bin/env gem build
|
2
2
|
# encoding: utf-8
|
3
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
4
|
|
4
5
|
require "base64"
|
6
|
+
require 'seed_dumper'
|
5
7
|
|
6
8
|
Gem::Specification.new do |s|
|
7
9
|
s.name = "seed_dumper"
|
8
|
-
s.version =
|
10
|
+
s.version = SeedDumper::VERSION
|
9
11
|
s.authors = ["Kevin Edwards"]
|
10
12
|
s.homepage = "http://github.com/kevTheDev/seed_dumper"
|
11
13
|
s.summary = "Create seed files for each model from your database data"
|
12
14
|
s.description = "#{s.summary}."
|
13
|
-
s.cert_chain = nil
|
14
15
|
s.email = "kev.j.edwards@gmail.com"
|
15
|
-
s.has_rdoc = true
|
16
16
|
|
17
17
|
s.extra_rdoc_files = [
|
18
18
|
"README.rdoc"
|
@@ -22,29 +22,37 @@ Gem::Specification.new do |s|
|
|
22
22
|
"MIT-LICENSE",
|
23
23
|
"README.rdoc",
|
24
24
|
"Rakefile",
|
25
|
-
"VERSION",
|
26
25
|
"lib/seed_dumper.rb",
|
27
26
|
"lib/seed_dumper/railtie.rb",
|
28
27
|
"lib/seed_dumper/fetcher.rb",
|
29
28
|
"lib/seed_dumper/writer.rb",
|
30
|
-
"lib/tasks/
|
29
|
+
"lib/tasks/seed_dumper.rake",
|
31
30
|
"seed_dumper.gemspec"
|
32
31
|
]
|
33
32
|
s.homepage = %q{http://github.com/kevTheDev/seed_dumper}
|
34
33
|
s.require_paths = ["lib"]
|
34
|
+
s.add_runtime_dependency 'rails'
|
35
35
|
s.rubygems_version = %q{1.3.7}
|
36
36
|
s.summary = %q{{Seed Dumper for Rails}}
|
37
37
|
s.test_files = [
|
38
38
|
"test/seed_dumper_test.rb",
|
39
39
|
"test/test_helper.rb"
|
40
40
|
]
|
41
|
+
s.post_install_message = %q{
|
42
|
+
================================================================================
|
41
43
|
|
44
|
+
Rails 2.x
|
45
|
+
---------
|
46
|
+
If you are using SeedDumper with Rails 2.x you will need to place the following at
|
47
|
+
the end of your Rakefile so Rubygems can load the SeedDumper tasks;
|
42
48
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
49
|
+
require 'seed_dumper'
|
50
|
+
SeedDumper.load_tasks if defined?(SeedDumper)
|
51
|
+
|
52
|
+
Rails 3.x
|
53
|
+
---------
|
54
|
+
Your work here is done!
|
55
|
+
|
56
|
+
================================================================================
|
57
|
+
}
|
50
58
|
end
|
metadata
CHANGED
@@ -1,79 +1,74 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: seed_dumper
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
prerelease:
|
6
|
-
segments:
|
7
|
-
- 0
|
8
|
-
- 1
|
9
|
-
- 0
|
10
|
-
version: 0.1.0
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.2
|
5
|
+
prerelease:
|
11
6
|
platform: ruby
|
12
|
-
authors:
|
7
|
+
authors:
|
13
8
|
- Kevin Edwards
|
14
9
|
autorequire:
|
15
10
|
bindir: bin
|
16
|
-
cert_chain:
|
17
|
-
date: 2011-
|
18
|
-
|
19
|
-
|
20
|
-
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-09-19 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rails
|
16
|
+
requirement: &70307772338880 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70307772338880
|
21
25
|
description: Create seed files for each model from your database data.
|
22
26
|
email: kev.j.edwards@gmail.com
|
23
27
|
executables: []
|
24
|
-
|
25
28
|
extensions: []
|
26
|
-
|
27
|
-
extra_rdoc_files:
|
29
|
+
extra_rdoc_files:
|
28
30
|
- README.rdoc
|
29
|
-
files:
|
31
|
+
files:
|
30
32
|
- CHANGELOG.rdoc
|
31
33
|
- MIT-LICENSE
|
32
34
|
- README.rdoc
|
33
35
|
- Rakefile
|
34
|
-
- VERSION
|
35
36
|
- lib/seed_dumper.rb
|
36
37
|
- lib/seed_dumper/railtie.rb
|
37
38
|
- lib/seed_dumper/fetcher.rb
|
38
39
|
- lib/seed_dumper/writer.rb
|
39
|
-
- lib/tasks/
|
40
|
+
- lib/tasks/seed_dumper.rake
|
40
41
|
- seed_dumper.gemspec
|
41
42
|
- test/seed_dumper_test.rb
|
42
43
|
- test/test_helper.rb
|
43
|
-
has_rdoc: true
|
44
44
|
homepage: http://github.com/kevTheDev/seed_dumper
|
45
45
|
licenses: []
|
46
|
-
|
47
|
-
|
46
|
+
post_install_message: ! "\n================================================================================\n\nRails
|
47
|
+
2.x\n---------\nIf you are using SeedDumper with Rails 2.x you will need to place
|
48
|
+
the following at \nthe end of your Rakefile so Rubygems can load the SeedDumper
|
49
|
+
tasks;\n\n require 'seed_dumper'\n SeedDumper.load_tasks if defined?(SeedDumper)\n\nRails
|
50
|
+
3.x\n---------\nYour work here is done!\n\n================================================================================\n"
|
48
51
|
rdoc_options: []
|
49
|
-
|
50
|
-
require_paths:
|
52
|
+
require_paths:
|
51
53
|
- lib
|
52
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
54
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
53
55
|
none: false
|
54
|
-
requirements:
|
55
|
-
- -
|
56
|
-
- !ruby/object:Gem::Version
|
57
|
-
|
58
|
-
|
59
|
-
- 0
|
60
|
-
version: "0"
|
61
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - ! '>='
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: '0'
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
62
61
|
none: false
|
63
|
-
requirements:
|
64
|
-
- -
|
65
|
-
- !ruby/object:Gem::Version
|
66
|
-
|
67
|
-
segments:
|
68
|
-
- 0
|
69
|
-
version: "0"
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
70
66
|
requirements: []
|
71
|
-
|
72
67
|
rubyforge_project:
|
73
|
-
rubygems_version: 1.
|
68
|
+
rubygems_version: 1.8.10
|
74
69
|
signing_key:
|
75
70
|
specification_version: 3
|
76
|
-
summary:
|
77
|
-
test_files:
|
71
|
+
summary: ! '{Seed Dumper for Rails}'
|
72
|
+
test_files:
|
78
73
|
- test/seed_dumper_test.rb
|
79
74
|
- test/test_helper.rb
|
data/VERSION
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
0.1.0
|
data/lib/tasks/seed_dump.rake
DELETED
@@ -1,10 +0,0 @@
|
|
1
|
-
namespace :db do
|
2
|
-
namespace :seed do
|
3
|
-
desc 'Dump records from the database into db/seeds.rb'
|
4
|
-
task :dump => :environment do
|
5
|
-
models = ActiveRecord::Base.send(:subclasses)
|
6
|
-
models.each { |model| SeedDumper::Writer.write_data(model.name, SeedDumper::Fetcher.fetch_data(model) ) }
|
7
|
-
end
|
8
|
-
end
|
9
|
-
end
|
10
|
-
# /db
|