# File lib/chewy/index/adapter/active_record.rb, line 36 def primary_key @primary_key ||= target.primary_key.to_sym end
class Chewy::Index::Adapter::ActiveRecord
Public Class Methods
accepts?(target)
click to toggle source
# File lib/chewy/index/adapter/active_record.rb, line 7 def self.accepts?(target) defined?(::ActiveRecord::Base) && ( target.is_a?(Class) && target < ::ActiveRecord::Base || target.is_a?(::ActiveRecord::Relation)) end
Private Instance Methods
cleanup_default_scope!()
click to toggle source
# File lib/chewy/index/adapter/active_record.rb, line 15 def cleanup_default_scope! if Chewy.logger && (@default_scope.arel.orders.present? || @default_scope.arel.limit.present? || @default_scope.arel.offset.present?) Chewy.logger.warn('Default type scope order, limit and offset are ignored and will be nullified') end @default_scope = @default_scope.reorder(nil).limit(nil).offset(nil) end
import_scope(scope, options) { |grouped_objects(objects)| ... }
click to toggle source
# File lib/chewy/index/adapter/active_record.rb, line 24 def import_scope(scope, options) pluck_in_batches(scope, **options.slice(:batch_size)).inject(true) do |result, ids| objects = if options[:raw_import] raw_default_scope_where_ids_in(ids, options[:raw_import]) else default_scope_where_ids_in(ids) end result & yield(grouped_objects(objects)) end end
object_class()
click to toggle source
# File lib/chewy/index/adapter/active_record.rb, line 101 def object_class ::ActiveRecord::Base end
pluck(scope, fields: [], typecast: true)
click to toggle source
# File lib/chewy/index/adapter/active_record.rb, line 44 def pluck(scope, fields: [], typecast: true) if typecast scope.except(:includes).distinct.pluck(primary_key, *fields) else scope = scope.except(:includes).distinct scope.select_values = [primary_key, *fields].map do |column| target.columns_hash.key?(column) ? target.arel_table[column] : column end sql = scope.to_sql if fields.present? target.connection.select_rows(sql) else target.connection.select_values(sql) end end end
pluck_in_batches(scope, fields: [], batch_size: nil, typecast: true) { |ids| ... }
click to toggle source
# File lib/chewy/index/adapter/active_record.rb, line 62 def pluck_in_batches(scope, fields: [], batch_size: nil, typecast: true) unless block_given? return enum_for( :pluck_in_batches, scope, fields: fields, batch_size: batch_size, typecast: typecast ) end scope = scope.reorder(target_id.asc).limit(batch_size) ids = pluck(scope, fields: fields, typecast: typecast) count = 0 while ids.present? yield ids break if ids.size < batch_size last_id = ids.last.is_a?(Array) ? ids.last.first : ids.last ids = pluck(scope.where(target_id.gt(last_id)), fields: fields, typecast: typecast) end count end
primary_key()
click to toggle source
raw_default_scope_where_ids_in(ids, converter)
click to toggle source
# File lib/chewy/index/adapter/active_record.rb, line 92 def raw_default_scope_where_ids_in(ids, converter) sql = default_scope_where_ids_in(ids).to_sql object_class.connection.execute(sql).map(&converter) end
relation_class()
click to toggle source
# File lib/chewy/index/adapter/active_record.rb, line 97 def relation_class ::ActiveRecord::Relation end
scope_where_ids_in(scope, ids)
click to toggle source
# File lib/chewy/index/adapter/active_record.rb, line 88 def scope_where_ids_in(scope, ids) scope.where(target_id.in(Array.wrap(ids))) end
target_id()
click to toggle source
# File lib/chewy/index/adapter/active_record.rb, line 40 def target_id target.arel_table[primary_key.to_s] end