Class: Comfy::Cms::Page
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Comfy::Cms::Page
- Includes:
- WithCategories, WithFragments
- Defined in:
- app/models/comfy/cms/page.rb
Instance Attribute Summary collapse
-
#content ⇒ Object
Returns the value of attribute content.
Class Method Summary collapse
-
.options_for_select(site:, current_page: nil, exclude_self: false) ⇒ Object
– Class Methods ———————————————————– Tree-like structure for pages.
Instance Method Summary collapse
-
#full_path ⇒ Object
– Instance Methods ——————————————————– For previewing purposes sometimes we need to have full_path set.
-
#identifier ⇒ Object
Somewhat unique method of identifying a page that is not a full_path.
-
#translate! ⇒ Object
This method will mutate page object by transfering attributes from translation for a given locale.
-
#url(relative: false) ⇒ Object
Full url for a page.
Instance Attribute Details
#content ⇒ Object
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 67 |
# File 'app/models/comfy/cms/page.rb', line 50 def self.(site:, current_page: nil, exclude_self: false) = [] = ->(page, depth = 0) do return if page.nil? return if exclude_self && page == current_page << ["#{'. . ' * depth}#{page.label}", page.id] page.children.order(:position).each do |child_page| .call(child_page, depth + 1) end end .call(site.pages.root) end |
Instance Method Details
#full_path ⇒ Object
– 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
72 73 74 |
# File 'app/models/comfy/cms/page.rb', line 72 def full_path read_attribute(:full_path) || assign_full_path end |
#identifier ⇒ Object
Somewhat unique method of identifying a page that is not a full_path
77 78 79 |
# File 'app/models/comfy/cms/page.rb', line 77 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.
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'app/models/comfy/cms/page.rb', line 88 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
82 83 84 |
# File 'app/models/comfy/cms/page.rb', line 82 def url(relative: false) [site.url(relative: relative), full_path].compact.join end |