Class: Gluttonberg::PageRepairer

Inherits:
Object
  • Object
show all
Defined in:
lib/gluttonberg/content/page_repairer.rb

Class Method Summary collapse

Class Method Details

.change_page_description(page, old_description_name, new_description_name, page_attributes) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/gluttonberg/content/page_repairer.rb', line 60

def self.change_page_description(page, old_description_name, new_description_name, page_attributes)
  if(old_description_name != new_description_name)
    old_description = PageDescription[old_description_name.to_sym]
    new_description = PageDescription[new_description_name.to_sym]
    if old_description && new_description
      PageRepairer.update_page_sections(page, old_description, new_description)
    elsif new_description #old one does not exist anymore
    end
    page.update_attributes(page_attributes)
    PageRepairer.create_missing_sections(page)
    page.create_default_template_file
  end
end

.clean_page_section(page, content, klass) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/gluttonberg/content/page_repairer.rb', line 33

def self.clean_page_section(page, content, klass)
  found = page.description.contains_section?(content.section_name , content.class.to_s.demodulize.underscore)
  unless found
    puts "#{content.section_name} (#{klass.name}) section from #{page.name} page"
    content.destroy
  end
end

.clean_page_sections(page) ⇒ Object

remove page sections from database which does not exist anymore in page description



24
25
26
27
28
29
30
31
# File 'lib/gluttonberg/content/page_repairer.rb', line 24

def self.clean_page_sections(page)
  [PlainTextContent , HtmlContent , ImageContent].each do |klass|
    list = klass.where(:page_id => page.id).all
    list.each do |item|
      self.clean_page_section(page, item, klass)
    end
  end
end

.create_missing_section(page, section_name, section_info) ⇒ Object



49
50
51
52
53
54
55
56
57
58
# File 'lib/gluttonberg/content/page_repairer.rb', line 49

def self.create_missing_section(page, section_name, )
  # Create the content
  association = page.send([:type].to_s.pluralize)
  content = association.where(:section_name => section_name).first
  if content.blank?
    puts "Create #{section_name} section for #{page.name} page"
    content = association.create(:section_name => section_name)
  end
  content
end

.create_missing_section_localization(page, section_name, section_info, content, localization) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/gluttonberg/content/page_repairer.rb', line 99

def self.create_missing_section_localization(page, section_name, , content, localization)
  content_localization_count = content.localizations.where({
    "#{[:type]}_id" => content.id,
    :page_localization_id => localization.id
  }).count
  # missing localization. create it
  if content_localization_count == 0
    puts "Create #{localization.locale.name} localizations for #{content.section_name}"
    content.localizations.create({
      :parent => content,
      :page_localization => localization
    })
  end
end

.create_missing_section_localizations(page, section_name, section_info, content) ⇒ Object



90
91
92
93
94
95
96
97
# File 'lib/gluttonberg/content/page_repairer.rb', line 90

def self.create_missing_section_localizations(page, section_name, , content)
  # Create each localization
  if content.class.localized?
    page.localizations.all.each do |localization|
      self.create_missing_section_localization(page, section_name, , content, localization)
    end
  end
end

.create_missing_sections(page) ⇒ Object

create missing page sections for page



42
43
44
45
46
47
# File 'lib/gluttonberg/content/page_repairer.rb', line 42

def self.create_missing_sections(page)
  page.description.sections.each do |section_name, |
    content = self.create_missing_section(page, section_name, )
    self.create_missing_section_localizations(page, section_name, , content)
  end
end

.repair_page_structure(page) ⇒ Object

repair_pages_structure



12
13
14
15
16
17
18
19
20
21
# File 'lib/gluttonberg/content/page_repairer.rb', line 12

def self.repair_page_structure(page)
  if page.description.blank?
    puts "Page description '#{page.description_name}' for '#{page.name}' (#{page.id}) page  does not exist in page descriptions file. "
  elsif !page.description.sections.blank?
    puts "Updating page structure for #{page.name} (#{page.id}) page"
    self.clean_page_sections(page)
    self.create_missing_sections(page)
    puts "\n"
  end
end

.repair_pages_structureObject



3
4
5
6
7
8
9
10
# File 'lib/gluttonberg/content/page_repairer.rb', line 3

def self.repair_pages_structure
  pages = Page.all

  pages.each do |page|
    self.repair_page_structure(page)
  end # pages loop end
  puts "completed"
end

.update_page_sections(page, old_description, new_description) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/gluttonberg/content/page_repairer.rb', line 74

def self.update_page_sections(page, old_description, new_description)
  used_sections = []
  new_description.sections.each do |section_name,  |
    matched_type_section = old_description.sections.find_all{|old_section_name, | !used_sections.include?(old_section_name) &&  old_section_name == section_name && [:type] == [:type] }.first
    association = page.send([:type].to_s.pluralize)
    unless matched_type_section.blank?
      content = association.where(:section_name => matched_type_section.first.to_s).first
      used_sections << matched_type_section.first.to_s
      unless content.blank?
        content.update_attributes(:section_name => section_name)
      end
    else
    end
  end #sections loop
end