Class: Table

Inherits:
TableEntityBase show all
Defined in:
lib/table.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from TableEntityBase

all, find

Constructor Details

#initialize(table_id, table) ⇒ Table

Returns a new instance of Table.



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/table.rb', line 9

def initialize(table_id, table)
  @id = table_id
  @name = table['name'].underscore.singularize
  @entities = []
  @polymorphic = false
  @polymorphic_names = []
  @attributes = []
  table['attributes'].each do |(_attribute_id, attribute)|
    @attributes << Attribute.new(attribute)
  end
  @superclass = nil
  @subclasses = []
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



7
8
9
# File 'lib/table.rb', line 7

def attributes
  @attributes
end

#entitiesObject

Returns the value of attribute entities.



7
8
9
# File 'lib/table.rb', line 7

def entities
  @entities
end

#idObject

Returns the value of attribute id.



7
8
9
# File 'lib/table.rb', line 7

def id
  @id
end

#nameObject

Returns the value of attribute name.



7
8
9
# File 'lib/table.rb', line 7

def name
  @name
end

#polymorphicObject

Returns the value of attribute polymorphic.



7
8
9
# File 'lib/table.rb', line 7

def polymorphic
  @polymorphic
end

#polymorphic_namesObject

Returns the value of attribute polymorphic_names.



7
8
9
# File 'lib/table.rb', line 7

def polymorphic_names
  @polymorphic_names
end

#subclassesObject

Returns the value of attribute subclasses.



7
8
9
# File 'lib/table.rb', line 7

def subclasses
  @subclasses
end

#superclassObject

Returns the value of attribute superclass.



7
8
9
# File 'lib/table.rb', line 7

def superclass
  @superclass
end

Class Method Details

.update_superclasses(parsed_tables) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/table.rb', line 23

def self.update_superclasses(parsed_tables)
  all.each do |table|
    superclass_id = parsed_tables[table.id]['superclassId']

    next if superclass_id == ''

    super_class = Table.find superclass_id
    table.superclass = super_class
    super_class.subclasses << table
  end
end

Instance Method Details

#add_polymorphic_reference(command, poly_name) ⇒ Object



69
70
71
# File 'lib/table.rb', line 69

def add_polymorphic_reference(command, poly_name)
  command << " #{poly_name}:references{polymorphic}"
end

#add_polymorphic_reference_migration_for_stiObject



61
62
63
64
65
66
67
# File 'lib/table.rb', line 61

def add_polymorphic_reference_migration_for_sti
  return unless superclass && polymorphic

  polymorphic_names.each do |name|
    generate_reference_migration(name, true)
  end
end

#add_reference_migrationObject



121
122
123
124
125
126
127
128
129
# File 'lib/table.rb', line 121

def add_reference_migration
  entities.each do |entity|
    (entity.parents_has_many + entity.parents_has_one).each do |parent|
      next if entity.one_polymorphic_names?(parent)

      generate_reference_migration(parent.name)
    end
  end
end

#check_polymorphic(command) ⇒ Object



98
99
100
101
102
103
# File 'lib/table.rb', line 98

def check_polymorphic(command)
  update_polymorphic_names
  polymorphic_names.each do |poly_name|
    add_polymorphic_reference(command, poly_name)
  end
end

#create_modelObject



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/table.rb', line 105

def create_model
  return if File.exist?("./app/models/#{name}.rb")

  command = "bundle exec rails generate model #{model_name}"

  command << ' type' if subclasses.any? && root_class?

  attributes.each { |attribute| attribute.add_to(command) } unless superclass

  check_polymorphic(command)

  command << " --parent=#{superclass.name.classify}" if superclass

  system(command)
end

#generate_reference_migration(name, polymorphic = false) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/table.rb', line 53

def generate_reference_migration(name, polymorphic = false)
  command = "bundle exec rails generate migration Add#{name.camelize}RefTo#{root_class.name.camelize} #{name}:references"

  command << '{polymorphic}' if polymorphic

  system(command)
end

#model_nameObject



35
36
37
# File 'lib/table.rb', line 35

def model_name
  name.camelize
end

#root_classObject



39
40
41
42
43
44
45
46
47
# File 'lib/table.rb', line 39

def root_class
  nil unless @superclass

  root = self

  root = root.superclass while root.superclass

  root
end

#root_class?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/table.rb', line 49

def root_class?
  !superclass
end

#update_polymorphic_namesObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/table.rb', line 73

def update_polymorphic_names
  return if entities.empty?

  belong_parents = []
  entities.each do |entity|
    entity.parent_associations.each do |association|
      belong_parents << association.first_entity
    end
  end

  belong_parent_names = belong_parents.map(&:name)

  filtered_parent_names = belong_parent_names.find_all do |parent_name|
    belong_parent_names.count(parent_name) > 1
  end.uniq

  self.polymorphic_names = filtered_parent_names.find_all do |parent_name|
    belong_parents.find_all do |entity|
      entity.name == parent_name
    end.map(&:table).map(&:name).uniq.size > 1
  end

  self.polymorphic = true if polymorphic_names.size.positive?
end