Class: Promethee::StructureUpgraderService

Inherits:
Object
  • Object
show all
Defined in:
app/services/promethee/structure_upgrader_service.rb

Constant Summary collapse

BASE_COMPONENTS =
{
  aside: Promethee::StructureUpgrader::Components::Aside,
  blockquote: Promethee::StructureUpgrader::Components::Blockquote,
  collection: Promethee::StructureUpgrader::Components::Collection,
  collection_item: Promethee::StructureUpgrader::Components::CollectionItem,
  column: Promethee::StructureUpgrader::Components::Column,
  cover: Promethee::StructureUpgrader::Components::Cover,
  faq: Promethee::StructureUpgrader::Components::Faq,
  faq_item: Promethee::StructureUpgrader::Components::FaqItem,
  image: Promethee::StructureUpgrader::Components::Image,
  page: Promethee::StructureUpgrader::Components::Page,
  row: Promethee::StructureUpgrader::Components::Row,
  slider: Promethee::StructureUpgrader::Components::Slider,
  slider_item: Promethee::StructureUpgrader::Components::SliderItem,
  table: Promethee::StructureUpgrader::Components::Table,
  table_cell: Promethee::StructureUpgrader::Components::TableCell,
  text: Promethee::StructureUpgrader::Components::Text,
  video: Promethee::StructureUpgrader::Components::Video
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model_name) ⇒ StructureUpgraderService

Returns a new instance of StructureUpgraderService.



24
25
26
27
28
29
30
31
32
33
# File 'app/services/promethee/structure_upgrader_service.rb', line 24

def initialize(model_name)
  begin
    model_class = model_name.constantize
    objects = model_class.all
  rescue
    puts 'Please provide a valid model name (e.g. `rake promethee:upgrade_structure[Page]`)'
    exit
  end
  @objects = objects
end

Instance Attribute Details

#objectsObject

Returns the value of attribute objects.



22
23
24
# File 'app/services/promethee/structure_upgrader_service.rb', line 22

def objects
  @objects
end

Instance Method Details

#process_component(data) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
# File 'app/services/promethee/structure_upgrader_service.rb', line 80

def process_component(data)
  return nil unless data.has_key? 'type'

  component_type = data['type']
  component_upgrader = search_component(component_type).new(data)

  data = component_upgrader.upgraded_data
  data['children'] ||= []
  data['children'].map! { |child| process_component(child) }.compact!

  data
end

#process_localization(data) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/services/promethee/structure_upgrader_service.rb', line 57

def process_localization(data)
  data['components'].map! { |component|
    process_localization_component(component)
  }.compact!

  # We remove the possible children to concatenate them to the list
  children = []
  data['components'].each { |component|
    children.concat component.delete('children').to_a
  }
  data['components'].concat(children).compact!

  data
end

#process_localization_component(component) ⇒ Object



72
73
74
75
76
77
78
# File 'app/services/promethee/structure_upgrader_service.rb', line 72

def process_localization_component(component)
  upgraded_component = process_component(component)
  return nil if upgraded_component.nil?
  # We only keep the translatable attributes
  upgraded_component['attributes'].keep_if { |key, object_value| object_value['translatable'] }
  upgraded_component
end

#process_object(object) ⇒ Object



48
49
50
51
52
53
54
55
# File 'app/services/promethee/structure_upgrader_service.rb', line 48

def process_object(object)
  puts "Processing object ##{object.id}"

  object.data = object.data.has_key?('components')  ? process_localization(object.data)
                                                    : process_component(object.data)
  object.save
  puts "End processing object ##{object.id}"
end

#startObject



35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/services/promethee/structure_upgrader_service.rb', line 35

def start
  puts '= START STRUCTURE UPGRADE ='
  puts "Number of objects: #{objects.count}"
  i = 0
  objects.each do |object|
    next unless can_process?(object.data)
    i += 1
    process_object(object)
  end
  puts "Number of processed objects: #{i}"
  puts '====== END UPGRADER ========'
end