Sha256: f343e967f4e06d1b2342e167fdf8690a6cfbd6e124aef514a137b782b7f84a57

Contents?: true

Size: 1.44 KB

Versions: 13

Compression:

Stored size: 1.44 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?
          self.class.find_permalink_by_param(params[: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

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

  end

end

Version data entries

13 entries across 13 versions & 3 rubygems

Version Path
infopark_fiona_connector-7.0.1.5.2.3.rc4 lib/rails_connector/cms_env.rb
infopark_fiona_connector-7.0.1 lib/rails_connector/cms_env.rb
infopark_fiona_connector-7.0.1.beta2 lib/rails_connector/cms_env.rb
infopark_fiona_connector-7.0.0 lib/rails_connector/cms_env.rb
infopark_fiona_connector-6.10.0.beta1 lib/rails_connector/cms_env.rb
infopark_cloud_connector-6.9.5 lib/rails_connector/cms_env.rb
infopark_rails_connector-6.9.4 lib/rails_connector/cms_env.rb
infopark_rails_connector-6.9.3.1.36404185 lib/rails_connector/cms_env.rb
infopark_rails_connector-6.9.2.1.125136549 lib/rails_connector/cms_env.rb
infopark_rails_connector-6.9.1.3.22208381 lib/rails_connector/cms_env.rb
infopark_rails_connector-6.9.0.3.197272233 lib/rails_connector/cms_env.rb
infopark_rails_connector-6.8.3.174.51542603 lib/rails_connector/cms_env.rb
infopark_rails_connector-6.8.3.115.227021242 lib/rails_connector/cms_env.rb