Class: LatoCms::TemplateManager

Inherits:
Object
  • Object
show all
Defined in:
app/models/lato_cms/template_manager.rb

Class Method Summary collapse

Class Method Details

.componentsObject



16
17
18
# File 'app/models/lato_cms/template_manager.rb', line 16

def components
  load_yaml_files(components_path)
end

.components_pathObject



8
9
10
# File 'app/models/lato_cms/template_manager.rb', line 8

def components_path
  Rails.root.join(LatoCms.config.templates_path, 'components')
end

.find_component(component_id) ⇒ Object



25
26
27
28
# File 'app/models/lato_cms/template_manager.rb', line 25

def find_component(component_id)
  return nil if component_id.blank?
  components.find { |c| c['id'] == component_id.to_s }
end

.find_template(template_id) ⇒ Object



20
21
22
23
# File 'app/models/lato_cms/template_manager.rb', line 20

def find_template(template_id)
  return nil if template_id.blank?
  templates.find { |t| t['id'] == template_id.to_s }
end

.resolve_template_components(template) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/models/lato_cms/template_manager.rb', line 34

def resolve_template_components(template)
  return [] unless template && template['components']

  template['components'].map do |tc_id, tc_config|
    component = find_component(tc_config['component_id'])
    {
      template_component_id: tc_id.to_s,
      component_id: tc_config['component_id'].to_s,
      name: tc_config['name'].presence || component&.dig('name') || tc_config['component_id'].to_s.humanize,
      required: tc_config['required'] == true,
      component: component,
      fields: component ? (component['fields'] || {}) : {}
    }
  end
end

.template_optionsObject



30
31
32
# File 'app/models/lato_cms/template_manager.rb', line 30

def template_options
  templates.map { |t| [t['name'], t['id']] }
end

.templatesObject



12
13
14
# File 'app/models/lato_cms/template_manager.rb', line 12

def templates
  load_yaml_files(templates_path)
end

.templates_pathObject



4
5
6
# File 'app/models/lato_cms/template_manager.rb', line 4

def templates_path
  Rails.root.join(LatoCms.config.templates_path, 'templates')
end