legacy_data 0.1.5 → 0.1.6
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/VERSION +1 -1
- data/generators/models_from_tables/models_from_tables_generator.rb +65 -0
- data/legacy_data.gemspec +5 -3
- data/lib/legacy_data/schema.rb +1 -1
- data/spec/expected/factories.rb +5 -0
- data/spec/functional/models_from_tables_spec.rb +1 -0
- data/spec/legacy_data/schema_spec.rb +1 -1
- data/spec/models_from_tables_generator_spec.rb +15 -6
- data/spec/spec_helper.rb +2 -0
- metadata +4 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.6
|
@@ -17,10 +17,43 @@ class ModelsFromTablesGenerator < Rails::Generator::Base
|
|
17
17
|
|
18
18
|
analyzed_tables.each do |analyzed_table|
|
19
19
|
analyzed_table.class_name = LegacyData::TableClassNameMapper.class_name_for(analyzed_table[:table_name])
|
20
|
+
|
20
21
|
m.class_collisions :class_path, analyzed_table[:class_name]
|
21
22
|
m.template 'model.rb',
|
22
23
|
File.join('app/models', "#{analyzed_table[:class_name].underscore}.rb"),
|
23
24
|
:assigns => analyzed_table.to_hash
|
25
|
+
|
26
|
+
add_factory_girl_factory analyzed_table.to_hash
|
27
|
+
|
28
|
+
# # puts m.source_root
|
29
|
+
# # factory_template = m.source_path('factory.rb')
|
30
|
+
#
|
31
|
+
# factory_template = File.dirname(__FILE__) + '/templates/factory.rb'
|
32
|
+
# puts "factory_template #{factory_template}"
|
33
|
+
# new_factory = m.send(:render_file, factory_template) do |file|
|
34
|
+
# # vars = analyzed_table.to_hash
|
35
|
+
# # b = binding
|
36
|
+
# # vars.each { |k,v| eval "#{k} = vars[:#{k}] || vars['#{k}']", b }
|
37
|
+
# # puts 'in block'
|
38
|
+
# "in block"
|
39
|
+
# # Render the source file with the temporary binding.
|
40
|
+
# # ERB.new(file.read, nil, '-').result(b)
|
41
|
+
# end
|
42
|
+
# puts 'after new_factory'
|
43
|
+
# puts " its... #{new_factory}"
|
44
|
+
#
|
45
|
+
# m.gsub_file 'spec/factories.rb', /\Z/ do |match|
|
46
|
+
# # # # puts match
|
47
|
+
# # # puts 'BEFORE'
|
48
|
+
# # # # puts m.source_path('factory.rb')
|
49
|
+
# # # # puts analyzed_table.to_hash.inspect
|
50
|
+
# # # puts 'BEFORE'
|
51
|
+
# puts 'before'
|
52
|
+
# puts new_factory
|
53
|
+
# puts "DONE"
|
54
|
+
# "#{match}\n #{new_factory}\n"
|
55
|
+
# # "#{match}\n ok \n"
|
56
|
+
# end
|
24
57
|
end
|
25
58
|
end
|
26
59
|
rescue => e
|
@@ -38,4 +71,36 @@ protected
|
|
38
71
|
'Do not follow foreign keys to model associated tables') { |value| options[:skip_associated] = true }
|
39
72
|
end
|
40
73
|
|
74
|
+
|
75
|
+
def add_factory_girl_factory analyzed_schema
|
76
|
+
factory_name = analyzed_schema[:class_name].underscore
|
77
|
+
columns = analyzed_schema[:columns]
|
78
|
+
|
79
|
+
File.open("#{RAILS_ROOT}/spec/factories.rb", 'a+') do |file|
|
80
|
+
file.write "Factory.define :#{factory_name} do |#{factory_name.to_s.first}|\n"
|
81
|
+
columns.each do |c|
|
82
|
+
if c.null == false && c.name != analyzed_schema[:primary_key]
|
83
|
+
value = case c.type
|
84
|
+
when :integer
|
85
|
+
"7"
|
86
|
+
when :string
|
87
|
+
"'hi'"
|
88
|
+
when :boolean
|
89
|
+
'false'
|
90
|
+
when :date
|
91
|
+
'{Time.now}'
|
92
|
+
when :datetime
|
93
|
+
'{Time.now}'
|
94
|
+
when :decimal
|
95
|
+
'12.3'
|
96
|
+
else
|
97
|
+
raise "the generator forgot to handle columns of type #{c.type}"
|
98
|
+
end
|
99
|
+
file.write " #{factory_name.to_s.first}.#{c.name} #{value}\n"
|
100
|
+
end
|
101
|
+
end
|
102
|
+
file.write "end\n\n"
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
41
106
|
end
|
data/legacy_data.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{legacy_data}
|
8
|
-
s.version = "0.1.
|
8
|
+
s.version = "0.1.6"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Alex Rothenberg"]
|
12
|
-
s.date = %q{2009-10-
|
12
|
+
s.date = %q{2009-10-13}
|
13
13
|
s.description = %q{Create ActiveRecord models from an existing database}
|
14
14
|
s.email = %q{alex@alexrothenberg.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -33,6 +33,7 @@ Gem::Specification.new do |s|
|
|
33
33
|
"lib/legacy_data/schema.rb",
|
34
34
|
"lib/legacy_data/table_class_name_mapper.rb",
|
35
35
|
"lib/legacy_data/table_definition.rb",
|
36
|
+
"spec/expected/factories.rb",
|
36
37
|
"spec/expected/post.rb",
|
37
38
|
"spec/functional/expected/comment.rb",
|
38
39
|
"spec/functional/expected/post.rb",
|
@@ -51,7 +52,8 @@ Gem::Specification.new do |s|
|
|
51
52
|
s.rubygems_version = %q{1.3.2}
|
52
53
|
s.summary = %q{Create ActiveRecord models from an existing database}
|
53
54
|
s.test_files = [
|
54
|
-
"spec/expected/
|
55
|
+
"spec/expected/factories.rb",
|
56
|
+
"spec/expected/post.rb",
|
55
57
|
"spec/functional/expected/comment.rb",
|
56
58
|
"spec/functional/expected/post.rb",
|
57
59
|
"spec/functional/functional_spec_helper.rb",
|
data/lib/legacy_data/schema.rb
CHANGED
@@ -66,7 +66,7 @@ module LegacyData
|
|
66
66
|
def analyze_table
|
67
67
|
log "analyzing #{table_name} => #{class_name}"
|
68
68
|
TableDefinition.new(:table_name => table_name,
|
69
|
-
:columns =>
|
69
|
+
:columns => columns,
|
70
70
|
:primary_key => primary_key,
|
71
71
|
:relations => relations,
|
72
72
|
:constraints => constraints
|
@@ -4,6 +4,7 @@ describe 'Models From Tables generator' do
|
|
4
4
|
before :all do
|
5
5
|
silence_warnings { RAILS_ROOT = File.expand_path(File.dirname(__FILE__) + '/../../output/functional') }
|
6
6
|
FileUtils.mkdir_p(RAILS_ROOT + '/app/models')
|
7
|
+
FileUtils.mkdir_p(RAILS_ROOT + '/spec')
|
7
8
|
|
8
9
|
LegacyData::Schema.stub!(:log)
|
9
10
|
end
|
@@ -45,7 +45,7 @@ describe LegacyData::Schema do
|
|
45
45
|
|
46
46
|
it 'should have all the information about the table' do
|
47
47
|
@schema.stub!(:class_name ).and_return(class_name =mock)
|
48
|
-
@schema.stub!(:
|
48
|
+
@schema.stub!(:columns ).and_return(columns =mock)
|
49
49
|
@schema.stub!(:primary_key ).and_return(primary_key=mock)
|
50
50
|
@schema.stub!(:relations ).and_return(relations =mock)
|
51
51
|
@schema.stub!(:constraints ).and_return(constraints=mock)
|
@@ -2,17 +2,21 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
|
2
2
|
|
3
3
|
describe 'Models From Tables generator' do
|
4
4
|
before :all do
|
5
|
-
silence_warnings { RAILS_ROOT = File.expand_path(File.dirname(__FILE__) + '
|
5
|
+
silence_warnings { RAILS_ROOT = File.expand_path(File.dirname(__FILE__) + '/../output') }
|
6
6
|
FileUtils.mkdir_p(RAILS_ROOT + '/app/models')
|
7
|
+
FileUtils.mkdir_p(RAILS_ROOT + '/spec')
|
7
8
|
end
|
8
9
|
after :all do
|
9
10
|
Object.send(:remove_const, :RAILS_ROOT)
|
10
11
|
end
|
11
12
|
|
12
13
|
before :each do
|
14
|
+
FileUtils.rm(RAILS_ROOT + '/spec/factories.rb')
|
15
|
+
|
13
16
|
@posts = LegacyData::TableDefinition.new( :class_name => 'Posts',
|
14
17
|
:table_name => 'posts',
|
15
|
-
:columns => ['title',
|
18
|
+
:columns => [stub(:name=>'title', :null=>false, :type=>:string),
|
19
|
+
stub(:name=>'body', :null=>false, :type=>:string)],
|
16
20
|
:primary_key => 'id',
|
17
21
|
:relations => { :has_many =>{'comments'=>{:foreign_key=>'comment_id'}},
|
18
22
|
:belongs_to =>{},
|
@@ -27,16 +31,21 @@ describe 'Models From Tables generator' do
|
|
27
31
|
}
|
28
32
|
)
|
29
33
|
|
30
|
-
|
31
|
-
|
32
|
-
it 'should generate a posts model' do
|
34
|
+
|
33
35
|
LegacyData::Schema.should_receive(:analyze).with(hash_including(:table_name=>'posts')).and_return([@posts])
|
34
36
|
LegacyData::TableClassNameMapper.should_receive(:let_user_validate_dictionary)
|
35
37
|
LegacyData::TableClassNameMapper.stub!(:class_name_for).with('posts' ).and_return('Post')
|
36
38
|
LegacyData::TableClassNameMapper.stub!(:class_name_for).with('comments').and_return('Comment')
|
37
39
|
|
38
40
|
invoke_generator('models_from_tables', ["--table-name", "posts"], :create)
|
39
|
-
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'should generate a posts model' do
|
40
44
|
File.read(RAILS_ROOT + '/app/models/post.rb').should == File.read(File.expand_path(File.dirname(__FILE__) + '/expected/post.rb'))
|
41
45
|
end
|
46
|
+
|
47
|
+
it 'should generate a :posts factory' do
|
48
|
+
|
49
|
+
File.read(RAILS_ROOT + '/spec/factories.rb').should == File.read(File.expand_path(File.dirname(__FILE__) + '/expected/factories.rb'))
|
50
|
+
end
|
42
51
|
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: legacy_data
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alex Rothenberg
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-10-
|
12
|
+
date: 2009-10-13 00:00:00 -04:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -58,6 +58,7 @@ files:
|
|
58
58
|
- lib/legacy_data/schema.rb
|
59
59
|
- lib/legacy_data/table_class_name_mapper.rb
|
60
60
|
- lib/legacy_data/table_definition.rb
|
61
|
+
- spec/expected/factories.rb
|
61
62
|
- spec/expected/post.rb
|
62
63
|
- spec/functional/expected/comment.rb
|
63
64
|
- spec/functional/expected/post.rb
|
@@ -97,6 +98,7 @@ signing_key:
|
|
97
98
|
specification_version: 3
|
98
99
|
summary: Create ActiveRecord models from an existing database
|
99
100
|
test_files:
|
101
|
+
- spec/expected/factories.rb
|
100
102
|
- spec/expected/post.rb
|
101
103
|
- spec/functional/expected/comment.rb
|
102
104
|
- spec/functional/expected/post.rb
|