Class: Comfy::Cms::Page

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.options_for_select(site:, page: nil, current_page: nil, depth: 0, exclude_self: true, spacer: ". . ") ⇒ Object

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



48
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/comfy/cms/page.rb', line 48

def self.options_for_select(site:, page: nil, current_page: nil, depth: 0, exclude_self: true, spacer: ". . ")
  return [] if (current_page ||= site.pages.root) == page && exclude_self || !current_page
  out = []

  unless current_page == page
    out << ["#{spacer * depth}#{current_page.label}", current_page.id]
  end

  if current_page.children_count.nonzero?
    current_page.children.each do |child|
      out += options_for_select(
        site:         site,
        page:         page,
        current_page: child,
        depth:        depth + 1,
        exclude_self: exclude_self,
        spacer:       spacer
      )
    end
  end

  out.compact
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



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

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



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

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

#translate!(locale) ⇒ Object

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



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

def translate!(locale)
  translation = translations.published.find_by!(locale: 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



85
86
87
# File 'app/models/comfy/cms/page.rb', line 85

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