Sha256: a87e935327dd896878f04868522b0e630434b611b5a3fcd64a500767d846e9f7

Contents?: true

Size: 1004 Bytes

Versions: 4

Compression:

Stored size: 1004 Bytes

Contents

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

4 entries across 4 versions & 1 rubygems

Version Path
infopark_cloud_connector-6.8.0.356.19698103 lib/rails_connector/workspace_selection_middleware.rb
infopark_cloud_connector-6.8.0.348.160665197 lib/rails_connector/workspace_selection_middleware.rb
infopark_cloud_connector-6.8.0.328.4a753fc lib/rails_connector/workspace_selection_middleware.rb
infopark_cloud_connector-6.8.0.322.c003f11 lib/rails_connector/workspace_selection_middleware.rb