seed_dump 0.4.0 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.rdoc CHANGED
@@ -1,5 +1,12 @@
1
1
  == Seed Dump
2
2
 
3
+ == 0.4.1 / 2012-08-01
4
+
5
+ * include TIMESTAMPS by default
6
+ * add TIMESTAMP flag and checks against attr_accessible [limratana].
7
+ * modifications for better testing
8
+ * make MODEL_DIR actually work
9
+
3
10
  == 0.4.0 / 2012-07-28
4
11
 
5
12
  * fix rdoc issue.
data/README.rdoc CHANGED
@@ -78,6 +78,9 @@ NO_DATA:
78
78
  WITH_ID:
79
79
  Inlcude the +:id+ in the create options
80
80
 
81
+ TIMESTAMPS:
82
+ Include the :created_by and :updated_by timestamps (default)
83
+
81
84
  SKIP_CALLBACKS:
82
85
  Deactivate callbacks while importing.
83
86
 
@@ -85,7 +88,8 @@ PG_SCHEMA:
85
88
  Postgres schema support
86
89
 
87
90
  WITHOUT_PROTECTION:
88
- Skip protection for columns that are protected by default. (Automatically activated if such columns are present)
91
+ Skip protection for columns that are protected by default.
92
+ Note : WITH_ID and TIMESTAMPS automatically override protection
89
93
 
90
94
  MODEL_DIR:
91
95
  Specify an alternative model dir
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.0
1
+ 0.4.1
data/lib/clip.rb ADDED
@@ -0,0 +1,6 @@
1
+ # by digitalross: http://stackoverflow.com/questions/1604305/all-but-last-element-of-ruby-array
2
+ class Array
3
+ def clip n=1
4
+ take size - n
5
+ end
6
+ end
@@ -1,4 +1,5 @@
1
1
  require "true"
2
+ require "clip"
2
3
 
3
4
  module SeedDump
4
5
  class Perform
@@ -8,18 +9,20 @@ module SeedDump
8
9
  @ar_options = {}
9
10
  @indent = ""
10
11
  @models = []
12
+ @last_record = []
11
13
  @seed_rb = ""
12
14
  @id_set_string = ""
13
- @verbose = true
14
15
  @model_dir = 'app/models/**/*.rb'
15
16
  end
16
17
 
17
18
  def setup(env)
18
19
  # config
20
+ @opts['verbose'] = env["VERBOSE"].true? || env['VERBOSE'].nil?
19
21
  @opts['debug'] = env["DEBUG"].true?
20
22
  @opts['with_id'] = env["WITH_ID"].true?
23
+ @opts['timestamps'] = env["TIMESTAMPS"].true? || env["TIMESTAMPS"].nil?
21
24
  @opts['no-data'] = env['NO_DATA'].true?
22
- @opts['without_protection'] = env['WITHOUT_PROTECTION'].true?
25
+ @opts['without_protection'] = env['WITHOUT_PROTECTION'].true? || (env['WITHOUT_PROTECTION'].nil? && @opts['timestamps'])
23
26
  @opts['skip_callbacks'] = env['SKIP_CALLBACKS'].true?
24
27
  @opts['models'] = env['MODELS'] || (env['MODEL'] ? env['MODEL'] : "")
25
28
  @opts['file'] = env['FILE'] || "#{Rails.root}/db/seeds.rb"
@@ -33,12 +36,17 @@ module SeedDump
33
36
 
34
37
  def loadModels
35
38
  puts "Searching in #{@opts['model_dir']} for models" if @opts['debug']
36
- Dir[@opts['model_dir']].sort.each do |f|
39
+ Dir[Dir.pwd + '/' + @opts['model_dir']].sort.each do |f|
37
40
  puts "Processing file #{f}" if @opts['debug']
38
41
  # parse file name and path leading up to file name and assume the path is a module
39
42
  f =~ /models\/(.*).rb/
40
43
  # split path by /, camelize the constituents, and then reform as a formal class name
41
- model = $1.split("/").map {|x| x.camelize}.join("::")
44
+ parts = $1.split("/").map {|x| x.camelize}
45
+ # Initialize nested model namespaces
46
+ parts.clip.inject(Object) { |x, y| x.const_set(y, Module.new) }
47
+ model = parts.join("::")
48
+ require f
49
+
42
50
  puts "Detected model #{model}" if @opts['debug']
43
51
  @models.push model if @opts['models'].include?(model) || @opts['models'].empty?
44
52
  end
@@ -48,6 +56,10 @@ module SeedDump
48
56
  @models
49
57
  end
50
58
 
59
+ def last_record
60
+ @last_record
61
+ end
62
+
51
63
  def dumpAttribute(a_s,r,k,v)
52
64
  if v.is_a?(BigDecimal)
53
65
  v = v.to_s
@@ -56,13 +68,15 @@ module SeedDump
56
68
  end
57
69
 
58
70
  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}")
71
+ if (!(k == 'created_at' || k == 'updated_at') || @opts['timestamps'])
72
+ a_s.push("#{k.to_sym.inspect} => #{v}")
73
+ end
61
74
  end
62
75
  end
63
76
 
64
77
  def dumpModel(model)
65
78
  @id_set_string = ''
79
+ @last_record = []
66
80
  create_hash = ""
67
81
  options = ''
68
82
  rows = []
@@ -72,7 +86,12 @@ module SeedDump
72
86
 
73
87
  arr.each_with_index { |r,i|
74
88
  attr_s = [];
75
- r.attributes.each { |k,v| dumpAttribute(attr_s,r,k,v) }
89
+ r.attributes.each do |k,v|
90
+ if ((model.attr_accessible[:default].include? k) || @opts['without_protection'] || @opts['with_id'])
91
+ dumpAttribute(attr_s,r,k,v)
92
+ @last_record.push k
93
+ end
94
+ end
76
95
  rows.push "#{@indent}{ " << attr_s.join(', ') << " }"
77
96
  }
78
97
 
@@ -88,19 +107,18 @@ module SeedDump
88
107
  @models.sort.each do |model|
89
108
  m = model.constantize
90
109
  if m.ancestors.include?(ActiveRecord::Base)
91
- puts "Adding #{model} seeds." if @verbose
110
+ puts "Adding #{model} seeds." if @opts['verbose']
92
111
 
93
112
  if @opts['skip_callbacks']
94
113
  @seed_rb << "#{model}.reset_callbacks :save\n"
95
114
  @seed_rb << "#{model}.reset_callbacks :create\n"
96
- puts "Callbacks are disabled." if @verbose
115
+ puts "Callbacks are disabled." if @opts['verbose']
97
116
  end
98
117
 
99
118
  @seed_rb << dumpModel(m) << "\n\n"
100
119
  else
101
- puts "Skipping non-ActiveRecord model #{model}..." if @verbose
120
+ puts "Skipping non-ActiveRecord model #{model}..." if @opts['verbose']
102
121
  end
103
- puts "Protection is disabled." if @verbose && @opts['without_protection']
104
122
  end
105
123
  end
106
124
 
@@ -134,6 +152,8 @@ module SeedDump
134
152
 
135
153
  setup env
136
154
 
155
+ puts "Protection is disabled." if @opts['verbose'] && @opts['without_protection']
156
+
137
157
  setSearchPath @opts['schema'] if @opts['schema']
138
158
 
139
159
  loadModels
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.0"
8
+ s.version = "0.4.1"
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 = "2012-07-28"
12
+ s.date = "2012-08-01"
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 = [
@@ -21,6 +21,7 @@ Gem::Specification.new do |s|
21
21
  "README.rdoc",
22
22
  "Rakefile",
23
23
  "VERSION",
24
+ "lib/clip.rb",
24
25
  "lib/seed_dump.rb",
25
26
  "lib/seed_dump/perform.rb",
26
27
  "lib/seed_dump/railtie.rb",
@@ -7,24 +7,53 @@ class SeedDumpTest < ActiveSupport::TestCase
7
7
  @sd = SeedDump::Perform.new
8
8
  # universial options for every test
9
9
  @env = {
10
- "MODEL_DIR" => Dir.pwd + '/test/models/**.rb',
10
+ "MODEL_DIR" => 'test/models/**.rb',
11
11
  "FILE" => Dir.pwd + '/test/db/seeds.rb',
12
- "DEBUG" => true
12
+ "VERBOSE" => false,
13
+ "DEBUG" => false
13
14
  }
14
15
  end
15
16
 
16
17
  test "load sample model" do
17
- @env['MODEL_DIR'] = Dir.pwd + '/test/models/*.rb'
18
+ @env['MODEL_DIR'] = 'test/models/*.rb'
18
19
  @sd.setup @env
19
20
  @sd.loadModels
20
21
  assert_equal ["Sample"], @sd.models
21
22
  end
22
23
 
23
24
  test "support nested models" do
24
- @env['MODEL_DIR'] = Dir.pwd + '/test/models/**/*.rb'
25
+ @env['MODEL_DIR'] = 'test/models/**/*.rb'
25
26
  @sd.setup @env
26
27
  @sd.loadModels
27
28
  assert_equal ["Nested::Sample", "Sample"], @sd.models
28
29
  end
29
30
 
31
+ test "without timestamps" do
32
+ @env['MODEL_DIR'] = 'test/models/*.rb'
33
+ @env['TIMESTAMPS'] = false
34
+ @sd.setup @env
35
+ @sd.loadModels
36
+ @sd.dumpModels
37
+ assert !@sd.last_record.include?("created_at"), "Should not include created_at if timestamps are off"
38
+ end
39
+
40
+ test "with timestamps" do
41
+ @env['MODEL_DIR'] = 'test/models/*.rb'
42
+ @env['TIMESTAMPS'] = true
43
+ @sd.setup @env
44
+ @sd.loadModels
45
+ @sd.dumpModels
46
+ assert @sd.last_record.include?("created_at"), "Must include created_at if timestamps are desired"
47
+ end
48
+
49
+ test "with id" do
50
+ @env['MODEL_DIR'] = 'test/models/*.rb'
51
+ @env['WITH_ID'] = true
52
+ @sd.setup @env
53
+ @sd.loadModels
54
+ @sd.dumpModels
55
+ assert @sd.last_record.include?("id"), "WITH_ID must include id"
56
+ end
57
+
58
+
30
59
  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.0
4
+ version: 0.4.1
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: 2012-07-28 00:00:00.000000000 Z
12
+ date: 2012-08-01 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
@@ -24,6 +24,7 @@ files:
24
24
  - README.rdoc
25
25
  - Rakefile
26
26
  - VERSION
27
+ - lib/clip.rb
27
28
  - lib/seed_dump.rb
28
29
  - lib/seed_dump/perform.rb
29
30
  - lib/seed_dump/railtie.rb