Class: Entity

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from TableEntityBase

all, find

Constructor Details

#initialize(node) ⇒ Entity

Returns a new instance of Entity.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/entity.rb', line 10

def initialize(node)
  @id = node['id']
  @name = node['data']['name'].underscore.singularize
  @table = Table.find(node['data']['tableId'])
  @table.entities << self

  @parent_associations = []
  @associations = []

  @parents_has_one = []
  @parents_has_many = []
  @parents_has_one_through = []
  @parents_has_many_through = []
  @parents_through = []

  @children_has_one = []
  @children_has_many = []
  @children_has_one_through = []
  @children_has_many_through = []
  @children_through = []
end

Instance Attribute Details

#associationsObject

Returns the value of attribute associations.



6
7
8
# File 'lib/entity.rb', line 6

def associations
  @associations
end

#children_has_manyObject

Returns the value of attribute children_has_many.



6
7
8
# File 'lib/entity.rb', line 6

def children_has_many
  @children_has_many
end

#children_has_many_throughObject

Returns the value of attribute children_has_many_through.



6
7
8
# File 'lib/entity.rb', line 6

def children_has_many_through
  @children_has_many_through
end

#children_has_oneObject

Returns the value of attribute children_has_one.



6
7
8
# File 'lib/entity.rb', line 6

def children_has_one
  @children_has_one
end

#children_has_one_throughObject

Returns the value of attribute children_has_one_through.



6
7
8
# File 'lib/entity.rb', line 6

def children_has_one_through
  @children_has_one_through
end

#children_throughObject

Returns the value of attribute children_through.



6
7
8
# File 'lib/entity.rb', line 6

def children_through
  @children_through
end

#idObject

Returns the value of attribute id.



6
7
8
# File 'lib/entity.rb', line 6

def id
  @id
end

#nameObject

Returns the value of attribute name.



6
7
8
# File 'lib/entity.rb', line 6

def name
  @name
end

#parentObject

Returns the value of attribute parent.



6
7
8
# File 'lib/entity.rb', line 6

def parent
  @parent
end

#parent_associationObject

Returns the value of attribute parent_association.



6
7
8
# File 'lib/entity.rb', line 6

def parent_association
  @parent_association
end

#parent_associationsObject

Returns the value of attribute parent_associations.



6
7
8
# File 'lib/entity.rb', line 6

def parent_associations
  @parent_associations
end

#parents_has_manyObject

Returns the value of attribute parents_has_many.



6
7
8
# File 'lib/entity.rb', line 6

def parents_has_many
  @parents_has_many
end

#parents_has_many_throughObject

Returns the value of attribute parents_has_many_through.



6
7
8
# File 'lib/entity.rb', line 6

def parents_has_many_through
  @parents_has_many_through
end

#parents_has_oneObject

Returns the value of attribute parents_has_one.



6
7
8
# File 'lib/entity.rb', line 6

def parents_has_one
  @parents_has_one
end

#parents_has_one_throughObject

Returns the value of attribute parents_has_one_through.



6
7
8
# File 'lib/entity.rb', line 6

def parents_has_one_through
  @parents_has_one_through
end

#parents_throughObject

Returns the value of attribute parents_through.



6
7
8
# File 'lib/entity.rb', line 6

def parents_through
  @parents_through
end

#tableObject

Returns the value of attribute table.



6
7
8
# File 'lib/entity.rb', line 6

def table
  @table
end

Class Method Details

.find_by_name(name) ⇒ Object



32
33
34
# File 'lib/entity.rb', line 32

def self.find_by_name(name)
  all.find { |entity| entity.name == name }
end

Instance Method Details

#model_nameObject



36
37
38
# File 'lib/entity.rb', line 36

def model_name
  table.name.camelize
end

#one_polymorphic_names?(entity) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/entity.rb', line 52

def one_polymorphic_names?(entity)
  table.polymorphic && table.polymorphic_names.include?(entity.name)
end

#root_class_name_different?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/entity.rb', line 48

def root_class_name_different?
  table.root_class.name != name
end

#root_classes_same?(entity) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/entity.rb', line 40

def root_classes_same?(entity)
  table.root_class == entity.table.root_class
end

#table_name_different?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/entity.rb', line 44

def table_name_different?
  table.name != name
end

#update_end_model_migration_files(start_entity, association) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/entity.rb', line 56

def update_end_model_migration_files(start_entity, association)
  return unless association.has_any?

  end_model_line = {}
  end_migration_line = {}

  end_model_line['belongs_to'] = ":#{start_entity.name}"

  if root_classes_same?(start_entity)
    end_model_line['optional'] = 'true'
    end_migration_line['null'] = 'true'
  else
    end_migration_line['null'] = 'false'
  end

  end_migration_line['null'] = 'true' unless table.root_class?

  polymorphic_end = one_polymorphic_names?(start_entity)

  unless polymorphic_end
    end_model_line['class_name'] = "\"#{start_entity.table.name.camelize}\"" if start_entity.table_name_different?

    if start_entity.root_class_name_different?
      end_migration_line['foreign_key'] = "{ to_table: :#{start_entity.table.root_class.name.pluralize} }"
    end

    if start_entity.table.superclass && start_entity.root_class_name_different?
      end_migration_line['column'] = ":#{start_entity.name}_id"
    end

  end

  update_project_files(start_entity, end_model_line, end_migration_line)
end

#update_migration_files(start_entity, end_migration_line) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/entity.rb', line 108

def update_migration_files(start_entity, end_migration_line)
  return if end_migration_line.empty?

  polymorphic_end = one_polymorphic_names?(start_entity)

  if table.root_class? && polymorphic_end
    migration_name = "Create#{table.name.camelize.pluralize}".underscore

    ProjectFile.update_line(
      migration_name,
      'migration',
      /t.references :#{start_entity.name}/,
      end_migration_line
    )

  else
    migration_name = "Add#{start_entity.name.camelize}RefTo#{table.root_class.name.camelize}".underscore

    ProjectFile.update_line(
      migration_name,
      'reference_migration',
      /add_reference :#{table.root_class.name.pluralize}/,
      end_migration_line
    )
  end
end

#update_model(end_entity, association) ⇒ Object



185
186
187
188
189
# File 'lib/entity.rb', line 185

def update_model(end_entity, association)
  end_entity.update_end_model_migration_files(self, association) if association.has_any?

  update_start_model_file(end_entity, association)
end

#update_model_files(start_entity, end_model_line) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/entity.rb', line 96

def update_model_files(start_entity, end_model_line)
  return if end_model_line.empty?

  polymorphic_end = one_polymorphic_names?(start_entity)

  if polymorphic_end
    ProjectFile.update_line(table.name, 'model', /belongs_to :#{start_entity.name}/, end_model_line)
  else
    ProjectFile.add_belong_line(table.name, end_model_line)
  end
end

#update_project_files(start_entity, end_model_line, end_migration_line) ⇒ Object



91
92
93
94
# File 'lib/entity.rb', line 91

def update_project_files(start_entity, end_model_line, end_migration_line)
  update_model_files(start_entity, end_model_line)
  update_migration_files(start_entity, end_migration_line)
end

#update_start_model_file(end_entity, association) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/entity.rb', line 135

def update_start_model_file(end_entity, association)
  start_model = table.name

  end_model = end_entity.name
  intermediate_entity = association.through_entity

  intermediate_model = intermediate_entity.name if intermediate_entity

  if association.has_many? || (children_has_many_through.include? end_entity)
    end_model = end_model.pluralize
    intermediate_model = intermediate_model.pluralize if intermediate_model
  end

  line_content = {}

  if association.has_many? || (children_has_many_through.include? end_entity)
    line_content['has_many'] = if intermediate_entity&.one_polymorphic_names?(end_entity) && (children_has_many_through.include? end_entity)
                                 ":#{end_entity.table.name.pluralize}"
                               else
                                 ":#{end_model}"
                               end
  end

  if association.has_one? || (children_has_one_through.include? end_entity)
    line_content['has_one'] = if intermediate_entity&.one_polymorphic_names?(end_entity) && (children_has_one_through.include? end_entity)
                                ":#{end_entity.table.name}"
                              else
                                ":#{end_model}"
                              end
  end

  if intermediate_entity
    line_content['through'] = ":#{intermediate_model}"
    if intermediate_entity.one_polymorphic_names?(end_entity)
      line_content['source'] = ":#{end_entity.name}"
      line_content['source_type'] = "\"#{end_entity.table.name.camelize}\" "
    end
  elsif !intermediate_entity
    line_content['class_name'] = "\"#{end_entity.table.name.camelize}\"" if end_entity.table_name_different?

    if end_entity.one_polymorphic_names?(self)
      line_content['as'] = ":#{name}"
    elsif table_name_different?
      line_content['foreign_key'] = "\"#{name.singularize}_id\""
    end
  end

  ProjectFile.add_line(start_model, end_model, line_content)
end