Sha256: f27aedf6e951adb07dde26f84d3633c060a40ede02202ad135f877796a9cd8dd

Contents?: true

Size: 1.82 KB

Versions: 4

Compression:

Stored size: 1.82 KB

Contents

module Enjoy::SeoPages
  extend ActiveSupport::Concern
  included do
    before_filter :find_page
  end

  private
  def find_page
    return if rails_admin?
    @seo_page = find_seo_page request.path
    if !@seo_page.nil? && !@seo_page.redirect.blank?
      redirect_to @seo_page.redirect, status: :moved_permanently
    end
  end

  def find_seo_page(path)
    do_redirect = false
    if path[0] != '/'
      path = '/' + path
    end
    if path.length > 1 && path[-1] == '/'
      path = path[0..-2]
      do_redirect = true
    end
    page = Enjoy::Page.enabled.where(fullpath: path).first

    if page.nil? && !params[:slug].blank?
      page = Enjoy::Page.enabled.where(fullpath: "/" + params[:slug]).first
    end

    if page.nil?
      page = find_seo_extra(path)
    end

    if page.nil?
      do_redirect = true
      spath = path.chomp(File.extname(path))
      if spath != path
        page = Enjoy::Page.enabled.where(fullpath: spath).first
      end
    end

    if !page.nil? && do_redirect
      redirect_to path, status: :moved_permanently
    end

    page
  end

  def find_seo_page_with_redirect(path)
    do_redirect = false
    if path[0] != '/'
      path = '/' + path
    end
    if path.length > 1 && path[-1] == '/'
      path = path[0..-2]
      do_redirect = true
    end

    page = Page.enabled.any_of({fullpath: path}, {redirect: path}).first
    if page.nil?
      do_redirect = true
      spath = path.chomp(File.extname(path))
      if spath != path
        page = Enjoy::Page.enabled.any_of({fullpath: spath}, {redirect: spath}).first
      end
    end
    if !page.nil? && do_redirect
      redirect_to path, status: :moved_permanently
    end

    page

  end

  def find_seo_extra(path)
    nil
  end

  def rails_admin?
    self.is_a?(RailsAdmin::ApplicationController) || self.is_a?(RailsAdmin::MainController)
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
enjoy_cms-0.2.0.2 app/controllers/concerns/enjoy/seo_pages.rb
enjoy_cms-0.2.0.1.beta app/controllers/concerns/enjoy/seo_pages.rb
enjoy_cms-0.2.0.beta app/controllers/concerns/enjoy/seo_pages.rb
enjoy_cms-0.1.0.beta1 app/controllers/concerns/enjoy/seo_pages.rb