Module: ElasticRecord::SearchesMany

Defined in:
lib/elastic_record/searches_many.rb,
lib/elastic_record/searches_many/builder.rb,
lib/elastic_record/searches_many/autosave.rb,
lib/elastic_record/searches_many/reflection.rb,
lib/elastic_record/searches_many/association.rb,
lib/elastic_record/searches_many/collection_proxy.rb

Defined Under Namespace

Modules: Autosave, ClassMethods Classes: Association, Builder, CollectionProxy, Reflection

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'lib/elastic_record/searches_many.rb', line 9

def self.included(base)
  base.class_eval do
    extend ClassMethods

    class_attribute :searches_many_reflections
    self.searches_many_reflections = {}

    include ElasticRecord::SearchesMany::Autosave
  end
end

Instance Method Details

#associated_records_to_autosave(association) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/elastic_record/searches_many/autosave.rb', line 48

def associated_records_to_autosave(association)
  if association.loaded?
    association.load_collection
  else
    []
  end
end

#mark_for_destructionObject

Marks this record to be destroyed as part of the parents save transaction. This does not actually destroy the record instantly, rather child record will be destroyed when parent.save is called.

Only useful if the :autosave option on the parent is enabled for this associated model.



61
62
63
# File 'lib/elastic_record/searches_many/autosave.rb', line 61

def mark_for_destruction
  @marked_for_destruction = true
end

#marked_for_destruction?Boolean

Returns whether or not this record will be destroyed as part of the parents save transaction.

Only useful if the :autosave option on the parent is enabled for this associated model.

Returns:

  • (Boolean)


68
69
70
# File 'lib/elastic_record/searches_many/autosave.rb', line 68

def marked_for_destruction?
  @marked_for_destruction
end

#reloadObject



82
83
84
85
# File 'lib/elastic_record/searches_many.rb', line 82

def reload
  super
  searches_many_cache.clear
end

#save_autosave_records(reflection) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/elastic_record/searches_many/autosave.rb', line 22

def save_autosave_records(reflection)
  if association = searches_many_instance_get(reflection.name)
    associated_records_to_autosave(association).each do |record|
      if record.marked_for_destruction?
        record.destroy
      elsif record.changed?
        record.save
      end
    end
  end      
end

#searches_many_association(name) ⇒ Object

Returns the searches_many instance for the given name, instantiating it if it doesn’t already exist



71
72
73
74
75
76
77
78
79
80
# File 'lib/elastic_record/searches_many.rb', line 71

def searches_many_association(name)
  association = searches_many_instance_get(name)

  if association.nil?
    association = ElasticRecord::SearchesMany::Association.new(self, searches_many_reflections[name])
    searches_many_instance_set(name, association)
  end

  association
end

#validate_autosave_records(reflection) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/elastic_record/searches_many/autosave.rb', line 34

def validate_autosave_records(reflection)
  if association = searches_many_instance_get(reflection.name)
    associated_records_to_autosave(association).each do |record|
      unless record.valid?
        record.errors.each do |attribute, message|
          attribute = "#{reflection.name}.#{attribute}"
          errors[attribute] << message
          errors[attribute].uniq!
        end
      end
    end
  end
end