Files
ubicloud/routes/common/base.rb
Jeremy Evans 75a4cb85bf Merge CloverWeb, CloverApi, and CloverRuntime into Clover
This is a prerequisite for merging the api and web routing trees.

Much of this is just moving the bodies of clover_web.rb,
clover_api.rb, and clover_runtime.rb into clover.rb.  However,
to preserve differences in behavior, such as always using
json responses for api and runtime, and only using sessions,
content-security-policy, and HSTS for web, this adds the api?,
runtime?, and web? methods, and overrides methods that check
those methods to allow for conditional behavior. It also adds
a response.json= method, which the api and runtime routes use
to signal that responses should be json.

For api and runtime routes, this updates the hash_branch calls
to use api and runtime specific namespaces.  The namespaces for
the web routes are not modified.

This removes the freezing of Clover from spec/routes/spec_helper.rb.
Now that Clover is the main app and autoloads hash branches, you
can only freeze if after all hash branches has been loaded. So
running an individual spec no longer freezes Clover.  However,
the frozen specs will still test that everything works when
Clover is frozen (those load all hash branches before freezing
Clover).

This changes autoload_routes to load all routes up front when
FORCE_AUTOLOAD=1.  This makes the test environment more like
the production environment.  Running a single spec will still
autoload only the branches necessary, to keep single spec runs
fast.
2024-11-05 10:47:00 -08:00

34 lines
609 B
Ruby

# frozen_string_literal: true
class Routes::Common::Base
module AppMode
API = :api
WEB = :web
end
def initialize(app:, request:, user:, location:, resource:)
@app = app
@request = request
@user = user
@resource = resource
@location = location
@mode = app.api? ? AppMode::API : AppMode::WEB
end
def project
@app.instance_variable_get(:@project)
end
def response
@app.response
end
def flash
@app.flash
end
def params
@params ||= (@mode == AppMode::API) ? @request.body.read : @request.params.reject { _1 == "_csrf" }.to_json
end
end