Class: Panda::CMS::Template

Inherits:
ApplicationRecord show all
Defined in:
app/models/panda/cms/template.rb

Overview

Represents a template in the Panda CMS application.

Class Method Summary collapse

Class Method Details

.defaultObject



31
32
33
# File 'app/models/panda/cms/template.rb', line 31

def self.default
  find_by(file_path: "layouts/page")
end

.generate_missing_blocksvoid

This method returns an undefined value.

Generate missing blocks for all templates



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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
90
91
92
93
# File 'app/models/panda/cms/template.rb', line 41

def self.generate_missing_blocks
  # Loop through all templates in app/views/layouts/*.html.erb
  Dir.glob("app/views/layouts/*.html.erb").each do |file|
    # TODO: Delete all blocks which aren't in use by a template?

    File.open(file).each_line do |line|
      # Matches:
      # Panda::CMS::RichTextComponent.new(key: :value)
      # Panda::CMS::RichTextComponent.new key: :value, key: value
      line.match(/Panda::CMS::([a-zA-Z]+)Component\.new[ (]+([^)]+)\)*/) do |match|
        # Extract the hash values
        template_path = file.gsub("app/views/", "").gsub(".html.erb", "")
        template_name = template_path.gsub("layouts/", "").titleize

        # Create the template if it doesn't exist
        template = Panda::CMS::Template.find_or_create_by!(file_path: template_path) do |template|
          template.name = template_name
        end

        next if match[1] == "PageMenu" # Skip PageMenu blocks
        next if match[1] == "Menu" # Skip Menu blocks

        # Previously used match[1].underscore but this supports more complex database
        # operations, and is more secure as it'll force the usage of a class
        block_kind = "Panda::CMS::#{match[1]}Component".constantize::KIND

        match[2].split(",").map do |keyvar|
          key, value = keyvar.split(":", 2)
          next if key != "key"

          block_name = value.to_s.strip.tr(":", "")
          # Create the block if it doesn't exist
          # TODO: +/- the output if it's created or removed
          begin
            block = Panda::CMS::Block.find_or_create_by!(template: template, kind: block_kind,
              key: block_name) do |block|
              block.name = block_name.titleize
            end
          rescue ActiveRecord::RecordInvalid => e
            raise "Error creating block '#{block_name}' on template '#{template_name}': #{e.message}"
          end

          # For the given block, create the block_content for each page using the template
          template.pages.each do |page|
            Panda::CMS::BlockContent.find_or_create_by!(block: block, page: page) do |block_content|
              block_content.content = {}
            end
          end
        end
      end
    end
  end
end

.reset_counter_cacheObject



35
36
37
# File 'app/models/panda/cms/template.rb', line 35

def self.reset_counter_cache
  find_each { |template| template.update_column(:pages_count, template.pages.count) }
end