Class: ElasticRecord::SearchesMany::Association
- Inherits:
-
Object
- Object
- ElasticRecord::SearchesMany::Association
- Defined in:
- lib/elastic_record/searches_many/association.rb
Instance Attribute Summary collapse
-
#collection ⇒ Object
readonly
Returns the value of attribute collection.
-
#owner ⇒ Object
readonly
Returns the value of attribute owner.
-
#reflection ⇒ Object
readonly
Returns the value of attribute reflection.
Instance Method Summary collapse
- #concat(*records) ⇒ Object
- #delete(records) ⇒ Object
- #eager_loaded_collection(records) ⇒ Object
-
#initialize(owner, reflection) ⇒ Association
constructor
A new instance of Association.
- #load_collection ⇒ Object
- #loaded? ⇒ Boolean
- #reader ⇒ Object
- #scope ⇒ Object
- #writer(other_records) ⇒ Object
Constructor Details
#initialize(owner, reflection) ⇒ Association
Returns a new instance of Association.
10 11 12 13 14 15 |
# File 'lib/elastic_record/searches_many/association.rb', line 10 def initialize(owner, reflection) @owner = owner @reflection = reflection @collection = [] @loaded = false end |
Instance Attribute Details
#collection ⇒ Object (readonly)
Returns the value of attribute collection.
6 7 8 |
# File 'lib/elastic_record/searches_many/association.rb', line 6 def collection @collection end |
#owner ⇒ Object (readonly)
Returns the value of attribute owner.
6 7 8 |
# File 'lib/elastic_record/searches_many/association.rb', line 6 def owner @owner end |
#reflection ⇒ Object (readonly)
Returns the value of attribute reflection.
6 7 8 |
# File 'lib/elastic_record/searches_many/association.rb', line 6 def reflection @reflection end |
Instance Method Details
#concat(*records) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/elastic_record/searches_many/association.rb', line 43 def concat(*records) load_collection if owner.new_record? result = true records.flatten.each do |record| add_to_collection(record) do |r| result &&= record.save unless owner.new_record? end end result && records end |
#delete(records) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/elastic_record/searches_many/association.rb', line 57 def delete(records) records.each do |record| callback(:before_remove, record) if [:autosave] || owner.new_record? record.mark_for_destruction else record.destroy end callback(:after_remove, record) end end |
#eager_loaded_collection(records) ⇒ Object
88 89 90 91 92 93 94 95 |
# File 'lib/elastic_record/searches_many/association.rb', line 88 def eager_loaded_collection(records) unless @loaded @collection = records @loaded = true end collection end |
#load_collection ⇒ Object
79 80 81 82 83 84 85 86 |
# File 'lib/elastic_record/searches_many/association.rb', line 79 def load_collection unless @loaded @collection = merge_collections(persisted_collection, collection) @loaded = true end collection end |
#loaded? ⇒ Boolean
39 40 41 |
# File 'lib/elastic_record/searches_many/association.rb', line 39 def loaded? @loaded end |
#reader ⇒ Object
35 36 37 |
# File 'lib/elastic_record/searches_many/association.rb', line 35 def reader CollectionProxy.new(self) end |
#scope ⇒ Object
71 72 73 74 75 76 77 |
# File 'lib/elastic_record/searches_many/association.rb', line 71 def scope search = klass.elastic_search.filter("#{reflection.belongs_to}_id" => owner.id).limit(1000000) if [:as] search.filter! "#{reflection.belongs_to}_type" => owner.class.name end search end |
#writer(other_records) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/elastic_record/searches_many/association.rb', line 17 def writer(other_records) other_records = other_records.map do |other_record| other_record.is_a?(Hash) ? klass.new(other_record) : other_record end delete(load_collection - other_records) merge_collections(load_collection, other_records) concat(other_records - load_collection) if reflection.counter_cache_column owner.send("#{reflection.counter_cache_column}=", other_records.size) end if reflection.touch_column owner.send("#{reflection.touch_column}=", Time.current) end end |