Sha256: 83723dbd02a5d296bf4d98e841f32c3960bf478ebeef5bc70b607c54e293fe17

Contents?: true

Size: 1.24 KB

Versions: 29

Compression:

Stored size: 1.24 KB

Contents

module Scrivito

  class CmsEnv

    OBJ_ENV_KEY = "SCRIVITO_OBJ"

    def initialize(env)
      @env = env
    end

    def load
      return if env[OBJ_ENV_KEY]
      load_object
    end

    protected

    attr_reader :env

    def load_object
      env[OBJ_ENV_KEY] =
        begin
          find_obj
        rescue StandardError => e
          e
        end
    end

    def find_obj
      found_obj =
        if params[:id]
          Obj.find(params[:id])
        elsif params[:permalink].present?
          self.class.find_permalink_by_param(params[:permalink])
        else
          if callback = Scrivito::Configuration.choose_homepage_callback
            callback_result = callback.call(env)
            if callback_result.is_a?(Obj)
              callback_result
            else
              raise "choose_homepage callback did not return an Obj. "\
                "Instead saw #{callback_result.class}."
            end
          else
            Obj.homepage
          end
        end

      found_obj
    end

    def params
      env["action_dispatch.request.path_parameters"]
    end

    def self.find_permalink_by_param(permalink_param)
      permalink =  Array(permalink_param).join("/")
      Obj.find_by_permalink!(permalink)
    end
  end
end

Version data entries

29 entries across 29 versions & 1 rubygems

Version Path
scrivito_sdk-0.41.1 lib/scrivito/cms_env.rb
scrivito_sdk-0.41.0 lib/scrivito/cms_env.rb
scrivito_sdk-0.41.0.rc2 lib/scrivito/cms_env.rb
scrivito_sdk-0.41.0.rc1 lib/scrivito/cms_env.rb
scrivito_sdk-0.40.0 lib/scrivito/cms_env.rb
scrivito_sdk-0.40.0.rc2 lib/scrivito/cms_env.rb
scrivito_sdk-0.40.0.rc1 lib/scrivito/cms_env.rb
scrivito_sdk-0.30.0 lib/scrivito/cms_env.rb
scrivito_sdk-0.30.0.rc1 lib/scrivito/cms_env.rb