Module: DmCms::PagesHelper

Defined in:
app/helpers/dm_cms/pages_helper.rb

Overview

Handles building the different content streams for a page


Instance Method Summary collapse

Instance Method Details

#allow_page_in_menu?(page) ⇒ Boolean

return true if the page should be allowed to be dislpayed in a menu


Returns:

  • (Boolean)


137
138
139
# File 'app/helpers/dm_cms/pages_helper.rb', line 137

def allow_page_in_menu?(page)
  page.present? && (page.is_published? || is_admin?) && page_authorized?(page) && !page.menutitle.blank?    
end

#content_by_name(name) ⇒ Object

Given the name of a container, queries for all content items for that container within the given page.




15
16
17
18
19
20
# File 'app/helpers/dm_cms/pages_helper.rb', line 15

def content_by_name( name )
  unless @current_page.nil?
    items = @current_page.cms_contentitems.where(container: name)
    render :partial => (items.nil? ? 'not_found' : 'content_fragment'), :collection => items
  end
end

#content_by_name?(name) ⇒ Boolean

Given the name of a container, check if any content is available


Returns:

  • (Boolean)


24
25
26
# File 'app/helpers/dm_cms/pages_helper.rb', line 24

def content_by_name?(name)
  (@current_page.nil? || @current_page.cms_contentitems.where(container: name).count == 0) ? false : true
end

Generates a simple multi-level page menu including children




67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'app/helpers/dm_cms/pages_helper.rb', line 67

def main_menu(options = {})
  return '' if (root = CmsPage.roots[0]).nil?

  menu_str                 = ''
  options[:ul]             = ''
  options[:ul]            += "class='#{options[:class]}' "  unless options[:class].blank?
  options[:ul]            += "id='#{options[:id]}' "        unless options[:id].blank?
  options[:include_root]   = root                           if options[:include_home]
  options[:active_class] ||= 'current'
  children                 = root.subtree.includes(:translations).arrange(order: :row_order).to_a[0][1]
  menu_str, submenu_active = (options[:type] == :bs3 ? menu_from_pages_bs3(children, options) : menu_from_pages(children, options))
  return menu_str.html_safe
end



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'app/helpers/dm_cms/pages_helper.rb', line 142

def main_menu_select(options = {})
  return '' if (root = CmsPage.roots[0]).nil?
  options[:id]            ||= ''
  options[:class]         ||= ''
  options[:include_root]    = root if options[:include_home]
  pages                     = root.subtree.includes(:translations).arrange(order: :row_order).to_a[0][1]
  menu_str                  = "<select id='#{options[:id]}' class='#{options[:class]}'>"
  menu_str                 += "<option value='' selected='selected'>#{nls(:main_menu_select_prompt)}</option>"
  if options[:include_home]
    menu_str += "<option value='#{dm_cms.showpage_url(root.slug)}'>#{root.menutitle}</option>"
  end
  pages.each do |page, children|
    if allow_page_in_menu?(page)
      menu_str += "<option value='#{dm_cms.showpage_url(page.slug)}'>#{page.menutitle}</option>"
    end
  end
  menu_str += "</select>"
  return menu_str.html_safe
end



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'app/helpers/dm_cms/pages_helper.rb', line 82

def menu_from_pages(pages, options = {})
  options[:ul]  ||= ''
  menu_str        = ''
  active_found    = false
  if (root = options[:include_root])
    active          = (current_page?(root) ? options[:active_class] : nil)
    active_found  ||= !active.nil?
    menu_str       +=  :li, page_link(root), class: active
  end
  pages.each do |page, children|
    if allow_page_in_menu?(page)
      submenu, submenu_active = (children.empty? ? '' : menu_from_pages(children, active_class: options[:active_class]))
      active                  = (submenu_active || current_page?(page) ? options[:active_class] : nil)
      active_found          ||= !active.nil?
      menu_str += (:li, class: active) do
        page_link(page) + 
        submenu.html_safe
      end
    end
  end
  return (menu_str.blank? ? '' : "<ul #{options[:ul]}>#{menu_str}</ul>"), active_found
end

Creates a standard Bootstrap 3 version of a main menu




107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'app/helpers/dm_cms/pages_helper.rb', line 107

def menu_from_pages_bs3(pages, options = {})
  options[:ul]  ||= ''
  menu_str        = ''
  active_found    = false
  if (root = options[:include_root])
    active          = (current_page?(root) ? options[:active_class] : nil)
    active_found  ||= !active.nil?
    menu_str       +=  :li, page_link(root), class: active
  end
  pages.each do |page, children|
    if allow_page_in_menu?(page)
      submenu, submenu_active = (children.empty? ? '' : menu_from_pages_bs3(children, ul: 'class="dropdown-menu"', active_class: options[:active_class]))
      active                  = (submenu_active || current_page?(page) ? options[:active_class] : nil)
      active_found          ||= !active.nil?
      if !submenu.blank?
        menu_str += (:li, class: ['dropdown', active].join(' ')) do
          page_link(page, ''.html_safe + page.menutitle + ' <b class="caret"></b>'.html_safe, class: 'dropdown-toggle', data: {toggle: 'dropdown'}) +
          submenu.html_safe
        end
      else
        menu_str +=  :li, page_link(page), class: active
      end
    end
  end
  return (menu_str.blank? ? '' : "<ul #{options[:ul]}>#{menu_str}</ul>"), active_found
end

return a link to the page’s slug, with the passed in link text




164
165
166
167
168
169
170
171
172
173
174
# File 'app/helpers/dm_cms/pages_helper.rb', line 164

def page_link(page, text = nil, options = {})
  text ||= page.menutitle
  case page.pagetype
  when 'link'
    link_to text, page.link || dm_cms.showpage_url(page.slug), options
  when 'link-new-window'
    link_to text, page.link || dm_cms.showpage_url(page.slug), options.merge(target: '_blank')
  else
    link_to text, dm_cms.showpage_url(page.slug), options
  end
end

#page_url(slug, locale = DmCore::Language.locale) ⇒ Object

Easy way to get a page url slug can either be a string, or a CmsPage object




8
9
10
# File 'app/helpers/dm_cms/pages_helper.rb', line 8

def page_url(slug, locale = DmCore::Language.locale)
  showpage_url(:locale => locale, :slug => (slug.kind_of?(CmsPage) ? slug.slug : slug))
end

#render_content_item(content_item) ⇒ Object




44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'app/helpers/dm_cms/pages_helper.rb', line 44

def render_content_item(content_item)
  if content_item.content.blank?
    content = ''
  else
    # --- process content type
    liquid_params = content_item.to_liquid
    liquid_params.reverse_merge!(current_user.to_liquid) if current_user
    case content_item.itemtype.downcase
      when 'textile'
        content = liquidize_textile(content_item.content, liquid_params)
      when 'markdown'
        content = liquidize_markdown(content_item.content, liquid_params)
      when 'html'
        content = liquidize_html(content_item.content, liquid_params)
      else
        content = ''
    end
  end
  return content
end

#snippet(slug) ⇒ Object




29
30
31
32
33
34
35
36
# File 'app/helpers/dm_cms/pages_helper.rb', line 29

def snippet(slug)
  cms_snippet = CmsSnippet.find_by_slug(slug)
  if cms_snippet
    render :partial => 'dm_cms/pages/snippet_fragment', locals: {snippet_fragment: cms_snippet}
  else
    render text: 'Snippet not found'
  end
end

#snippet?(slug) ⇒ Boolean


Returns:

  • (Boolean)


39
40
41
# File 'app/helpers/dm_cms/pages_helper.rb', line 39

def snippet?(slug)
  CmsSnippet.where(slug: slug).count == 0 ? false : true
end