seed_dump 0.3.4 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.rdoc CHANGED
@@ -1,5 +1,17 @@
1
1
  == Seed Dump
2
2
 
3
+ == 0.4.0 / 2012-07-28
4
+
5
+ * fix rdoc issue.
6
+ * automatically use :without_protection if created_at, updated_at are present.
7
+ * Properly dump decimal columns
8
+ * support nested models [h6y3]
9
+ * add WITHOUT_PROTECTION flag [Alexey Yurchenko]
10
+ * add utf-8 encoding comment [Alexey Yurchenko, Alexey Poimtsev, n0b0dy]
11
+ * add SKIP_CALLBACKS flag [Alexey Yurchenko]
12
+ * fixed typo in README [Andrey Ognevsky]
13
+ * add PG_SCHEMA flag [Luka Novsak]
14
+
3
15
  == 0.3.4 / 2011-07-14
4
16
 
5
17
  * Skip models that aren't Derived from ActiveRecord [iconoclast]
data/README.rdoc CHANGED
@@ -14,7 +14,7 @@ Dump all data directly to db/seeds.rb:
14
14
 
15
15
  rake db:seed:dump
16
16
 
17
- Dump only data from the users and products table and dump a maximum amount of 10 records:
17
+ Dump only data from the users and products table and dump a maximum amount of 2 records:
18
18
 
19
19
  $ rake db:seed:dump MODELS=User,Product LIMIT=2
20
20
 
@@ -78,5 +78,17 @@ NO_DATA:
78
78
  WITH_ID:
79
79
  Inlcude the +:id+ in the create options
80
80
 
81
+ SKIP_CALLBACKS:
82
+ Deactivate callbacks while importing.
83
+
84
+ PG_SCHEMA:
85
+ Postgres schema support
86
+
87
+ WITHOUT_PROTECTION:
88
+ Skip protection for columns that are protected by default. (Automatically activated if such columns are present)
89
+
90
+ MODEL_DIR:
91
+ Specify an alternative model dir
92
+
81
93
 
82
94
  Copyright (c) 2010 Rob Halff, released under the MIT license
data/Rakefile CHANGED
@@ -40,7 +40,7 @@ task :test => :check_dependencies
40
40
 
41
41
  task :default => :test
42
42
 
43
- require 'rake/rdoctask'
43
+ require 'rdoc/task'
44
44
  Rake::RDocTask.new do |rdoc|
45
45
  version = File.exist?('VERSION') ? File.read('VERSION') : ""
46
46
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.4
1
+ 0.4.0
@@ -1,3 +1,5 @@
1
+ require "true"
2
+
1
3
  module SeedDump
2
4
  class Perform
3
5
 
@@ -9,58 +11,76 @@ module SeedDump
9
11
  @seed_rb = ""
10
12
  @id_set_string = ""
11
13
  @verbose = true
12
- @model_dir = 'app/models/*.rb'
14
+ @model_dir = 'app/models/**/*.rb'
13
15
  end
14
16
 
15
17
  def setup(env)
16
18
  # config
17
- @opts['with_id'] = !env["WITH_ID"].nil?
18
- @opts['no-data'] = !env['NO_DATA'].nil?
19
+ @opts['debug'] = env["DEBUG"].true?
20
+ @opts['with_id'] = env["WITH_ID"].true?
21
+ @opts['no-data'] = env['NO_DATA'].true?
22
+ @opts['without_protection'] = env['WITHOUT_PROTECTION'].true?
23
+ @opts['skip_callbacks'] = env['SKIP_CALLBACKS'].true?
19
24
  @opts['models'] = env['MODELS'] || (env['MODEL'] ? env['MODEL'] : "")
20
25
  @opts['file'] = env['FILE'] || "#{Rails.root}/db/seeds.rb"
21
- @opts['append'] = (!env['APPEND'].nil? && File.exists?(@opts['file']) )
26
+ @opts['append'] = (env['APPEND'].true? && File.exists?(@opts['file']) )
22
27
  @ar_options = env['LIMIT'].to_i > 0 ? { :limit => env['LIMIT'].to_i } : {}
23
28
  @indent = " " * (env['INDENT'].nil? ? 2 : env['INDENT'].to_i)
24
29
  @opts['models'] = @opts['models'].split(',').collect {|x| x.underscore.singularize.camelize }
30
+ @opts['schema'] = env['PG_SCHEMA']
31
+ @opts['model_dir'] = env['MODEL_DIR'] || @model_dir
25
32
  end
26
33
 
27
34
  def loadModels
28
- Dir[@model_dir].sort.each do |f|
29
- model = File.basename(f, '.*').camelize
35
+ puts "Searching in #{@opts['model_dir']} for models" if @opts['debug']
36
+ Dir[@opts['model_dir']].sort.each do |f|
37
+ puts "Processing file #{f}" if @opts['debug']
38
+ # parse file name and path leading up to file name and assume the path is a module
39
+ f =~ /models\/(.*).rb/
40
+ # split path by /, camelize the constituents, and then reform as a formal class name
41
+ model = $1.split("/").map {|x| x.camelize}.join("::")
42
+ puts "Detected model #{model}" if @opts['debug']
30
43
  @models.push model if @opts['models'].include?(model) || @opts['models'].empty?
31
44
  end
32
45
  end
33
46
 
47
+ def models
48
+ @models
49
+ end
50
+
34
51
  def dumpAttribute(a_s,r,k,v)
35
- v = attribute_for_inspect(r,k)
36
- if k == 'id' && @opts['with_id']
37
- @id_set_string = "{ |c| c.#{k} = #{v} }.save"
52
+ if v.is_a?(BigDecimal)
53
+ v = v.to_s
38
54
  else
39
- a_s.push("#{k.to_sym.inspect} => #{v}") unless k == 'id' && !@opts['with_id']
55
+ v = attribute_for_inspect(r,k)
56
+ end
57
+
58
+ unless k == 'id' && !@opts['with_id']
59
+ @opts['without_protection'] = true if ["id", "created_at", "updated_at"].include?(k)
60
+ a_s.push("#{k.to_sym.inspect} => #{v}")
40
61
  end
41
62
  end
42
63
 
43
64
  def dumpModel(model)
44
65
  @id_set_string = ''
45
66
  create_hash = ""
67
+ options = ''
46
68
  rows = []
47
69
  arr = []
48
70
  arr = model.find(:all, @ar_options) unless @opts['no-data']
49
71
  arr = arr.empty? ? [model.new] : arr
72
+
50
73
  arr.each_with_index { |r,i|
51
74
  attr_s = [];
52
75
  r.attributes.each { |k,v| dumpAttribute(attr_s,r,k,v) }
53
- if @id_set_string.empty?
54
- rows.push "#{@indent}{ " << attr_s.join(', ') << " }"
55
- else
56
- create_hash << "\n#{model}.create" << '( ' << attr_s.join(', ') << ' )' << @id_set_string
57
- end
76
+ rows.push "#{@indent}{ " << attr_s.join(', ') << " }"
58
77
  }
59
- if @id_set_string.empty?
60
- "\n#{model}.create([\n" << rows.join(",\n") << "\n])\n"
61
- else
62
- create_hash
78
+
79
+ if @opts['without_protection']
80
+ options = ', :without_protection => true '
63
81
  end
82
+
83
+ "\n#{model}.create([\n" << rows.join(",\n") << "\n]#{options})\n"
64
84
  end
65
85
 
66
86
  def dumpModels
@@ -69,15 +89,24 @@ module SeedDump
69
89
  m = model.constantize
70
90
  if m.ancestors.include?(ActiveRecord::Base)
71
91
  puts "Adding #{model} seeds." if @verbose
92
+
93
+ if @opts['skip_callbacks']
94
+ @seed_rb << "#{model}.reset_callbacks :save\n"
95
+ @seed_rb << "#{model}.reset_callbacks :create\n"
96
+ puts "Callbacks are disabled." if @verbose
97
+ end
98
+
72
99
  @seed_rb << dumpModel(m) << "\n\n"
73
100
  else
74
101
  puts "Skipping non-ActiveRecord model #{model}..." if @verbose
75
102
  end
103
+ puts "Protection is disabled." if @verbose && @opts['without_protection']
76
104
  end
77
105
  end
78
106
 
79
107
  def writeFile
80
108
  File.open(@opts['file'], (@opts['append'] ? "a" : "w")) { |f|
109
+ f << "# encoding: utf-8\n"
81
110
  f << "# Autogenerated by the db:seed:dump task\n# Do not hesitate to tweak this to your needs\n" unless @opts['append']
82
111
  f << "#{@seed_rb}"
83
112
  }
@@ -96,10 +125,17 @@ module SeedDump
96
125
  end
97
126
  end
98
127
 
128
+ def setSearchPath(path, append_public=true)
129
+ path_parts = [path.to_s, ('public' if append_public)].compact
130
+ ActiveRecord::Base.connection.schema_search_path = path_parts.join(',')
131
+ end
132
+
99
133
  def run(env)
100
134
 
101
135
  setup env
102
136
 
137
+ setSearchPath @opts['schema'] if @opts['schema']
138
+
103
139
  loadModels
104
140
 
105
141
  puts "Appending seeds to #{@opts['file']}." if @opts['append']
data/lib/true.rb ADDED
@@ -0,0 +1,9 @@
1
+ # http://veerasundaravel.wordpress.com/2010/10/26/string-to-boolean-conversion-in-ruby/
2
+ class Object
3
+ def true? # to boolean
4
+ return false if self.nil?
5
+ return true if self == true || self =~ (/(true|t|yes|y|1)$/i)
6
+ return false if self == false || self.nil? || self =~ (/(false|f|no|n|0)$/i)
7
+ raise ArgumentError.new('invalid value for Boolean: "#{self}"')
8
+ end
9
+ end
data/seed_dump.gemspec CHANGED
@@ -4,14 +4,14 @@
4
4
  # -*- encoding: utf-8 -*-
5
5
 
6
6
  Gem::Specification.new do |s|
7
- s.name = %q{seed_dump}
8
- s.version = "0.3.4"
7
+ s.name = "seed_dump"
8
+ s.version = "0.4.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Rob Halff"]
12
- s.date = %q{2011-07-14}
13
- s.description = %q{Dump (parts) of your database to db/seeds.rb to get a headstart creating a meaningful seeds.rb file}
14
- s.email = %q{rob.halff@gmail.com}
12
+ s.date = "2012-07-28"
13
+ s.description = "Dump (parts) of your database to db/seeds.rb to get a headstart creating a meaningful seeds.rb file"
14
+ s.email = "rob.halff@gmail.com"
15
15
  s.extra_rdoc_files = [
16
16
  "README.rdoc"
17
17
  ]
@@ -21,20 +21,22 @@ Gem::Specification.new do |s|
21
21
  "README.rdoc",
22
22
  "Rakefile",
23
23
  "VERSION",
24
- "contents.yml",
25
24
  "lib/seed_dump.rb",
26
25
  "lib/seed_dump/perform.rb",
27
26
  "lib/seed_dump/railtie.rb",
28
27
  "lib/tasks/seed_dump.rake",
28
+ "lib/true.rb",
29
29
  "seed_dump.gemspec",
30
30
  "test/fixtures/samples.yml",
31
+ "test/models/nested/sample.rb",
32
+ "test/models/sample.rb",
31
33
  "test/seed_dump_test.rb",
32
34
  "test/test_helper.rb"
33
35
  ]
34
- s.homepage = %q{http://github.com/rhalff/seed_dump}
36
+ s.homepage = "http://github.com/rhalff/seed_dump"
35
37
  s.require_paths = ["lib"]
36
- s.rubygems_version = %q{1.6.2}
37
- s.summary = %q{{Seed Dumper for Rails}}
38
+ s.rubygems_version = "1.8.24"
39
+ s.summary = "{Seed Dumper for Rails}"
38
40
 
39
41
  if s.respond_to? :specification_version then
40
42
  s.specification_version = 3
@@ -1,10 +1,54 @@
1
- # lo & behold! I am a YAML comment!
2
- david:
3
- name: David Heinemeier Hansson
4
- birthday: 1979-10-15
5
- profession: Systems development
6
-
7
- steve:
8
- name: Steve Ross Kellock
9
- birthday: 1974-09-27
10
- profession: guy with keyboard
1
+ # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html
2
+
3
+ bharathiyar:
4
+ string: Poetry of Subramaniya Bharathiyar
5
+ text: யாமறிந்த மொழிகளிலே தமிழ்மொழி போல் இனிதாவது எங்கும் காணோம்,
6
+ பாமரராய் விலங்குகளாய், உலகனைத்தும் இகழ்ச்சிசொலப் பான்மை கெட்டு,
7
+ நாமமது தமிழரெனக் கொண்டு இங்கு வாழ்ந்திடுதல் நன்றோ? சொல்லீர்!
8
+ தேமதுரத் தமிழோசை உலகமெலாம் பரவும்வகை செய்தல் வேண்டும்.
9
+ integer: 1
10
+ float: 3.3333
11
+ decimal: 9.99
12
+ datetime: 1902-07-21 17:01:31
13
+ timestamp: 1900-07-21 17:01:31
14
+ time: 1903-07-21 17:01:31
15
+ date: 1904-07-21
16
+ binary:
17
+ boolean: false
18
+
19
+ pushkin:
20
+ string: Pushkin's Bronze Horseman
21
+ text: На берегу пустынных волн
22
+ Стоял он, дум великих полн,
23
+ И вдаль глядел. Пред ним широко
24
+ Река неслася; бедный чёлн
25
+ По ней стремился одиноко.
26
+ По мшистым, топким берегам
27
+ Чернели избы здесь и там,
28
+ Приют убогого чухонца;
29
+ И лес, неведомый лучам
30
+ В тумане спрятанного солнца,
31
+ Кругом шумел.
32
+ integer: 1
33
+ float: 1.5
34
+ decimal: 9.99
35
+ datetime: 2012-07-21 17:01:31
36
+ timestamp: 2012-07-21 17:01:31
37
+ time: 2012-07-21 17:01:31
38
+ date: 2012-07-21
39
+ binary:
40
+ boolean: false
41
+ poem:
42
+ string: Anglo-Saxon Rune Poem
43
+ text: ᚠᛇᚻ᛫ᛒᛦᚦ᛫ᚠᚱᚩᚠᚢᚱ᛫ᚠᛁᚱᚪ᛫ᚷᛖᚻᚹᛦᛚᚳᚢᛗ
44
+ ᛋᚳᛖᚪᛚ᛫ᚦᛖᚪᚻ᛫ᛗᚪᚾᚾᚪ᛫ᚷᛖᚻᚹᛦᛚᚳ᛫ᛗᛁᚳᛚᚢᚾ᛫ᚻᛦᛏ᛫ᛞᚫᛚᚪᚾ
45
+ ᚷᛁᚠ᛫ᚻᛖ᛫ᚹᛁᛚᛖ᛫ᚠᚩᚱ᛫ᛞᚱᛁᚻᛏᚾᛖ᛫ᛞᚩᛗᛖᛋ᛫ᚻᛚᛇᛏᚪᚾ
46
+ integer: 1
47
+ float: 1.9
48
+ decimal: 4.55
49
+ datetime: 2012-07-21 17:01:31
50
+ timestamp: 2012-07-21 17:01:31
51
+ time: 2012-07-21 17:01:31
52
+ date: 2012-07-21
53
+ binary:
54
+ boolean: false
@@ -0,0 +1,3 @@
1
+ class Nested::Sample < ActiveRecord::Base
2
+ attr_accessible :name
3
+ end
@@ -0,0 +1,3 @@
1
+ class Sample < ActiveRecord::Base
2
+ attr_accessible :binary, :boolean, :date, :datetime, :decimal, :float, :integer, :string, :text, :time, :timestamp
3
+ end
@@ -1,8 +1,30 @@
1
1
  require 'test_helper'
2
+ require "seed_dump/perform"
2
3
 
3
4
  class SeedDumpTest < ActiveSupport::TestCase
4
- # Replace this with your real tests.
5
- test "test WITH_ID" do
6
- assert false
5
+
6
+ setup do
7
+ @sd = SeedDump::Perform.new
8
+ # universial options for every test
9
+ @env = {
10
+ "MODEL_DIR" => Dir.pwd + '/test/models/**.rb',
11
+ "FILE" => Dir.pwd + '/test/db/seeds.rb',
12
+ "DEBUG" => true
13
+ }
14
+ end
15
+
16
+ test "load sample model" do
17
+ @env['MODEL_DIR'] = Dir.pwd + '/test/models/*.rb'
18
+ @sd.setup @env
19
+ @sd.loadModels
20
+ assert_equal ["Sample"], @sd.models
7
21
  end
22
+
23
+ test "support nested models" do
24
+ @env['MODEL_DIR'] = Dir.pwd + '/test/models/**/*.rb'
25
+ @sd.setup @env
26
+ @sd.loadModels
27
+ assert_equal ["Nested::Sample", "Sample"], @sd.models
28
+ end
29
+
8
30
  end
data/test/test_helper.rb CHANGED
@@ -1,3 +1,30 @@
1
+ $LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'lib')
1
2
  require 'rubygems'
2
3
  require 'test/unit'
3
4
  require 'active_support'
5
+ require 'active_record'
6
+
7
+ #require 'your_thing'
8
+
9
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
10
+
11
+ ActiveRecord::Schema.define(:version => 1) do
12
+ create_table "samples", :force => true do |t|
13
+ t.string "string"
14
+ t.text "text"
15
+ t.integer "integer"
16
+ t.float "float"
17
+ t.decimal "decimal"
18
+ t.datetime "datetime"
19
+ t.datetime "timestamp"
20
+ t.time "time"
21
+ t.date "date"
22
+ t.binary "binary"
23
+ t.boolean "boolean"
24
+ t.datetime "created_at", :null => false
25
+ t.datetime "updated_at", :null => false
26
+ end
27
+ end
28
+
29
+ #class Sample < ActiveRecord::Base
30
+ #end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: seed_dump
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.4
4
+ version: 0.4.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,8 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-07-14 00:00:00.000000000 +02:00
13
- default_executable:
12
+ date: 2012-07-28 00:00:00.000000000 Z
14
13
  dependencies: []
15
14
  description: Dump (parts) of your database to db/seeds.rb to get a headstart creating
16
15
  a meaningful seeds.rb file
@@ -25,16 +24,17 @@ files:
25
24
  - README.rdoc
26
25
  - Rakefile
27
26
  - VERSION
28
- - contents.yml
29
27
  - lib/seed_dump.rb
30
28
  - lib/seed_dump/perform.rb
31
29
  - lib/seed_dump/railtie.rb
32
30
  - lib/tasks/seed_dump.rake
31
+ - lib/true.rb
33
32
  - seed_dump.gemspec
34
33
  - test/fixtures/samples.yml
34
+ - test/models/nested/sample.rb
35
+ - test/models/sample.rb
35
36
  - test/seed_dump_test.rb
36
37
  - test/test_helper.rb
37
- has_rdoc: true
38
38
  homepage: http://github.com/rhalff/seed_dump
39
39
  licenses: []
40
40
  post_install_message:
@@ -55,7 +55,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
55
55
  version: '0'
56
56
  requirements: []
57
57
  rubyforge_project:
58
- rubygems_version: 1.6.2
58
+ rubygems_version: 1.8.24
59
59
  signing_key:
60
60
  specification_version: 3
61
61
  summary: ! '{Seed Dumper for Rails}'
data/contents.yml DELETED
@@ -1,22 +0,0 @@
1
- # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
2
- article1:
3
- id: 1
4
- title: Article one
5
- body: body
6
- extended: extended content
7
- created_at: 2011-06-01 00:00:01
8
- updated_at: 2011-06-01 00:00:02
9
- some_id: 1
10
- author: Johnny Johnson
11
- published: true
12
-
13
- article2:
14
- id: 2
15
- title: This is a very long title and it should not be truncated
16
- body: body
17
- extended: extended content
18
- created_at: 2010-06-01 20:00:01
19
- updated_at: 2010-06-01 20:00:02
20
- some_id: 2
21
- author: Bob
22
- published: true