Class: Chewy::Index::Adapter::Orm
- Defined in:
- lib/chewy/index/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
-
#initialize(target, **options) ⇒ Orm
constructor
A new instance of Orm.
- #load(ids, **options) ⇒ Object
- #name ⇒ Object
Methods inherited from Base
accepts?, #import, #import_fields, #type_name
Constructor Details
#initialize(target, **options) ⇒ Orm
Returns a new instance of Orm.
9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/chewy/index/adapter/orm.rb', line 9 def initialize(target, **) if target.is_a?(relation_class) @target = model_of_relation(target) @default_scope = target else @target = target @default_scope = all_scope end @options = cleanup_default_scope! end |
Instance Attribute Details
#default_scope ⇒ Object (readonly)
Returns the value of attribute default_scope.
7 8 9 |
# File 'lib/chewy/index/adapter/orm.rb', line 7 def default_scope @default_scope end |
Instance Method Details
#identify(collection) ⇒ Object
25 26 27 28 29 30 31 32 33 |
# File 'lib/chewy/index/adapter/orm.rb', line 25 def identify(collection) if collection.is_a?(relation_class) pluck(collection) else Array.wrap(collection).map do |entity| entity.respond_to?(primary_key) ? entity.public_send(primary_key) : entity end end end |
#load(ids, **options) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/chewy/index/adapter/orm.rb', line 99 def load(ids, **) scope = all_scope_where_ids_in(ids) additional_scope = [[:_index].to_sym].try(:[], :scope) || [:scope] loaded_objects = load_scope_objects(scope, additional_scope) loaded_objects = raw(loaded_objects, [:raw_import]) if [:raw_import] indexed_objects = loaded_objects.index_by do |object| object.public_send(primary_key).to_s end ids.map { |id| indexed_objects[id.to_s] } end |
#name ⇒ Object
21 22 23 |
# File 'lib/chewy/index/adapter/orm.rb', line 21 def name @name ||= ([:name].presence || target.name).to_s.camelize.demodulize end |