seed_dump 0.4.3 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -42,7 +42,7 @@ Use another output file instead of db/seeds.rb
42
42
 
43
43
  rake db:seed:dump FILE=db/categories.rb
44
44
 
45
- By default the :id column will not be added to the generated Create statements
45
+ By default the :id column will not be added to the generated create statements.
46
46
  If you do want the :id to be included use WITH_ID:
47
47
 
48
48
  rake db:seed:dump WITH_ID=1
@@ -54,15 +54,22 @@ It's up to you to edit these and change the values into something meaningful:
54
54
 
55
55
  rake db:seed:dump MODEL=User NO_DATA=1 APPEND=true
56
56
 
57
+ If you want the dump to use `create!` rather than `create`:
58
+
59
+ rake db:seed:dump CREATE_METHOD='create!'
60
+
57
61
  Here is a full example using all of the options above:
58
62
 
59
- rake db:seed:dump MODELS=Category LIMIT=10 APPEND=true FILE=db/categories.rb WITH_ID=1 NO_DATA=1
63
+ rake db:seed:dump MODELS=Category LIMIT=10 APPEND=true FILE=db/categories.rb WITH_ID=1 NO_DATA=1 CREATE_METHOD='create!'
60
64
 
61
65
  == All environment variables
62
66
 
63
67
  APPEND:
64
68
  Append the data to db/seeds.rb instead of overwriting it.
65
69
 
70
+ CREATE_METHOD:
71
+ Use the specified create method rather than 'create' (the default). Note: if you are using bash and want to use 'create!', be sure to use single quotes on the command line (i.e. CREATE_METHOD='create!').
72
+
66
73
  FILE:
67
74
  Use a different output file, default: db/seeds.rb
68
75
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.3
1
+ 0.5.0
@@ -6,11 +6,11 @@ module SeedDump
6
6
 
7
7
  def initialize
8
8
  @opts = {}
9
- @ar_options = {}
9
+ @ar_options = {}
10
10
  @indent = ""
11
11
  @models = []
12
12
  @last_record = []
13
- @seed_rb = ""
13
+ @seed_rb = ""
14
14
  @id_set_string = ""
15
15
  @model_dir = 'app/models/**/*.rb'
16
16
  end
@@ -33,6 +33,7 @@ module SeedDump
33
33
  @opts['models'] = @opts['models'].split(',').collect {|x| x.underscore.singularize.camelize }
34
34
  @opts['schema'] = env['PG_SCHEMA']
35
35
  @opts['model_dir'] = env['MODEL_DIR'] || @model_dir
36
+ @opts['create_method'] = env['CREATE_METHOD'] || 'create'
36
37
  end
37
38
 
38
39
  def loadModels
@@ -57,7 +58,7 @@ module SeedDump
57
58
  require f
58
59
 
59
60
  puts "Detected model #{model}" if @opts['debug']
60
- @models.push model if @opts['models'].include?(model) || @opts['models'].empty?
61
+ @models.push model if @opts['models'].include?(model) || @opts['models'].empty?
61
62
  end
62
63
  end
63
64
 
@@ -74,13 +75,13 @@ module SeedDump
74
75
  v = v.to_s
75
76
  else
76
77
  v = attribute_for_inspect(r,k)
77
- end
78
+ end
78
79
 
79
80
  unless k == 'id' && !@opts['with_id']
80
81
  if (!(k == 'created_at' || k == 'updated_at') || @opts['timestamps'])
81
82
  a_s.push("#{k.to_sym.inspect} => #{v}")
82
83
  end
83
- end
84
+ end
84
85
  end
85
86
 
86
87
  def dumpModel(model)
@@ -91,34 +92,34 @@ module SeedDump
91
92
  rows = []
92
93
  arr = []
93
94
  arr = model.find(:all, @ar_options) unless @opts['no-data']
94
- arr = arr.empty? ? [model.new] : arr
95
+ arr = arr.empty? ? [model.new] : arr
95
96
 
96
- arr.each_with_index { |r,i|
97
+ arr.each_with_index { |r,i|
97
98
  attr_s = [];
98
99
  r.attributes.each do |k,v|
99
100
  if ((model.attr_accessible[:default].include? k) || @opts['without_protection'] || @opts['with_id'])
100
101
  dumpAttribute(attr_s,r,k,v)
101
- @last_record.push k
102
+ @last_record.push k
102
103
  end
103
104
  end
104
105
  rows.push "#{@indent}{ " << attr_s.join(', ') << " }"
105
- }
106
+ }
106
107
 
107
108
  if @opts['without_protection']
108
109
  options = ', :without_protection => true '
109
110
  end
110
-
111
+
111
112
  if @opts['max']
112
113
  splited_rows = rows.each_slice(@opts['max']).to_a
113
114
  maxsarr = []
114
115
  splited_rows.each do |sr|
115
- maxsarr << "\n#{model}.create([\n" << sr.join(",\n") << "\n]#{options})\n"
116
- end
116
+ maxsarr << "\n#{model}.#{@opts['create_method']}([\n" << sr.join(",\n") << "\n]#{options})\n"
117
+ end
117
118
  maxsarr.join('')
118
119
  else
119
- "\n#{model}.create([\n" << rows.join(",\n") << "\n]#{options})\n"
120
+ "\n#{model}.#{@opts['create_method']}([\n" << rows.join(",\n") << "\n]#{options})\n"
120
121
  end
121
-
122
+
122
123
  end
123
124
 
124
125
  def dumpModels
@@ -152,7 +153,7 @@ module SeedDump
152
153
  #override the rails version of this function to NOT truncate strings
153
154
  def attribute_for_inspect(r,k)
154
155
  value = r.attributes[k]
155
-
156
+
156
157
  if value.is_a?(String) && value.length > 50
157
158
  "#{value}".inspect
158
159
  elsif value.is_a?(Date) || value.is_a?(Time)
data/seed_dump.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "seed_dump"
8
- s.version = "0.4.3"
8
+ s.version = "0.5.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 = "2013-05-11"
12
+ s.date = "2013-06-12"
13
13
  s.description = "Dump (parts) of your database to db/seeds.rb to get a headstart creating a meaningful seeds.rb file"
14
14
  s.email = "rob.halff@gmail.com"
15
15
  s.extra_rdoc_files = [
@@ -10,20 +10,20 @@ class SeedDumpTest < ActiveSupport::TestCase
10
10
  "MODEL_DIR" => 'test/models/**.rb',
11
11
  "FILE" => Dir.pwd + '/test/db/seeds.rb',
12
12
  "VERBOSE" => false,
13
- "DEBUG" => false
13
+ "DEBUG" => false
14
14
  }
15
15
  end
16
16
 
17
17
  test "load sample model" do
18
18
  @env['MODEL_DIR'] = 'test/models/*.rb'
19
- @sd.setup @env
19
+ @sd.setup @env
20
20
  @sd.loadModels
21
21
  assert_equal ["AbstractSample", "ChildSample", "Sample"], @sd.models
22
22
  end
23
23
 
24
24
  test "support nested models" do
25
25
  @env['MODEL_DIR'] = 'test/models/**/*.rb'
26
- @sd.setup @env
26
+ @sd.setup @env
27
27
  @sd.loadModels
28
28
  assert_equal ["AbstractSample", "ChildSample", "Nested::Sample", "Sample"], @sd.models
29
29
  end
@@ -65,5 +65,12 @@ class SeedDumpTest < ActiveSupport::TestCase
65
65
  assert_equal [], @sd.last_record
66
66
  end
67
67
 
68
-
68
+ test "create method" do
69
+ @env['MODEL_DIR'] = 'test/models/*.rb'
70
+ @env['CREATE_METHOD'] = 'create!'
71
+ @sd.setup @env
72
+ @sd.loadModels
73
+ @sd.dumpModels
74
+ assert @sd.instance_variable_get(:@seed_rb) =~ /create!/, 'CREATE_METHOD must specify the creation method'
75
+ end
69
76
  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.4.3
4
+ version: 0.5.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-05-11 00:00:00.000000000 Z
12
+ date: 2013-06-12 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Dump (parts) of your database to db/seeds.rb to get a headstart creating
15
15
  a meaningful seeds.rb file