Class: LatoCms::Page

Inherits:
ApplicationRecord
  • Object
show all
Includes:
LatoSpaces::Associable, LatoSpaces::AssociableRequired, LatoSpaces::AssociableUnique
Defined in:
app/models/lato_cms/page.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#actionsObject

Returns the value of attribute actions.



7
8
9
# File 'app/models/lato_cms/page.rb', line 7

def actions
  @actions
end

Instance Method Details

#as_json(options = {}) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'app/models/lato_cms/page.rb', line 58

def as_json(options = {})
  data = {
    id: id,
    title: title,
    permalink: permalink,
    locale: locale,
    template_id: template_id,
    template_name: template_name,
    frontend_url: frontend_url,
    created_at: created_at,
    updated_at: updated_at
  }

  if options[:include_fields]
    fields.load unless fields.loaded?
    data[:fields] = build_fields_json(template_components.select { |tc| component_effectively_enabled?(tc[:template_component_id]) })
  end

  data
end

#component_effectively_enabled?(template_component_id, default: true) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
50
51
# File 'app/models/lato_cms/page.rb', line 47

def component_effectively_enabled?(template_component_id, default: true)
  return true if component_required?(template_component_id)

  component_enabled?(template_component_id, default: default)
end

#component_enabled?(template_component_id, default: true) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
38
39
40
# File 'app/models/lato_cms/page.rb', line 35

def component_enabled?(template_component_id, default: true)
  value = (component_states || {})[template_component_id.to_s]
  return default if value.nil?

  value == true
end

#component_required?(template_component_id) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
45
# File 'app/models/lato_cms/page.rb', line 42

def component_required?(template_component_id)
  tc = template_components.find { |component| component[:template_component_id] == template_component_id.to_s }
  tc && tc[:required] == true
end

#set_component_enabled(template_component_id, enabled) ⇒ Object



53
54
55
56
# File 'app/models/lato_cms/page.rb', line 53

def set_component_enabled(template_component_id, enabled)
  key = template_component_id.to_s
  self.component_states = (component_states || {}).merge(key => ActiveModel::Type::Boolean.new.cast(enabled))
end

#templateObject



18
19
20
# File 'app/models/lato_cms/page.rb', line 18

def template
  LatoCms::TemplateManager.find_template(template_id)
end

#template_available?Boolean

Returns:

  • (Boolean)


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

def template_available?
  template_id.blank? || template.present?
end

#template_componentsObject



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

def template_components
  return [] unless template
  LatoCms::TemplateManager.resolve_template_components(template)
end

#template_nameObject



22
23
24
# File 'app/models/lato_cms/page.rb', line 22

def template_name
  template&.dig('name') || template_id
end