Sha256: 9e6ad16f0ef38ed7a61b7f5e2dc029873cbf0833856ab70ac183eb5b776536bc

Contents?: true

Size: 923 Bytes

Versions: 1

Compression:

Stored size: 923 Bytes

Contents

require_dependency "petrie/application_controller"

module Petrie
  class PagesController < ApplicationController
    rescue_from ActiveRecord::RecordNotFound, with: :not_found

    def home
    end

    def show
      @page = find_by_slug
    end

  private

    def find_by_slug
      page_slugs = params[:page].split(/\//).reverse
      candidates = Page.where(slug: page_slugs.first)

      return not_found if candidates.empty?
      return candidates.first if candidates.count == 1

      resolve_duplicates(page_slugs, candidates)
    end

    def resolve_duplicates(page_slugs, candidates)
      page_slugs.shift
      page_slugs.each do |page|
        parent_candidate = Page.friendly.find(page)

        candidates.each do |c|
          return c if parent_candidate.is_ancestor_of?(c)
        end
      end
    end

    def not_found
      raise ActionController::RoutingError.new('Not Found')
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
petrie-0.1.0 app/controllers/petrie/pages_controller.rb