Sha256: d745483778a78df9e7a2d78b0c471a65060a3de55ad90f5687e68b1949b89b70

Contents?: true

Size: 1.17 KB

Versions: 3

Compression:

Stored size: 1.17 KB

Contents

# frozen_string_literal: true

# Abstract controller for all frontend controllers.
module PagesCore
  class FrontendController < ::ApplicationController
    include ApplicationHelper

    before_action :set_i18n_locale
    helper_method :page_param

    # Loads @root_pages and @rss_feeds. To automatically load these in your
    # own controllers, add the following line to your controller definition:
    #
    #   before_action :load_root_pages
    #
    def load_root_pages
      @root_pages = Page.roots.localized(content_locale).published
      @rss_feeds = Page.where(feed_enabled: true)
                       .localized(content_locale)
                       .published
    end

    private

    def legacy_locales
      { "nor" => "nb",
        "eng" => "en" }
    end

    def page_param
      params[:page].is_a?(String) ? params[:page] : 1
    end

    def set_i18n_locale
      locale_param = content_locale
      if legacy_locales[locale_param]
        locale_param = legacy_locales[locale_param]
      end
      I18n.locale = locale_param
    rescue I18n::InvalidLocale
      raise if Rails.application.config.consider_all_requests_local

      render_error 404
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
pages_core-3.12.7 app/controllers/pages_core/frontend_controller.rb
pages_core-3.12.6 app/controllers/pages_core/frontend_controller.rb
pages_core-3.12.5 app/controllers/pages_core/frontend_controller.rb