Class: Comfy::Cms::Page

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
WithCategories, WithFragments
Defined in:
app/models/comfy/cms/page.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#contentObject

Returns the value of attribute content.



13
14
15
# File 'app/models/comfy/cms/page.rb', line 13

def content
  @content
end

Class Method Details

.options_for_select(site:, current_page: nil, exclude_self: false) ⇒ Object

– Class Methods ———————————————————– Tree-like structure for pages



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/models/comfy/cms/page.rb', line 50

def self.options_for_select(site:, current_page: nil, exclude_self: false)
  options = []

  options_for_page = ->(page, depth = 0) do
    return if exclude_self && page == current_page

    options << ["#{'. . ' * depth}#{page.label}", page.id]

    page.children.order(:position).each do |child_page|
      options_for_page.call(child_page, depth + 1)
    end
  end

  options_for_page.call(site.pages.root)

  options
end

Instance Method Details

#full_pathObject

– Instance Methods ——————————————————– For previewing purposes sometimes we need to have full_path set. This full path take care of the pages and its childs but not of the site path



71
72
73
# File 'app/models/comfy/cms/page.rb', line 71

def full_path
  read_attribute(:full_path) || assign_full_path
end

#identifierObject

Somewhat unique method of identifying a page that is not a full_path



76
77
78
# File 'app/models/comfy/cms/page.rb', line 76

def identifier
  parent_id.blank? ? "index" : full_path[1..-1].parameterize
end

#translate!Object

This method will mutate page object by transfering attributes from translation for a given locale.



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'app/models/comfy/cms/page.rb', line 87

def translate!
  # If site locale same as page's or there's no translastions, we do nothing
  if site.locale == I18n.locale.to_s || translations.blank?
    return
  end

  translation = translations.published.find_by!(locale: I18n.locale)
  self.layout        = translation.layout
  self.label         = translation.label
  self.content_cache = translation.content_cache

  # We can't just assign fragments as it's a relation and will write to DB
  # This has odd side-effect of preserving page's fragments and just replacing
  # them from the translation. Not an issue if all fragments match.
  self.fragments_attributes = translation.fragments_attributes
  readonly!

  self
end

#url(relative: false) ⇒ Object

Full url for a page



81
82
83
# File 'app/models/comfy/cms/page.rb', line 81

def url(relative: false)
  [site.url(relative: relative), full_path].compact.join
end