Class: Chewy::Type::Adapter::Orm
- Defined in:
- lib/chewy/type/adapter/orm.rb
Direct Known Subclasses
Constant Summary
Constants inherited from Base
Instance Attribute Summary collapse
-
#default_scope ⇒ Object
readonly
Returns the value of attribute default_scope.
Attributes inherited from Base
Instance Method Summary collapse
- #identify(collection) ⇒ Object
-
#import(*args, &block) ⇒ Object
Import method for ORM takes import data and import options.
-
#initialize(*args) ⇒ Orm
constructor
A new instance of Orm.
- #load(*args) ⇒ Object
- #name ⇒ Object
Methods inherited from Base
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. class_or_relation = args.first if class_or_relation.is_a?(relation_class) @target = class_or_relation.klass @default_scope = class_or_relation else @target = class_or_relation @default_scope = all_scope end cleanup_default_scope! end |
Instance Attribute Details
#default_scope ⇒ Object (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.id : 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.incative? }
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 |
# File 'lib/chewy/type/adapter/orm.rb', line 76 def import *args, &block = args. batch_size = [:batch_size] || BATCH_SIZE collection = args.empty? ? default_scope : (args.one? && args.first.is_a?(relation_class) ? args.first : args.flatten.compact) if collection.is_a?(relation_class) import_scope(collection, batch_size, &block) else import_objects(collection, batch_size, &block) end end |
#load(*args) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/chewy/type/adapter/orm.rb', line 90 def load *args = args. objects = args.flatten additional_scope = [[:_type].type_name.to_sym].try(:[], :scope) || [:scope] scope = all_scope_where_ids_in(objects.map(&:id)) loaded_objects = if additional_scope.is_a?(Proc) scope.instance_exec(&additional_scope) elsif additional_scope.is_a?(relation_class) scope.merge(additional_scope) else scope end.index_by { |object| object.id.to_s } objects.map { |object| loaded_objects[object.id.to_s] } end |
#name ⇒ Object
22 23 24 |
# File 'lib/chewy/type/adapter/orm.rb', line 22 def name @name ||= ([:name].present? ? [:name].to_s.camelize : target.model_name.to_s).demodulize end |