aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/test/current_attributes_test.rb
blob: 67ef6ef61973910fb96ce57c17507499f1947c58 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
require "abstract_unit"

class CurrentAttributesTest < ActiveSupport::TestCase
  Person = Struct.new(:name, :time_zone)

  class Current < ActiveSupport::CurrentAttributes
    attribute :world, :account, :person, :request
    delegate :time_zone, to: :person

    resets { Time.zone = "UTC" }

    def account=(account)
      super
      self.person = "#{account}'s person"
    end

    def person=(person)
      super
      Time.zone = person.try(:time_zone)
    end

    def request
      "#{super} something"
    end

    def intro
      "#{person.name}, in #{time_zone}"
    end
  end

  setup { Current.reset }

  test "read and write attribute" do
    Current.world = "world/1"
    assert_equal "world/1", Current.world
  end

  test "read overwritten attribute method" do
    Current.request = "request/1"
    assert_equal "request/1 something", Current.request
  end

  test "set attribute via overwritten method" do
    Current.account = "account/1"
    assert_equal "account/1", Current.account
    assert_equal "account/1's person", Current.person
  end

  test "set auxiliary class via overwritten method" do
    Current.person = Person.new("David", "Central Time (US & Canada)")
    assert_equal "Central Time (US & Canada)", Time.zone.name
  end

  test "resets auxiliary class via callback" do
    Current.person = Person.new("David", "Central Time (US & Canada)")
    assert_equal "Central Time (US & Canada)", Time.zone.name

    Current.reset
    assert_equal "UTC", Time.zone.name
  end

  test "set attribute only via scope" do
    Current.world = "world/1"

    Current.set(world: "world/2") do
      assert_equal "world/2", Current.world
    end

    assert_equal "world/1", Current.world
  end

  test "set multiple attributes" do
    Current.world = "world/1"
    Current.account = "account/1"

    Current.set(world: "world/2", account: "account/2") do
      assert_equal "world/2", Current.world
      assert_equal "account/2", Current.account
    end

    assert_equal "world/1", Current.world
    assert_equal "account/1", Current.account
  end

  test "delegation" do
    Current.person = Person.new("David", "Central Time (US & Canada)")
    assert_equal "Central Time (US & Canada)", Current.time_zone
    assert_equal "Central Time (US & Canada)", Current.instance.time_zone
  end

  test "all methods forward to the instance" do
    Current.person = Person.new("David", "Central Time (US & Canada)")
    assert_equal "David, in Central Time (US & Canada)", Current.intro
    assert_equal "David, in Central Time (US & Canada)", Current.instance.intro
  end
end