Module: RareMap::ModelGenerator

Included in:
Mapper
Defined in:
lib/rare_map/model_generator.rb

Instance Method Summary collapse

Instance Method Details

#generate_models(models, root = './') ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/rare_map/model_generator.rb', line 5

def generate_models(models, root = './')
  root ||= './'
  path = root + 'app/models/'
  Dir.mkdir root + 'app' unless Dir.exist? root + 'app'
  Dir.mkdir path unless Dir.exist? path
  
  group_db2conn = generate_connection_models(models, path)
  
  models.each do |model|
    output = ''
    output <<
    "module #{model.group.camelize}\n" <<
    "  class #{model.classify} < #{group_db2conn[[model.group, model.db_name]]}\n" <<
    "    self.table_name = '#{model.table.name}'\n" <<
    "    self.inheritance_column = 'ruby_type'\n" <<
    "    #{ "self.primary_key = '#{model.table.primary_key}'\n" if model.table.primary_key }\n" <<
    "    if ActiveRecord::VERSION::STRING < '4.0.0' || defined?(ProtectedAttributes)\n" <<
    "      attr_accessible #{model.table.columns.map { |col| ":#{col.name}" }.join(', ')}\n" <<
    "    end\n\n"
    
    belongs_to = model.relations.select { |rel| rel.type == :belongs_to }
    unless belongs_to.empty?
      output << belongs_to
        .map { |rel| "    belongs_to :#{rel.table.pluralize.singularize}, :foreign_key => '#{rel.foreign_key}', :class_name => '#{classify_by_table(rel.table, model, models)}'" }
        .join("\n") << "\n"
    end
    
    has_one = model.relations.select { |rel| rel.type == :has_one }
    unless has_one.empty?
      output << has_one
        .map { |rel| "    has_one :#{rel.table.pluralize.singularize}, :foreign_key => '#{rel.foreign_key}', :class_name => '#{classify_by_table(rel.table, model, models)}'" }
        .join("\n") << "\n"
    end
    
    has_many = model.relations.select { |rel| rel.type == :has_many }
    unless has_many.empty?
      output << has_many
        .map { |rel| "    has_many :#{rel.table.pluralize}, :foreign_key => '#{rel.foreign_key}', :class_name => '#{classify_by_table(rel.table, model, models)}'" }
        .join("\n") << "\n"
    end
    
    has_many_through = model.relations.select { |rel| rel.type == :has_many_through }
    unless has_many_through.empty?
      output << has_many_through
        .map { |rel| "    has_many :#{rel.table.pluralize}#{"_by_#{rel.through}, :source => :#{rel.table.pluralize.singularize}" if has_many_through.count { |hmt| hmt.table == rel.table } > 1 || has_many.count { |hm| hm.table == rel.table } > 0 }, :through => :#{rel.through.pluralize}, :foreign_key => '#{rel.foreign_key}', :class_name => '#{classify_by_table(rel.table, model, models)}'" }
        .join("\n") << "\n"
    end
    
    output << "  end\n"
    output << 'end'
    
    Dir.mkdir path + "#{model.group.underscore}" unless Dir.exist? path + "#{model.group}"
    f = File.new(path + "#{model.group.underscore}/#{model.classify.underscore}.rb", 'w')
    f.write output
    f.close
  end
  
  models
end