Class: LatoCms::Page
- Inherits:
-
ApplicationRecord
- Object
- ApplicationRecord
- LatoCms::Page
- Includes:
- LatoSpaces::Associable, LatoSpaces::AssociableRequired, LatoSpaces::AssociableUnique
- Defined in:
- app/models/lato_cms/page.rb
Instance Attribute Summary collapse
-
#actions ⇒ Object
Returns the value of attribute actions.
Instance Method Summary collapse
- #as_json(options = {}) ⇒ Object
- #component_effectively_enabled?(template_component_id, default: true) ⇒ Boolean
- #component_enabled?(template_component_id, default: true) ⇒ Boolean
- #component_required?(template_component_id) ⇒ Boolean
-
#link_translation(other_page) ⇒ Object
Links another page as a translation of this one, merging any existing groups so all members share a single translation_group_id.
- #set_component_enabled(template_component_id, enabled) ⇒ Object
- #template ⇒ Object
- #template_available? ⇒ Boolean
- #template_components ⇒ Object
- #template_name ⇒ Object
-
#translation_candidates(locale) ⇒ Object
Pages of a given locale that can be linked to this page: same spaces group, not this page, and not already part of any translation group.
-
#translation_for(locale) ⇒ Object
The linked translation for a given locale, if any.
-
#translations ⇒ Object
Sibling pages linked as translations of this page.
-
#unlink_translation(other_page) ⇒ Object
Removes a page from this page's translation group.
Instance Attribute Details
#actions ⇒ Object
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
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'app/models/lato_cms/page.rb', line 120 def as_json( = {}) data = { id: id, title: title, permalink: permalink, locale: locale, template_id: template_id, template_name: template_name, frontend_url: frontend_url, translations: translations_json, created_at: created_at, updated_at: updated_at } if [: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
109 110 111 112 113 |
# File 'app/models/lato_cms/page.rb', line 109 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
97 98 99 100 101 102 |
# File 'app/models/lato_cms/page.rb', line 97 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
104 105 106 107 |
# File 'app/models/lato_cms/page.rb', line 104 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 |
#link_translation(other_page) ⇒ Object
Links another page as a translation of this one, merging any existing groups so all members share a single translation_group_id.
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'app/models/lato_cms/page.rb', line 49 def link_translation(other_page) return false unless other_page && other_page.id != id return false unless other_page.lato_spaces_group_id == lato_spaces_group_id group_id = translation_group_id.presence || other_page.translation_group_id.presence || SecureRandom.uuid old_group_id = other_page.translation_group_id.presence transaction do update!(translation_group_id: group_id) # Re-point every member of the other page's former group to keep groups merged. if old_group_id && old_group_id != group_id LatoCms::Page .for_lato_spaces_group(lato_spaces_group_id) .where(translation_group_id: old_group_id) .update_all(translation_group_id: group_id) else other_page.update!(translation_group_id: group_id) end end true end |
#set_component_enabled(template_component_id, enabled) ⇒ Object
115 116 117 118 |
# File 'app/models/lato_cms/page.rb', line 115 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 |
#template ⇒ Object
80 81 82 |
# File 'app/models/lato_cms/page.rb', line 80 def template LatoCms::TemplateManager.find_template(template_id) end |
#template_available? ⇒ Boolean
88 89 90 |
# File 'app/models/lato_cms/page.rb', line 88 def template_available? template_id.blank? || template.present? end |
#template_components ⇒ Object
92 93 94 95 |
# File 'app/models/lato_cms/page.rb', line 92 def template_components return [] unless template LatoCms::TemplateManager.resolve_template_components(template) end |
#template_name ⇒ Object
84 85 86 |
# File 'app/models/lato_cms/page.rb', line 84 def template_name template&.dig('name') || template_id end |
#translation_candidates(locale) ⇒ Object
Pages of a given locale that can be linked to this page: same spaces group, not this page, and not already part of any translation group.
40 41 42 43 44 45 |
# File 'app/models/lato_cms/page.rb', line 40 def translation_candidates(locale) LatoCms::Page .for_lato_spaces_group(lato_spaces_group_id) .where(locale: locale.to_s, translation_group_id: nil) .where.not(id: id) end |
#translation_for(locale) ⇒ Object
The linked translation for a given locale, if any.
34 35 36 |
# File 'app/models/lato_cms/page.rb', line 34 def translation_for(locale) translations.find_by(locale: locale.to_s) end |
#translations ⇒ Object
Sibling pages linked as translations of this page.
24 25 26 27 28 29 30 31 |
# File 'app/models/lato_cms/page.rb', line 24 def translations return LatoCms::Page.none if translation_group_id.blank? LatoCms::Page .for_lato_spaces_group(lato_spaces_group_id) .where(translation_group_id: translation_group_id) .where.not(id: id) end |
#unlink_translation(other_page) ⇒ Object
Removes a page from this page's translation group.
73 74 75 76 77 78 |
# File 'app/models/lato_cms/page.rb', line 73 def unlink_translation(other_page) return false unless other_page && other_page.translation_group_id == translation_group_id other_page.update!(translation_group_id: nil) true end |