Sha256: 02711247c85e11608ef0d9086a194a4f26412a9e89e5c765310567251e0941ac

Contents?: true

Size: 1014 Bytes

Versions: 2

Compression:

Stored size: 1014 Bytes

Contents

#:enddoc:
module RailsConnector

class WorkspaceSelectionMiddleware
  CURRENT_WORKSPACE_SESSION_KEY = '_rc-ws'
  CURRENT_WORKSPACE_PARAMS_KEY = '_rc-ws'

  def initialize(app)
    @app = app
  end

  def call(env)
    handle_workspace_parameter(env)
    workspace_id = session(env)[CURRENT_WORKSPACE_SESSION_KEY]
    Workspace.current = proc do
      if workspace_id
        begin
          Workspace.find(workspace_id)
        rescue Kvom::NotFound
          Workspace.default
        end
      else
        Workspace.default
      end
    end
    @app.call(env)
  end

  private

  def handle_workspace_parameter(env)
    workspace_id = Rack::Request.new(env).params[CURRENT_WORKSPACE_PARAMS_KEY]
    return unless workspace_id
    if workspace_id.present?
      session(env)[CURRENT_WORKSPACE_SESSION_KEY] = workspace_id
    else
      session(env).delete(CURRENT_WORKSPACE_SESSION_KEY)
    end
  end

  def session(env)
    env[Rack::Session::Abstract::ENV_SESSION_KEY]
  end
end

end # module RailsConnector

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
infopark_cloud_connector-6.8.0.210.ed204b0 lib/rails_connector/workspace_selection_middleware.rb
infopark_cloud_connector-6.8.0.110.6570b45 lib/rails_connector/workspace_selection_middleware.rb