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'].split(' || ')[0].underscore.singularize
  @twin_name = node['data']['name'].split(' || ')[1]&.underscore&.singularize
  @clone_parent = Table.find(node['data']['tableId'])

  @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

#clone_parentObject

Returns the value of attribute clone_parent.



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

def clone_parent
  @clone_parent
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

#twin_nameObject

Returns the value of attribute twin_name.



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

def twin_name
  @twin_name
end

Class Method Details

.find_by_name(name) ⇒ Object



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

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

Instance Method Details

#clone_name_different?Boolean

Returns:

  • (Boolean)


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

def clone_name_different?
  clone_parent.name != name
end

#model_nameObject



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

def model_name
  clone_parent.name.camelize
end

#one_polymorphic_names?(entity) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#tableObject



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

def table
  clone_parent
end

#tables_same?(entity) ⇒ Boolean

Returns:

  • (Boolean)


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

def tables_same?(entity)
  table == entity.table
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
# File 'lib/entity.rb', line 56

def update_end_model_migration_files(start_entity, association)
  polymorphic_end = one_polymorphic_names?(start_entity)

  return if polymorphic_end

  end_model_line = {}
  end_migration_line = {}

  if association.has_any?
    end_model_line['belongs_to'] = ":#{start_entity.name}"

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

    if !polymorphic_end && start_entity.clone_name_different?
      end_model_line['class_name'] = "\"#{start_entity.clone_parent.name.camelize}\""
      end_migration_line['foreign_key'] = "{ to_table: :#{start_entity.clone_parent.name.pluralize} }"
    end
  end

  ProjectFile.add_belong_line(clone_parent.name, end_model_line) unless end_model_line.empty?

  unless end_migration_line.empty?
    migration_name = "Add#{start_entity.name.camelize}RefTo#{clone_parent.name.camelize}".underscore

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

#update_model(end_entity, association) ⇒ Object



140
141
142
143
144
# File 'lib/entity.rb', line 140

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_start_model_file(end_entity, association) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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
134
135
136
137
138
# File 'lib/entity.rb', line 90

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) && (association.has_many? || (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) && (association.has_one? || (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.clone_name_different?

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

  ProjectFile.add_line(start_model, end_model, line_content)
end