Sha256: ea1abf9a4a9e52e7c83945857df8b7e7cd4ac39b190b267d863246ece29539bd

Contents?: true

Size: 1.22 KB

Versions: 16

Compression:

Stored size: 1.22 KB

Contents

# frozen_string_literal: true

class WCC::Contentful::App::PagesController < ApplicationController
  include WCC::Contentful::App::PreviewPassword

  helper ::WCC::Contentful::App::SectionHelper

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

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

    return render 'pages/show' if @page

    redirect = redirect_model.find_by(slug: slug, options: { include: 0, preview: preview? })
    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 site_config_model
    # They may have not installed `site-config` in the project
    WCC::Contentful::Model.resolve_constant('site-config')
  rescue WCC::Contentful::ContentTypeNotFoundError
    nil
  end

  def global_site_config
    return unless model = site_config_model

    @global_site_config ||= model.instance(preview?)
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
wcc-contentful-app-1.2.0 app/controllers/wcc/contentful/app/pages_controller.rb
wcc-contentful-app-1.1.2 app/controllers/wcc/contentful/app/pages_controller.rb
wcc-contentful-app-1.1.1 app/controllers/wcc/contentful/app/pages_controller.rb
wcc-contentful-app-1.1.0 app/controllers/wcc/contentful/app/pages_controller.rb
wcc-contentful-app-1.0.8 app/controllers/wcc/contentful/app/pages_controller.rb
wcc-contentful-app-1.0.7 app/controllers/wcc/contentful/app/pages_controller.rb
wcc-contentful-app-1.0.6 app/controllers/wcc/contentful/app/pages_controller.rb
wcc-contentful-app-1.0.5 app/controllers/wcc/contentful/app/pages_controller.rb
wcc-contentful-app-1.0.4 app/controllers/wcc/contentful/app/pages_controller.rb
wcc-contentful-app-1.0.3 app/controllers/wcc/contentful/app/pages_controller.rb
wcc-contentful-app-1.0.2 app/controllers/wcc/contentful/app/pages_controller.rb
wcc-contentful-app-1.0.1 app/controllers/wcc/contentful/app/pages_controller.rb
wcc-contentful-app-1.0.0 app/controllers/wcc/contentful/app/pages_controller.rb
wcc-contentful-app-1.0.0.pre.rc3 app/controllers/wcc/contentful/app/pages_controller.rb
wcc-contentful-app-1.0.0.pre.rc2 app/controllers/wcc/contentful/app/pages_controller.rb
wcc-contentful-app-1.0.0.pre.rc1 app/controllers/wcc/contentful/app/pages_controller.rb