legacy_data 0.1.12 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.rvmrc +1 -0
- data/History.txt +2 -2
- data/README.md +6 -4
- data/Rakefile +9 -11
- data/VERSION +1 -1
- data/examples/generated/blog_mysql/factories.rb +3 -3
- data/examples/generated/blog_sqlite3/factories.rb +4 -4
- data/examples/generated/drupal_mysql/factories.rb +321 -321
- data/examples/generated/j2ee_petstore_mysql/factories.rb +39 -39
- data/examples/generated/j2ee_petstore_oracle/factories.rb +36 -36
- data/examples/generated/j2ee_petstore_sqlite3/factories.rb +39 -39
- data/legacy_data.gemspec +4 -3
- data/lib/{active_record/connection_adapters/oracleenhanced_adapter.rb → foreigner/connection_adapters/oracle_enhanced_adapter.rb} +0 -0
- data/lib/generators/models_from_tables/USAGE +12 -0
- data/lib/generators/models_from_tables/models_from_tables_generator.rb +70 -0
- data/lib/generators/models_from_tables/templates/model.rb +11 -0
- data/lib/legacy_data.rb +6 -7
- data/lib/legacy_data/schema.rb +1 -0
- data/lib/legacy_data/table_class_name_mapper.rb +4 -2
- data/lib/legacy_data/table_definition.rb +1 -2
- data/spec/functional/blog_adapterspec.rb +13 -13
- data/spec/functional/drupal_adapterspec.rb +14 -12
- data/spec/functional/functional_spec_helper.rb +3 -5
- data/spec/functional/j2ee_petstore_adapterspec.rb +14 -14
- data/spec/generators/models_from_tables/expected/factories.rb +5 -0
- data/spec/{expected → generators/models_from_tables/expected}/post.rb +0 -0
- data/spec/{models_from_tables_generator_spec.rb → generators/models_from_tables/models_from_tables_generator_spec.rb} +28 -28
- data/spec/legacy_data/schema_spec.rb +0 -1
- data/spec/legacy_data/table_class_name_mapper_spec.rb +1 -4
- data/spec/spec_helper.rb +46 -28
- metadata +66 -49
- data/generators/models_from_tables/USAGE +0 -6
- data/generators/models_from_tables/models_from_tables_generator.rb +0 -78
- data/generators/models_from_tables/templates/model.rb +0 -11
- data/spec/expected/factories.rb +0 -5
@@ -1,78 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/../../lib/legacy_data'
|
2
|
-
|
3
|
-
class ModelsFromTablesGenerator < Rails::Generator::Base
|
4
|
-
def manifest
|
5
|
-
record do |m|
|
6
|
-
LegacyData::TableClassNameMapper.naming_convention = options[:table_naming_convention]
|
7
|
-
|
8
|
-
analyzed_tables = LegacyData::Schema.analyze(options)
|
9
|
-
|
10
|
-
unless analyzed_tables.blank?
|
11
|
-
m.directory File.join('app/models')
|
12
|
-
|
13
|
-
LegacyData::TableClassNameMapper.let_user_validate_dictionary
|
14
|
-
|
15
|
-
analyzed_tables.each do |analyzed_table|
|
16
|
-
analyzed_table.class_name = LegacyData::TableClassNameMapper.class_name_for(analyzed_table[:table_name])
|
17
|
-
|
18
|
-
m.class_collisions :class_path, analyzed_table[:class_name]
|
19
|
-
m.template 'model.rb',
|
20
|
-
File.join('app/models', "#{analyzed_table[:class_name].underscore}.rb"),
|
21
|
-
:assigns => {:definition => analyzed_table}
|
22
|
-
|
23
|
-
add_factory_girl_factory analyzed_table if options[:with_factories]
|
24
|
-
end
|
25
|
-
|
26
|
-
end
|
27
|
-
end
|
28
|
-
rescue => e
|
29
|
-
# for debugging...
|
30
|
-
puts e.backtrace
|
31
|
-
end
|
32
|
-
|
33
|
-
protected
|
34
|
-
def add_options!(opt)
|
35
|
-
opt.on('--table-name [ARG]',
|
36
|
-
'Only generate models for given table') { |value| options[:table_name] = value }
|
37
|
-
opt.on('--table-naming-convention [ARG]',
|
38
|
-
'Naming convention for tables in the database - will not be used when generating naming the models') { |value| options[:table_naming_convention] = value }
|
39
|
-
opt.on('--skip-associated',
|
40
|
-
'Do not follow foreign keys to model associated tables') { |value| options[:skip_associated] = true }
|
41
|
-
opt.on('--with-factories',
|
42
|
-
'Add factory-girl factories for all created models') { |value| options[:with_factories] = true }
|
43
|
-
end
|
44
|
-
|
45
|
-
|
46
|
-
def add_factory_girl_factory analyzed_schema
|
47
|
-
factory_name = analyzed_schema[:class_name].underscore
|
48
|
-
columns = analyzed_schema[:columns]
|
49
|
-
|
50
|
-
File.open("#{RAILS_ROOT}/spec/factories.rb", 'a+') do |file|
|
51
|
-
file.write "Factory.define :#{factory_name} do |#{factory_name.to_s.first}|\n"
|
52
|
-
column_with_longest_name = columns.max {|a,b| a.name.length <=> b.name.length }
|
53
|
-
columns.each do |c|
|
54
|
-
if c.null == false && c.name != analyzed_schema[:primary_key]
|
55
|
-
value = case c.type
|
56
|
-
when :integer then %[7]
|
57
|
-
when :float then %[12.3]
|
58
|
-
when :decimal then %[12.3]
|
59
|
-
when :datetime then %[{Time.now}]
|
60
|
-
when :date then %[{Time.now}]
|
61
|
-
when :timestamp then %[{Time.now}]
|
62
|
-
when :time then %[{Time.now}]
|
63
|
-
when :text then %['some text value']
|
64
|
-
when :string then %['some string']
|
65
|
-
when :binary then %['some binary stuff']
|
66
|
-
when :boolean then %[false]
|
67
|
-
else
|
68
|
-
"'Sorry the models_to_tables_generator is not sure how to default columns of type #{c.type}'"
|
69
|
-
end
|
70
|
-
spaces = column_with_longest_name.name.size - c.name.size
|
71
|
-
file.write " #{factory_name.to_s.first}.#{c.name}#{' ' * spaces} #{value}\n"
|
72
|
-
end
|
73
|
-
end
|
74
|
-
file.write "end\n\n"
|
75
|
-
end
|
76
|
-
end
|
77
|
-
|
78
|
-
end
|
@@ -1,11 +0,0 @@
|
|
1
|
-
class <%= definition.class_name -%> < ActiveRecord::Base
|
2
|
-
<%= " set_table_name #{definition.table_name.downcase.to_sym.inspect}\n" if definition.unconventional_table_name? -%>
|
3
|
-
<%= " set_primary_key #{definition.primary_key.to_sym.inspect}\n" if definition.unconventional_primary_key? -%>
|
4
|
-
|
5
|
-
# Relationships
|
6
|
-
<%= definition.relationships_to_s %>
|
7
|
-
|
8
|
-
# Constraints
|
9
|
-
<%= definition.constraints_to_s %>
|
10
|
-
end
|
11
|
-
|