Sha256: 075ef497d901b4b274e4d7698ed074194a126c3f222131046fa4038877f91ff8

Contents?: true

Size: 1.33 KB

Versions: 20

Compression:

Stored size: 1.33 KB

Contents

#:enddoc:
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

20 entries across 20 versions & 1 rubygems

Version Path
infopark_rails_connector-6.8.0.210.ed204b0 lib/rails_connector/cms_env.rb
infopark_rails_connector-6.8.0.110.6570b45 lib/rails_connector/cms_env.rb
infopark_rails_connector-6.8.0.72.d18d096 lib/rails_connector/cms_env.rb
infopark_rails_connector-6.8.0.23.da7f96b lib/rails_connector/cms_env.rb
infopark_rails_connector-6.8.0.16.def5e85 lib/rails_connector/cms_env.rb
infopark_rails_connector-6.8.0.15.a24f5ff lib/rails_connector/cms_env.rb
infopark_rails_connector-6.8.0.beta.200.891.647580e lib/rails_connector/cms_env.rb
infopark_rails_connector-6.8.0.beta.200.889.d503e42 lib/rails_connector/cms_env.rb
infopark_rails_connector-6.8.0.beta.200.883.f5f063b lib/rails_connector/cms_env.rb
infopark_rails_connector-6.8.0.beta.200.884.4bd86e6 lib/rails_connector/cms_env.rb
infopark_rails_connector-6.8.0.beta.200.869.9609b39 lib/rails_connector/cms_env.rb
infopark_rails_connector-6.8.0.beta.200.856.8c0fec5 lib/rails_connector/cms_env.rb
infopark_rails_connector-6.8.0.beta.200.809.bdfa8c3 lib/rails_connector/cms_env.rb
infopark_rails_connector-6.8.0.beta.200.785.05d4af9 lib/rails_connector/cms_env.rb
infopark_rails_connector-6.8.0.beta.200.744.99f67fc lib/rails_connector/cms_env.rb
infopark_rails_connector-6.8.0.beta.200.720.44fbabd lib/rails_connector/cms_env.rb
infopark_rails_connector-6.8.0.beta.200.713.e5c3150 lib/rails_connector/cms_env.rb
infopark_rails_connector-6.8.0.beta.200.681.7c84f4a lib/rails_connector/cms_env.rb
infopark_rails_connector-6.8.0.beta.200.663.ceecdee lib/rails_connector/cms_env.rb
infopark_rails_connector-6.8.0.beta.200.650.7c93155 lib/rails_connector/cms_env.rb