Sha256: a515b421c6892aed8fc9134c1dd589c7d8bcb7d538dda5c2c6b5effb08e0fcae

Contents?: true

Size: 1.32 KB

Versions: 12

Compression:

Stored size: 1.32 KB

Contents

module RailsConnector

  class CmsEnv

    OBJ_ENV_KEY = "INFOPARK_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?
          permalink =  params[:permalink].to_a.join("/")
          Obj.find_by_permalink!(permalink)
        else
          if callback = RailsConnector::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

      if found_obj.suppressed?
        raise RailsConnector::ResourceNotFound,
          "Tried to access Obj #{found_obj.inspect}, but it is suppressed (suppress_export is set)!"
      end

      found_obj
    end

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

  end

end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
infopark_rails_connector-6.8.0.539.93842523 lib/rails_connector/cms_env.rb
infopark_rails_connector-6.8.0.529.114081979 lib/rails_connector/cms_env.rb
infopark_rails_connector-6.8.0.518.30999728 lib/rails_connector/cms_env.rb
infopark_rails_connector-6.8.0.515.34928522 lib/rails_connector/cms_env.rb
infopark_rails_connector-6.8.0.498.46559598 lib/rails_connector/cms_env.rb
infopark_rails_connector-6.8.0.480.261594408 lib/rails_connector/cms_env.rb
infopark_rails_connector-6.8.0.444.171626367 lib/rails_connector/cms_env.rb
infopark_rails_connector-6.8.0.406.131718077 lib/rails_connector/cms_env.rb
infopark_rails_connector-6.8.0.356.19698103 lib/rails_connector/cms_env.rb
infopark_rails_connector-6.8.0.348.160665197 lib/rails_connector/cms_env.rb
infopark_rails_connector-6.8.0.328.4a753fc lib/rails_connector/cms_env.rb
infopark_rails_connector-6.8.0.322.c003f11 lib/rails_connector/cms_env.rb