Module: Elastic::Railties::ARHelpers

Extended by:
ARHelpers
Included in:
ARHelpers
Defined in:
lib/elastic/railties/ar_helpers.rb

Instance Method Summary collapse

Instance Method Details

#find_each_with_options(_collection, includes: nil, scope: nil, &_block) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/elastic/railties/ar_helpers.rb', line 5

def find_each_with_options(_collection, includes: nil, scope: nil, &_block)
  if _collection.respond_to? :find_each
    _collection = _collection.includes(*includes) if includes
    _collection = _collection.send(scope) if scope
    _collection.find_each(&_block)
  elsif _collection.respond_to? :each
    ActiveRecord::Associations::Preloader.new.preload(_collection, includes) if includes
    _collection.each(&_block)
  else
    raise 'Elastic ActiveRecord importing is only supported for collection types'
  end
end

#infer_ar4_field_options(_klass, _field) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/elastic/railties/ar_helpers.rb', line 18

def infer_ar4_field_options(_klass, _field)
  # TODO: consider methods occluded by an override:
  # AR defines methods, this wont work: return nil if _klass.method_defined? _field

  return nil unless _klass.serialized_attributes[_field].nil? # occluded by serializer
  return nil if _klass.columns_hash[_field].nil?
  ar_type_to_options _klass.columns_hash[_field].type
end

#infer_ar5_field_options(_klass, _field) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/elastic/railties/ar_helpers.rb', line 27

def infer_ar5_field_options(_klass, _field)
  # TODO: consider methods occluded by an override:
  # AR defines methods, this wont work: return nil if _klass.method_defined? _field

  meta = _klass.type_for_attribute _field
  return nil if meta.to_s == 'ActiveRecord::Type::Serialized' # occluded by serializer
  ar_type_to_options meta.type
end