Sha256: e6fa511547942d97c71423e83a39bf294ab4891c5b0f9dde43db216d3edf25ff

Contents?: true

Size: 1.67 KB

Versions: 2

Compression:

Stored size: 1.67 KB

Contents

# encoding: utf-8

# Methods added to this helper will be available to all templates
# in the application.
module PagesCore
  module ApplicationHelper
    include PagesCore::HeadTagsHelper
    include PagesCore::ImagesHelper

    def page_link(page, options = {})
      link_locale = options[:locale] || locale
      page.localize(link_locale) do |p|
        title = options[:title] || p.name.to_s
        return title unless conditional_options?(options)
        url = if p.redirects?
                p.redirect_path(locale: link_locale)
              else
                page_path(link_locale, p)
              end
        link_to(title, url, class: options[:class])
      end
    end

    def page_url(page_or_locale, page = nil, options = {})
      if page
        locale = page_or_locale
      else
        ActiveSupport::Deprecation.warn(
          "Calling page_url without locale is deprecated"
        )
        locale = options[:locale] || @locale
        page = page_or_locale
      end
      page.localize(locale) do |p|
        if p.redirects?
          p.redirect_path(locale: locale)
        else
          super locale, p, options
        end
      end
    end

    def unique_page(page_name, &block)
      locale = @locale || I18n.default_locale.to_s
      page = Page.where(unique_name: page_name).first
      if page && block_given?
        output = capture(page.localize(locale), &block)
        concat(output)
      end
      (page) ? page.localize(locale) : nil
    end

    private

    def conditional_options?(options = {})
      if options.key?(:if)
        options[:if]
      elsif options.key?(:unless)
        !options[:unless]
      else
        true
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
pages_core-3.4.3 app/helpers/pages_core/application_helper.rb
pages_core-3.4.2 app/helpers/pages_core/application_helper.rb