Module: Alchemy::PagesHelper

Includes:
BaseHelper, ElementsHelper
Defined in:
app/helpers/alchemy/pages_helper.rb

Instance Method Summary collapse

Methods included from ElementsHelper

#element_preview_code, #element_preview_code_attributes, #element_tags, #element_tags_attributes, #render_element, #render_elements

Methods included from ElementsBlockHelper

#element_view_for

Methods included from UrlHelper

#download_alchemy_attachment_path, #download_alchemy_attachment_url, #show_alchemy_page_path, #show_alchemy_page_url, #show_page_path_params

Methods included from BaseHelper

#message_icon_class, #page_or_find, #render_flash_notice, #render_icon, #render_message, #shorten, #warning

Instance Method Details

Renders links to language root pages of all published languages.

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • linkname (String) — default: 'name'

    Renders name/code of language, or I18n translation for code.

  • show_title (Boolean) — default: true

    Renders title attributes for the links.

  • spacer (String) — default: ''

    Renders the passed spacer string. You can also overwrite the spacer partial: “alchemy/language_links/_spacer”.

  • reverse (Boolean) — default: false

    Reverses the ordering of the links.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/helpers/alchemy/pages_helper.rb', line 22

def language_links(options = {})
  options = {
    linkname: "name",
    show_title: true,
    spacer: "",
    reverse: false
  }.merge(options)
  languages = Language.on_current_site.published.with_root_page.order("name #{options[:reverse] ? "DESC" : "ASC"}")
  return nil if languages.count < 2

  render(
    partial: "alchemy/language_links/language",
    collection: languages,
    spacer_template: "alchemy/language_links/spacer",
    locals: {languages: languages, options: options}
  )
end

#meta_descriptionObject



161
162
163
# File 'app/helpers/alchemy/pages_helper.rb', line 161

def meta_description
  @page.meta_description.presence || Language.current_root_page.try(:meta_description)
end

#meta_keywordsObject



165
166
167
# File 'app/helpers/alchemy/pages_helper.rb', line 165

def meta_keywords
  @page.meta_keywords.presence || Language.current_root_page.try(:meta_keywords)
end

#meta_robotsObject



169
170
171
# File 'app/helpers/alchemy/pages_helper.rb', line 169

def meta_robots
  "#{@page.robot_index? ? "" : "no"}index, #{@page.robot_follow? ? "" : "no"}follow"
end

#page_title(options = {}) ⇒ Object

Returns current page title

Options:

prefix: ""                 # Prefix
separator: ""              # Separating prefix and title


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

def page_title(options = {})
  return "" if @page.title.blank?

  options = {
    prefix: "",
    suffix: "",
    separator: ""
  }.update(options)
  title_parts = [options[:prefix]]
  title_parts << if response.status == 200
    @page.title
  else
    response.status
  end
  title_parts << options[:suffix]
  title_parts.reject(&:blank?).join(options[:separator]).html_safe
end

#render_breadcrumb(options = {}) ⇒ Object

Returns page links in a breadcrumb beginning from root to current page.

Options:

separator: %(<span class="separator">></span>)      # Maybe you don't want this separator. Pass another one.
page: @page                                         # Pass a different Page instead of the default (@page).
without: nil                                        # Pass Page object or array of Pages that must not be displayed.
restricted_only: false                              # Pass boolean for displaying restricted pages only.
reverse: false                                      # Pass boolean for displaying breadcrumb in reversed reversed.


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
133
134
# File 'app/helpers/alchemy/pages_helper.rb', line 107

def render_breadcrumb(options = {})
  options = {
    separator: ">",
    page: @page,
    restricted_only: false,
    reverse: false,
    link_active_page: false
  }.merge(options)

  pages = options[:page]
    .self_and_ancestors.contentpages
    .published

  if options.delete(:restricted_only)
    pages = pages.restricted
  end

  if options.delete(:reverse)
    pages = pages.reorder("lft DESC")
  end

  if options[:without].present?
    without = options.delete(:without)
    pages = pages.where.not(id: without.try(:collect, &:id) || without.id)
  end

  render "alchemy/breadcrumb/wrapper", pages: pages, options: options
end

#render_menu(menu_type, options = {}) ⇒ Object

Renders a menu partial

Menu partials are placed in the ‘app/views/alchemy/menus` folder Use the `rails g alchemy:menus` generator to create the partials

Parameters:

  • - (String)

    Type of the menu

  • - (Hash)

    A set of options available in your menu partials



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'app/helpers/alchemy/pages_helper.rb', line 79

def render_menu(menu_type, options = {})
  root_node = Alchemy::Node.roots.find_by(
    menu_type: menu_type,
    language: Alchemy::Language.current
  )
  if root_node.nil?
    warning("Menu with type #{menu_type} not found!")
    return
  end

  render("alchemy/menus/#{menu_type}/wrapper", menu: root_node, options: options)
rescue ActionView::MissingTemplate => e
  warning <<~WARN
    Menu partial not found for #{menu_type}.
    #{e}
  WARN
end

#render_page_layoutObject

Renders the layout for current page.

Page layout files belongs in /app/views/alchemy/page_layouts/

Falls back to /app/views/alchemy/page_layouts/standard if the page_layout partial is not found.



46
47
48
49
50
51
# File 'app/helpers/alchemy/pages_helper.rb', line 46

def render_page_layout
  render @page, page: @page
rescue ActionView::MissingTemplate
  warning("PageLayout: '#{@page.page_layout}' not found. Rendering standard page_layout.")
  render "alchemy/page_layouts/standard", page: @page
end

#render_site_layout(&block) ⇒ Object

Renders a partial for current site

Place a rails partial into app/views/alchemy/site_layouts

and name it like your site name.

Example:

<%= render_site_layout %>

renders app/views/alchemy/site_layouts/_default_site.html.erb for the site named “Default Site”.



65
66
67
68
69
70
# File 'app/helpers/alchemy/pages_helper.rb', line 65

def render_site_layout(&block)
  render current_alchemy_site, &block
rescue ActionView::MissingTemplate
  warning("Site layout for #{current_alchemy_site.try(:name)} not found. Please run `rails g alchemy:site_layouts`")
  ""
end