Class: ActiveRecordOverrides

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/sorbet-rails/rails_mixins/active_record_overrides.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeActiveRecordOverrides

Returns a new instance of ActiveRecordOverrides.



10
11
12
# File 'lib/sorbet-rails/rails_mixins/active_record_overrides.rb', line 10

def initialize
  @enum_calls = {}
end

Instance Attribute Details

#enum_callsObject (readonly)

Returns the value of attribute enum_calls.



8
9
10
# File 'lib/sorbet-rails/rails_mixins/active_record_overrides.rb', line 8

def enum_calls
  @enum_calls
end

Instance Method Details

#get_enum_call(klass, enum_sym) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/sorbet-rails/rails_mixins/active_record_overrides.rb', line 27

def get_enum_call(klass, enum_sym)
  return nil if klass == Object
  class_name = klass.name
  class_enum_calls = @enum_calls[klass.name]
  return class_enum_calls[enum_sym] if class_enum_calls && class_enum_calls.has_key?(enum_sym)
  return get_enum_call(klass.superclass, enum_sym)
end

#store_enum_call(klass, kwargs) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/sorbet-rails/rails_mixins/active_record_overrides.rb', line 14

def store_enum_call(klass, kwargs)
  class_name = klass.name
  @enum_calls[class_name] ||= {}
  # modeling the logic in
  # https://github.com/rails/rails/blob/master/activerecord/lib/active_record/enum.rb#L152
  kwargs.each do |name, values|
    next if ::ActiveRecord::Enum::SR_ENUM_KEYWORDS.include?(name)

    # calling dup is required, because Rails internally mutates `kwargs` (the args you passed to `enum`)
    @enum_calls[class_name][name] = kwargs.dup
  end
end