Module: LowCardTables::HasLowCardTable::Base::ClassMethods

Defined in:
lib/low_card_tables/has_low_card_table/base.rb

Instance Method Summary collapse

Instance Method Details

#_low_card_associations_managerObject

The LowCardAssociationsManager keeps track of which low-card tables this table refers to; see its documentation for more information.



175
176
177
# File 'lib/low_card_tables/has_low_card_table/base.rb', line 175

def _low_card_associations_manager
  @_low_card_associations_manager ||= LowCardTables::HasLowCardTable::LowCardAssociationsManager.new(self)
end

#_low_card_dynamic_method_managerObject

The LowCardDynamicMethodManager is responsible for maintaining the right delegated method names in the _low_card_dynamic_methods_module; see its documentation for more information.



181
182
183
# File 'lib/low_card_tables/has_low_card_table/base.rb', line 181

def _low_card_dynamic_method_manager
  @_low_card_dynamic_method_manager ||= LowCardTables::HasLowCardTable::LowCardDynamicMethodManager.new(self)
end

#_low_card_dynamic_methods_moduleObject

This maintains a single module that gets included into this class; it is the place where we add all delegated methods. We use a module rather than defining them directly on this class so that users can still override them and use #super to call our implementation, if desired.



188
189
190
191
192
193
194
195
# File 'lib/low_card_tables/has_low_card_table/base.rb', line 188

def _low_card_dynamic_methods_module
  @_low_card_dynamic_methods_module ||= begin
    out = Module.new
    self.const_set(:LowCardDynamicMethods, out)
    include out
    out
  end
end

#_low_card_find_sti_class(type_name) ⇒ Object



131
132
133
# File 'lib/low_card_tables/has_low_card_table/base.rb', line 131

def _low_card_find_sti_class(type)
  find_sti_class(type)
end

#association_for_inheritance_columnObject



85
86
87
# File 'lib/low_card_tables/has_low_card_table/base.rb', line 85

def association_for_inheritance_column
  _low_card_associations_manager.association_containing_method_named(inheritance_column)
end

#descends_from_active_record?Boolean

Returns:

  • (Boolean)


89
90
91
92
93
94
95
96
97
# File 'lib/low_card_tables/has_low_card_table/base.rb', line 89

def descends_from_active_record?
  out = super

  if out && (self != ::ActiveRecord::Base) && (! superclass.abstract_class?) && (superclass != ::ActiveRecord::Base)
    out = false if association_for_inheritance_column
  end

  out
end

#discriminate_class_for_record(record, call_super = true) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/low_card_tables/has_low_card_table/base.rb', line 114

def discriminate_class_for_record(record, call_super = true)
  if (! record[inheritance_column])
    if (association = association_for_inheritance_column)
      foreign_key = record[association.foreign_key_column_name]

      foreign_key = association.low_card_class.low_card_type_cast_id(foreign_key)
      low_card_row = association.low_card_class.low_card_row_for_id(foreign_key)
      type = low_card_row.send(inheritance_column)

      return _low_card_find_sti_class(type) if type
    end
  end

  super(record) if call_super
end

#has_any_low_card_tables?Boolean

This overrides the implementation in LowCardTables::ActiveRecord::Base – the only way we get included in a class is if that class has declared has_low_card_table to at least one table.

Returns:

  • (Boolean)


81
82
83
# File 'lib/low_card_tables/has_low_card_table/base.rb', line 81

def has_any_low_card_tables?
  true
end

#instantiate(record) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/low_card_tables/has_low_card_table/base.rb', line 153

def instantiate(record)
  sti_class = discriminate_class_for_record(record, false)
  if sti_class
    record_id = sti_class.primary_key && record[sti_class.primary_key]

    if defined?(::ActiveRecord::IdentityMap) && ::ActiveRecord::IdentityMap.enabled? && record_id
      instance = use_identity_map(sti_class, record_id, record)
    else
      instance = sti_class.allocate
      instance.init_with('attributes' => record)
      instance
    end

    instance
  else
    super
  end
end

#type_condition(table = arel_table) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/low_card_tables/has_low_card_table/base.rb', line 99

def type_condition(table = arel_table)
  if (association = association_for_inheritance_column)
    sti_names  = ([self] + descendants).map { |model| model.sti_name }

    ids = association.low_card_class.low_card_ids_matching(inheritance_column => sti_names).to_a
    table[association.foreign_key_column_name].in(ids)
  else
    if ::ActiveRecord::Base.method(:type_condition).arity.abs >= 1
      super
    else
      super()
    end
  end
end