Class: CmsPage

Inherits:
ActiveRecord::Base
  • Object
show all
Extended by:
FriendlyId
Includes:
DmCore::Concerns::FriendlyId, RankedModel
Defined in:
app/models/cms_page.rb

Overview

Implementation Note: if the ‘menutitle’ is blank, that indicates the page should not be shown in menus. It can still be published and directly linked to, but it should not show up in any auto-generated menus. This gives the ability to have many pages in a section, with some of them ‘hidden’ from the main menu lists, but can still be linked to and shown.


Defined Under Namespace

Classes: Translation

Constant Summary collapse

PAGETYPE =

— list of pagetypes

[['Regular content', 'content'], ["Link to interior page using it's slug", 'pagelink'], ['Link to external page with url', 'link'], ['Link to external page with url (open in new window)', 'link-new-window'], ['Menu divider (no content)', 'divider'], ['Controller/Action (rarely used)', 'controller/action']]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create_default_siteObject

Create a default site. Check if pages exists first, so we can add missing pages to already created sites.




154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'app/models/cms_page.rb', line 154

def self.create_default_site
  #--- index page
  unless (site = CmsPage.find_by_slug('index'))
    site = CmsPage.create(:slug => 'index', :pagetype => 'content', :template => 'index', 
                          :published => true, :title => 'Front Page')
  end

  unless (standard = CmsPage.find_by_slug('standard_pages'))
    standard = site.children.create(slug: 'standard_pages', pagetype: 'pagelink',
                                    published: false, title: 'Standard Pages')
  end
  
  unless CmsPage.find_by_slug('missing')
    standard.children.create( :slug => 'missing', :pagetype => 'content', :template => '404',
                              :published => true, :title => 'Page Missing')
  end
  
  unless CmsPage.find_by_slug('coming_soon')
    standard.children.create( :slug => 'coming_soon', :pagetype => 'content', :template => 'coming_soon',
                              :published => true, :title => 'Coming Soon')
  end
  unless CmsPage.find_by_slug('signup_success')
    standard.children.create( :slug => 'signup_success', :pagetype => 'pagelink',
                              :link => 'index', :published => true, :title => 'Signup Success')
  end
  unless CmsPage.find_by_slug('confirmation_success')
    standard.children.create( :slug => 'confirmation_success', :pagetype => 'pagelink',
                              :link => 'index', :published => true, :title => 'Confirmaton Success')
  end
end

.page_typesObject




118
119
120
# File 'app/models/cms_page.rb', line 118

def self.page_types
  PAGETYPE
end

Instance Method Details

#divider?Boolean

is this a special divider page - which doesn’t get rendered, it’s for adding categories in a sub menu


Returns:

  • (Boolean)


74
75
76
# File 'app/models/cms_page.rb', line 74

def divider?
  pagetype == 'divider'
end

#duplicate_with_associationsObject

todo currently, this mostly works from the console. However, when run from the browser it hangs in some type of infinite loop, inside amoeba_dup. Was unable to track it down, so this function is currently not called anywhere.




189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'app/models/cms_page.rb', line 189

def duplicate_with_associations
  new_page = nil
  new_slug = "duplicate-#{self.slug}"
  if CmsPage.find_by_slug(new_slug)
    #--- already a duplicate page, return nil
    return nil
  else
    CmsPage.paper_trail_off!
    CmsContentitem.paper_trail_off!
    CmsPage::Translation.paper_trail_off!
    CmsContentitem::Translation.paper_trail_off!
    new_page = self.amoeba_dup
    new_page.slug = new_slug
    # new_page.without_versioning do
      new_page.save
    # end
    CmsPage.paper_trail_on!
    CmsContentitem.paper_trail_on!
    CmsPage::Translation.paper_trail_on!
    CmsContentitem::Translation.paper_trail_on!
#       new_page      = self.initialize_dup(self)
#       new_page.slug = new_slug
#       
#       DmCore::Language.language_array.each do |locale|
#         new_page.send("title_#{locale}=",     self.send("title_#{locale}"))     unless self.send("title_#{locale}").nil?
#         new_page.send("menutitle_#{locale}=", self.send("menutitle_#{locale}")) unless self.send("menutitle_#{locale}").nil?
#         new_page.save
#        end
# #      new_page.save
#       new_page.reload
#       
#       # cms_contentitems.each do |content|
#       #   content.deep_clone(new_page.id)
#       # end
  end
  return new_page
end

#header_accent_color(default = '') ⇒ Object




113
114
115
# File 'app/models/cms_page.rb', line 113

def header_accent_color(default = '')
  self.preferred_header_accent_color || default
end

#header_image(default = nil) ⇒ Object

Return the header image, or a default if not specified




108
109
110
# File 'app/models/cms_page.rb', line 108

def header_image(default = nil)
  self.attributes['header_image'] || default
end

#is_published?Boolean

is the page published, based on the publish flag and the publish dates


Returns:

  • (Boolean)


61
62
63
64
# File 'app/models/cms_page.rb', line 61

def is_published?
  # --- {todo} need to hook in the publish dates
  published
end

#model_slugObject

Base the slug on the default locale




55
56
57
# File 'app/models/cms_page.rb', line 55

def model_slug
  send("title_#{Account.current.preferred_default_locale}")
end

#page_templateObject

Return the template name. If it’s empty, the go to each parent until one is found. raise an exception if there is no page template - otherwise error is hidden




82
83
84
85
86
87
# File 'app/models/cms_page.rb', line 82

def page_template
  page = self
  page = page.parent while page.template.blank? && !page.is_root?      
  raise "No template available for page #{self.slug}" if page.template.blank?
  return page.template
end

#published_children(user, options = {include_blank_titles: false}) ⇒ Object

Return a list of published children pages

> user, to check for permissions

> include_blank_titles to true to include pages with blank titles.

do not include by default



94
95
96
97
98
99
100
101
102
103
104
# File 'app/models/cms_page.rb', line 94

def published_children(user, options = {include_blank_titles: false})
  pages = []
  if self.has_children?
    self.children.each do |child|
      if child.is_published? || (user && user.is_admin?)
        pages << child unless child[:menutitle].blank? && !options[:include_blank_titles]
      end
    end
  end
  return pages
end

#show_social_buttons?Boolean


Returns:

  • (Boolean)


67
68
69
# File 'app/models/cms_page.rb', line 67

def show_social_buttons?
  preferred_show_social_buttons?
end

#to_liquidObject

Generate any data to pass when rendering with Liquid




124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'app/models/cms_page.rb', line 124

def to_liquid
  { 'page' => 
    {
      'title'     => self.title,
      'menutitle' => self.menutitle,
      'slug'      => self.slug
    },
    'subscription' =>
    {
      'trial?'    => false,
      'active?'   => true
    }
  }
end

#visited?(cookie_hash) ⇒ Boolean

Check if this page has been cookied. If needed, we will set a cookie, using the page’s slug, to a value of 1. This indicates that the page has been visited. This is only needed in cases where we want to ensure a page has been visited before enabling some other function.

If cookie_hash is empty, then we don’t care if the page has been visited or not, simply return true


Returns:

  • (Boolean)


147
148
149
# File 'app/models/cms_page.rb', line 147

def visited?(cookie_hash)
  return ((cookie_hash.empty? || cookie_hash[slug] == "1") ? true : false)
end