Skip to content

Commit c4928b1

Browse files
committed
Restore location of Utopia::Controller[].
1 parent f2ff6e2 commit c4928b1

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

lib/utopia/controller/middleware.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,6 @@ class Middleware
2121
# The controller filename.
2222
CONTROLLER_RB = "controller.rb".freeze
2323

24-
def self.[] request
25-
request.env[VARIABLES_KEY]
26-
end
27-
2824
# @param root [String] The content root where controllers will be loaded from.
2925
# @param base [Class] The base class for controllers.
3026
def initialize(app, root: Utopia::default_root, base: Controller::Base)

lib/utopia/controller/variables.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
# Released under the MIT License.
44
# Copyright, 2014-2025, by Samuel Williams.
55

6+
require_relative "../middleware"
7+
68
module Utopia
79
module Controller
810
# Provides a stack-based instance variable lookup mechanism. It can flatten a stack of controllers into a single hash.
@@ -61,5 +63,9 @@ def [] key
6163
fetch("@#{key}".to_sym, nil)
6264
end
6365
end
66+
67+
def self.[] request
68+
request.env[VARIABLES_KEY]
69+
end
6470
end
6571
end

test/utopia/controller/variables.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# Copyright, 2016-2025, by Samuel Williams.
55

66
require "utopia/controller/variables"
7+
require "rack/request"
78

89
class TestController
910
attr_accessor :x, :y, :z
@@ -40,4 +41,19 @@ def copy_instance_variables(from)
4041

4142
expect(variables.to_hash).to be == {x: 10, y: 20}
4243
end
44+
45+
describe Utopia::Controller do
46+
it "returns variables from request env" do
47+
variables = Utopia::Controller::Variables.new
48+
request = Rack::Request.new(Utopia::VARIABLES_KEY => variables)
49+
50+
expect(Utopia::Controller[request]).to be == variables
51+
end
52+
53+
it "returns nil when variables are not set" do
54+
request = Rack::Request.new({})
55+
56+
expect(Utopia::Controller[request]).to be_nil
57+
end
58+
end
4359
end

0 commit comments

Comments
 (0)