Sha256: bb810a0c96e0e668bd4daadd4df105a180e68011114063037507667193815fa9

Contents?: true

Size: 1.62 KB

Versions: 4

Compression:

Stored size: 1.62 KB

Contents

module PandaCms
  class PagesController < ApplicationController
    include ActionView::Helpers::TagHelper

    def root
      params[:path] = ""
      show
    end

    def show
      if PandaCms.config.require_login_to_view && !user_signed_in?
        redirect_to panda_cms_maintenance_path and return
      end

      path_to_find = "/" + params[:path].to_s
      page = Page.find_by(path: path_to_find) || Page.find_by(path: "/404")
      PandaCms::Current.page = page
      layout = page&.template&.file_path

      # TODO: If page is active?
      if page && layout
        globals = {
          page: page,
          title: page.title
        }

        unless ignore_visit?
          RecordVisitJob.perform_later(
            url: request.url,
            user_agent: request.user_agent,
            referrer: request.referrer,
            ip_address: request.remote_ip,
            page_id: page.id,
            current_user_id: current_user&.id,
            params: params.to_unsafe_h.except(:controller, :action, :path),
            visited_at: Time.zone.now
          )
        end

        render inline: "", assigns: globals, status: :ok, layout: layout
      else
        # This works for now, but we may want to override in future (e.g. custom 404s)
        render file: "#{Rails.root}/public/404.html", layout: false, status: :not_found
      end
    end

    private

    def ignore_visit?
      # Ignore visits from bots (TODO: make this configurable)
      return true if /bot/i.match?(request.user_agent)
      # Ignore visits from Honeybadger
      return true if request.headers.to_h.key? "Honeybadger-Token"

      false
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
panda_cms-0.6.3 app/controllers/panda_cms/pages_controller.rb
panda_cms-0.6.2 app/controllers/panda_cms/pages_controller.rb
panda_cms-0.6.1 app/controllers/panda_cms/pages_controller.rb
panda_cms-0.6.0 app/controllers/panda_cms/pages_controller.rb