Skip to content

Commit

Permalink
resolve page & redirect constants before finding
Browse files Browse the repository at this point in the history
  • Loading branch information
jpowell committed Jan 14, 2019
1 parent 971687c commit 375375e
Showing 1 changed file with 11 additions and 3 deletions.
Expand Up @@ -5,24 +5,32 @@ class WCC::Contentful::App::PagesController < ApplicationController

def index
@page = global_site_config&.homepage ||
WCC::Contentful::Model::Page.find_by(slug: '/', options: { include: 3 })
page_model.find_by(slug: '/', options: { include: 3 })
render 'pages/show'
end

def show
slug = '/' + params[:slug]
@page = WCC::Contentful::Model::Page.find_by(slug: slug, options: { include: 3 })
@page = page_model.find_by(slug: slug, options: { include: 3 })

return render 'pages/show' if @page

redirect = WCC::Contentful::Model::Redirect.find_by(slug: slug, options: { include: 0 })
redirect = redirect_model.find_by(slug: slug, options: { include: 0 })
raise WCC::Contentful::App::PageNotFoundError, slug unless redirect

redirect_to redirect.href
end

private

def page_model
WCC::Contentful::Model.resolve_constant('page')
end

def redirect_model
WCC::Contentful::Model.resolve_constant('redirect')
end

def global_site_config
# They may have not installed `site-config` in the project
return unless defined?(WCC::Contentful::Model::SiteConfig)
Expand Down

0 comments on commit 375375e

Please sign in to comment.