aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/attribute_methods/read_test.rb
blob: 764305459d4f36e472da1571eb39cd29580815bb (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
require "cases/helper"
require 'active_support/core_ext/object/inclusion'
require 'thread'

module ActiveRecord
  module AttributeMethods
    class ReadTest < ActiveRecord::TestCase
      class FakeColumn < Struct.new(:name)
        def type_cast_code(var)
          var
        end

        def type; :integer; end
      end

      def setup
        @klass = Class.new do
          def self.superclass; Base; end
          def self.active_record_super; Base; end
          def self.base_class; self; end

          include ActiveRecord::AttributeMethods

          def self.define_attribute_methods
            # Created in the inherited/included hook for "proper" ARs
            @attribute_methods_mutex ||= Mutex.new

            super
          end

          def self.column_names
            %w{ one two three }
          end

          def self.primary_key
          end

          def self.columns
            column_names.map { FakeColumn.new(name) }
          end

          def self.columns_hash
            Hash[column_names.map { |name|
              [name, FakeColumn.new(name)]
            }]
          end
        end
      end

      def test_define_attribute_methods
        instance = @klass.new

        @klass.column_names.each do |name|
          assert !name.in?(instance.methods.map(&:to_s))
        end

        @klass.define_attribute_methods

        @klass.column_names.each do |name|
          assert name.in?(instance.methods.map(&:to_s)), "#{name} is not defined"
        end
      end

      def test_attribute_methods_generated?
        assert(!@klass.attribute_methods_generated?, 'attribute_methods_generated?')
        @klass.define_attribute_methods
        assert(@klass.attribute_methods_generated?, 'attribute_methods_generated?')
      end
    end
  end
end