seed_dumper 0.1.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/CHANGELOG.rdoc ADDED
File without changes
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Kevin Edwards
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,46 @@
1
+ = SeedDumper
2
+
3
+ Seed dumper is a simple plugin that adds a rake task named db:seed:dump.
4
+ Forked and simplified from seed_dump. If you require
5
+
6
+ It allows you to create a db/seeds.rb from your existing data in the database.
7
+ When there is no data in the database it will generate empty create statements.
8
+
9
+ It mainly exists for people who are too lazy writing create statements in db/seeds.rb themselves
10
+ and need something (+seed_dump+) to dump data from the table(s) into seeds.rb
11
+
12
+ == Example Usage
13
+
14
+ Dump all data directly to db/seeds.rb:
15
+
16
+ rake db:seed:dump
17
+
18
+
19
+ Result:
20
+
21
+ $ cat db/seeds.rb
22
+ # Autogenerated by the db:seed:dump task
23
+ # Do not hesitate to tweak this to your needs
24
+
25
+ products = Product.create([
26
+ { :category_id => 1, :description => "Long Sleeve Shirt", :name => "Long Sleeve Shirt" },
27
+ { :category_id => 3, :description => "Plain White Tee Shirt", :name => "Plain T-Shirt" }
28
+ ])
29
+
30
+ users = User.create([
31
+ { :id => 1, :password => "123456", :username => "test_1" },
32
+ { :id => 2, :password => "234567", :username => "tes2" }
33
+ ])
34
+
35
+
36
+ == Note on Patches/Pull Requests
37
+
38
+ * Fork the project.
39
+ * Make your feature addition or bug fix.
40
+ * Add tests for it. This is important so I don't break it in a
41
+ future version unintentionally.
42
+ * Commit, do not mess with rakefile, version, or history.
43
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
44
+ * Send me a pull request. Bonus points for topic branches.
45
+
46
+ Copyright (c) 2010 Kevin Edwards, released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,36 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ require 'rake/testtask'
5
+ Rake::TestTask.new(:test) do |test|
6
+ test.libs << 'lib' << 'test'
7
+ test.pattern = 'test/**/test_*.rb'
8
+ test.verbose = true
9
+ end
10
+
11
+ begin
12
+ require 'rcov/rcovtask'
13
+ Rcov::RcovTask.new do |test|
14
+ test.libs << 'test'
15
+ test.pattern = 'test/**/test_*.rb'
16
+ test.verbose = true
17
+ end
18
+ rescue LoadError
19
+ task :rcov do
20
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
21
+ end
22
+ end
23
+
24
+ task :test => :check_dependencies
25
+
26
+ task :default => :test
27
+
28
+ require 'rake/rdoctask'
29
+ Rake::RDocTask.new do |rdoc|
30
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
31
+
32
+ rdoc.rdoc_dir = 'rdoc'
33
+ rdoc.title = "seed_dump #{version}"
34
+ rdoc.rdoc_files.include('README*')
35
+ rdoc.rdoc_files.include('lib/**/*.rb')
36
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,31 @@
1
+ module SeedDumper
2
+
3
+ # Dumper
4
+ class Fetcher
5
+
6
+ def self.fetch_data(klass, options={})
7
+ model_name = klass.name
8
+
9
+ puts "Adding #{model_name.camelize} seeds."
10
+
11
+ create_hash = ''
12
+ records = klass.all
13
+
14
+ records.each_with_index do |record, index|
15
+ attr_s = [];
16
+
17
+ record.attributes.each do |key, value|
18
+ value = value.class == Time ? "\"#{value}\"" : value.inspect
19
+ attr_s.push("#{key.to_sym.inspect} => #{value}") unless key == 'id'
20
+ end
21
+
22
+ create_hash << (index > 0 ? ",\n" : "\n") << ' ' << '{ ' << attr_s.join(', ') << ' }'
23
+ end
24
+ # / records.each_with_index
25
+
26
+ return "\n#{model_name.camelize}.create([#{create_hash}\n])\n"
27
+ end
28
+
29
+ end
30
+
31
+ end
@@ -0,0 +1,12 @@
1
+ require 'seed_dumper'
2
+ require 'rails'
3
+
4
+ module SeedDumper
5
+ class Railtie < Rails::Railtie
6
+
7
+ rake_tasks do
8
+ load "tasks/seed_dumper.rake"
9
+ end
10
+
11
+ end
12
+ end
@@ -0,0 +1,19 @@
1
+ module SeedDumper
2
+
3
+ class Writer
4
+
5
+ def self.write_data(klass_name, data_string='')
6
+ FileUtils.mkdir("#{Rails.root}/db/seeds/") unless File.exist?("#{Rails.root}/db/seeds/")
7
+ seed_file_name = "#{Rails.root}/db/seeds/#{klass_name.tableize}.rb"
8
+
9
+ File.open(seed_file_name, 'w') do |f|
10
+ puts "Writing #{seed_file_name}."
11
+ f << data_string
12
+ end
13
+
14
+ end
15
+
16
+ end
17
+
18
+
19
+ end
@@ -0,0 +1,6 @@
1
+ require 'seed_dumper/fetcher'
2
+ require 'seed_dumper/writer'
3
+
4
+ module SeedDumper
5
+ require 'seedbank/railtie' if defined?(Rails) && Rails::VERSION::MAJOR >= 3
6
+ end
@@ -0,0 +1,10 @@
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
@@ -0,0 +1,50 @@
1
+ #!/usr/bin/env gem build
2
+ # encoding: utf-8
3
+
4
+ require "base64"
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "seed_dumper"
8
+ s.version = "0.1.0"
9
+ s.authors = ["Kevin Edwards"]
10
+ s.homepage = "http://github.com/kevTheDev/seed_dumper"
11
+ s.summary = "Create seed files for each model from your database data"
12
+ s.description = "#{s.summary}."
13
+ s.cert_chain = nil
14
+ s.email = "kev.j.edwards@gmail.com"
15
+ s.has_rdoc = true
16
+
17
+ s.extra_rdoc_files = [
18
+ "README.rdoc"
19
+ ]
20
+ s.files = [
21
+ "CHANGELOG.rdoc",
22
+ "MIT-LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "lib/seed_dumper.rb",
27
+ "lib/seed_dumper/railtie.rb",
28
+ "lib/seed_dumper/fetcher.rb",
29
+ "lib/seed_dumper/writer.rb",
30
+ "lib/tasks/seed_dump.rake",
31
+ "seed_dumper.gemspec"
32
+ ]
33
+ s.homepage = %q{http://github.com/kevTheDev/seed_dumper}
34
+ s.require_paths = ["lib"]
35
+ s.rubygems_version = %q{1.3.7}
36
+ s.summary = %q{{Seed Dumper for Rails}}
37
+ s.test_files = [
38
+ "test/seed_dumper_test.rb",
39
+ "test/test_helper.rb"
40
+ ]
41
+
42
+
43
+ begin
44
+ require "changelog"
45
+ rescue LoadError
46
+ warn "You have to have changelog gem installed for post install message"
47
+ else
48
+ s.post_install_message = CHANGELOG.new.version_changes
49
+ end
50
+ end
@@ -0,0 +1,8 @@
1
+ require 'test_helper'
2
+
3
+ class SeedDumperTest < ActiveSupport::TestCase
4
+ # Replace this with your real tests.
5
+ test "the truth" do
6
+ assert true
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'active_support'
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: seed_dumper
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Kevin Edwards
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain:
17
+ date: 2011-04-05 00:00:00 +01:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: Create seed files for each model from your database data.
22
+ email: kev.j.edwards@gmail.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files:
28
+ - README.rdoc
29
+ files:
30
+ - CHANGELOG.rdoc
31
+ - MIT-LICENSE
32
+ - README.rdoc
33
+ - Rakefile
34
+ - VERSION
35
+ - lib/seed_dumper.rb
36
+ - lib/seed_dumper/railtie.rb
37
+ - lib/seed_dumper/fetcher.rb
38
+ - lib/seed_dumper/writer.rb
39
+ - lib/tasks/seed_dump.rake
40
+ - seed_dumper.gemspec
41
+ - test/seed_dumper_test.rb
42
+ - test/test_helper.rb
43
+ has_rdoc: true
44
+ homepage: http://github.com/kevTheDev/seed_dumper
45
+ licenses: []
46
+
47
+ post_install_message:
48
+ rdoc_options: []
49
+
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ hash: 3
58
+ segments:
59
+ - 0
60
+ version: "0"
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ hash: 3
67
+ segments:
68
+ - 0
69
+ version: "0"
70
+ requirements: []
71
+
72
+ rubyforge_project:
73
+ rubygems_version: 1.3.7
74
+ signing_key:
75
+ specification_version: 3
76
+ summary: "{Seed Dumper for Rails}"
77
+ test_files:
78
+ - test/seed_dumper_test.rb
79
+ - test/test_helper.rb