aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/test/core_ext/module/attribute_aliasing_test.rb
blob: 9e5edda77a001eaa04c47028e4cf929f2b56cfad (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
# frozen_string_literal: true
require "abstract_unit"
require "active_support/core_ext/module/aliasing"

module AttributeAliasing
  class Content
    attr_accessor :title, :Data

    def initialize
      @title, @Data = nil, nil
    end

    def title?
      !title.nil?
    end

    def Data?
      !self.Data.nil?
    end
  end

  class Email < Content
    alias_attribute :subject, :title
    alias_attribute :body, :Data
  end
end

class AttributeAliasingTest < ActiveSupport::TestCase
  def test_attribute_alias
    e = AttributeAliasing::Email.new

    assert !e.subject?

    e.title = "Upgrade computer"
    assert_equal "Upgrade computer", e.subject
    assert e.subject?

    e.subject = "We got a long way to go"
    assert_equal "We got a long way to go", e.title
    assert e.title?
  end

  def test_aliasing_to_uppercase_attributes
    # Although it's very un-Ruby, some people's AR-mapped tables have
    # upper-case attributes, and when people want to alias those names
    # to more sensible ones, everything goes *foof*.
    e = AttributeAliasing::Email.new

    assert !e.body?
    assert !e.Data?

    e.body = "No, really, this is not a joke."
    assert_equal "No, really, this is not a joke.", e.Data
    assert e.Data?

    e.Data = "Uppercased methods are the suck"
    assert_equal "Uppercased methods are the suck", e.body
    assert e.body?
  end
end