Class: Table
- Inherits:
-
TableEntityBase
- Object
- TableEntityBase
- Table
- Defined in:
- lib/table.rb
Instance Attribute Summary collapse
-
#attributes ⇒ Object
Returns the value of attribute attributes.
-
#entities ⇒ Object
Returns the value of attribute entities.
-
#id ⇒ Object
Returns the value of attribute id.
-
#name ⇒ Object
Returns the value of attribute name.
-
#polymorphic ⇒ Object
Returns the value of attribute polymorphic.
-
#polymorphic_names ⇒ Object
Returns the value of attribute polymorphic_names.
-
#twin_name ⇒ Object
Returns the value of attribute twin_name.
Instance Method Summary collapse
- #add_polymorphic_reference(command, poly_name) ⇒ Object
- #add_reference_migration ⇒ Object
- #add_references(entity) ⇒ Object
- #check_polymorphic(command) ⇒ Object
- #create_model ⇒ Object
-
#initialize(table_id, tables) ⇒ Table
constructor
A new instance of Table.
- #model_name ⇒ Object
- #update_polymorphic_names ⇒ Object
Methods inherited from TableEntityBase
Constructor Details
#initialize(table_id, tables) ⇒ Table
Returns a new instance of Table.
9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/table.rb', line 9 def initialize(table_id, tables) @id = table_id @name = tables[table_id]['name'].split(' || ')[0].underscore.singularize @entities = [] @polymorphic = false @polymorphic_names = [] @attributes = [] tables[table_id]['attributes'].each do |(_attribute_id, attribute)| @attributes << Attribute.new(attribute) end end |
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
7 8 9 |
# File 'lib/table.rb', line 7 def attributes @attributes end |
#entities ⇒ Object
Returns the value of attribute entities.
7 8 9 |
# File 'lib/table.rb', line 7 def entities @entities end |
#id ⇒ Object
Returns the value of attribute id.
7 8 9 |
# File 'lib/table.rb', line 7 def id @id end |
#name ⇒ Object
Returns the value of attribute name.
7 8 9 |
# File 'lib/table.rb', line 7 def name @name end |
#polymorphic ⇒ Object
Returns the value of attribute polymorphic.
7 8 9 |
# File 'lib/table.rb', line 7 def polymorphic @polymorphic end |
#polymorphic_names ⇒ Object
Returns the value of attribute polymorphic_names.
7 8 9 |
# File 'lib/table.rb', line 7 def polymorphic_names @polymorphic_names end |
#twin_name ⇒ Object
Returns the value of attribute twin_name.
7 8 9 |
# File 'lib/table.rb', line 7 def twin_name @twin_name end |
Instance Method Details
#add_polymorphic_reference(command, poly_name) ⇒ Object
31 32 33 |
# File 'lib/table.rb', line 31 def add_polymorphic_reference(command, poly_name) command << " #{poly_name}:references{polymorphic}" end |
#add_reference_migration ⇒ Object
79 80 81 82 83 84 85 86 87 |
# File 'lib/table.rb', line 79 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) add_references(parent) end end end |
#add_references(entity) ⇒ Object
25 26 27 28 29 |
# File 'lib/table.rb', line 25 def add_references(entity) command = "bundle exec rails generate migration Add#{entity.name.camelize}RefTo#{name.camelize} #{entity.name}:references" system(command) end |
#check_polymorphic(command) ⇒ Object
58 59 60 61 62 63 64 65 |
# File 'lib/table.rb', line 58 def check_polymorphic(command) update_polymorphic_names @polymorphic_names.each do |poly_name| add_polymorphic_reference(command, poly_name) end @polymorphic = true if @polymorphic_names.size.positive? end |
#create_model ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/table.rb', line 67 def create_model return if File.exist?("./app/models/#{name}.rb") command = 'bundle exec rails generate model ' command << model_name attributes.each { |attribute| attribute.add_to(command) } check_polymorphic(command) system(command) end |
#model_name ⇒ Object
21 22 23 |
# File 'lib/table.rb', line 21 def model_name name.camelize end |
#update_polymorphic_names ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/table.rb', line 35 def update_polymorphic_names return if entities.size.zero? 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 @polymorphic_names = filtered_parent_names.find_all do |parent_name| belong_parents.find_all do |entity| entity.name == parent_name end.map(&:clone_parent).map(&:name).uniq.size > 1 end end |