Class: Chewy::Type::Adapter::Orm

Inherits:
Base show all
Defined in:
lib/chewy/type/adapter/orm.rb

Direct Known Subclasses

ActiveRecord, Mongoid, Sequel

Constant Summary

Constants inherited from Base

Base::BATCH_SIZE

Instance Attribute Summary collapse

Attributes inherited from Base

#options, #target

Instance Method Summary collapse

Methods inherited from Base

accepts?, #type_name

Constructor Details

#initialize(*args) ⇒ Orm

Returns a new instance of Orm.



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/chewy/type/adapter/orm.rb', line 9

def initialize(*args)
  @options = args.extract_options!
  class_or_relation = args.first
  if class_or_relation.is_a?(relation_class)
    @target = model_of_relation(class_or_relation)
    @default_scope = class_or_relation
  else
    @target = class_or_relation
    @default_scope = all_scope
  end
  cleanup_default_scope!
end

Instance Attribute Details

#default_scopeObject (readonly)

Returns the value of attribute default_scope.



7
8
9
# File 'lib/chewy/type/adapter/orm.rb', line 7

def default_scope
  @default_scope
end

Instance Method Details

#identify(collection) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/chewy/type/adapter/orm.rb', line 26

def identify(collection)
  if collection.is_a?(relation_class)
    pluck_ids(collection)
  else
    Array.wrap(collection).map do |entity|
      entity.is_a?(object_class) ? entity.public_send(primary_key) : entity
    end
  end
end

#import(*args, &block) ⇒ Object

Import method for ORM takes import data and import options

Import data types:

* Nothing passed - imports all the model data according to type
  default scope
* ORM scope
* Objects collection
* Ids collection

Import options:

<tt>:batch_size</tt> - import batch size, 1000 objects by default

Method handles destroyed objects as well. In case of objects ORM scope or array passed, objects, responding with true to ‘destroyed?` method will be deleted from index. In case of ids array passed - documents with missing records ids will be deleted from index:

users = User.all
users.each { |user| user.destroy if user.inactive? }
UsersIndex::User.import users # inactive users will be deleted from index
# or
UsersIndex::User.import users.map(&:id) # deleted user ids will be deleted from index

Also there is custom type option ‘delete_if`. It it returns `true` object will be deleted from index. Note that if this option is defined and return `false` Chewy will still check `destroyed?` method. This is useful for paranoid objects deleting implementation.

define_type User, delete_if: ->{ deleted_at } do
  ...
end

users = User.all
users.each { |user| user.deleted_at = Time.now }
UsersIndex::User.import users # paranoid deleted users will be deleted from index
# or
UsersIndex::User.import users.map(&:id) # user ids will be deleted from index


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/chewy/type/adapter/orm.rb', line 76

def import(*args, &block)
  options = args.extract_options!
  options[:batch_size] ||= BATCH_SIZE

  collection = if args.empty?
    default_scope
  elsif args.one? && args.first.is_a?(relation_class)
    args.first
  else
    args.flatten.compact
  end

  if collection.is_a?(relation_class)
    import_scope(collection, options, &block)
  else
    import_objects(collection, options, &block)
  end
end

#load(*args) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/chewy/type/adapter/orm.rb', line 95

def load(*args)
  load_options = args.extract_options!
  objects = args.flatten

  additional_scope = load_options[load_options[:_type].type_name.to_sym].try(:[], :scope) || load_options[:scope]

  scope = all_scope_where_ids_in(objects.map(&primary_key))
  loaded_objects = load_scope_objects(scope, additional_scope).index_by { |object| object.public_send(primary_key).to_s }

  objects.map { |object| loaded_objects[object.public_send(primary_key).to_s] }
end

#nameObject



22
23
24
# File 'lib/chewy/type/adapter/orm.rb', line 22

def name
  @name ||= (options[:name].presence || target.name).to_s.camelize.demodulize
end