Class: ArrayToAssociationWrapper

Inherits:
Array
  • Object
show all
Defined in:
lib/array_to_association_wrapper.rb

Instance Method Summary collapse

Instance Method Details

#collection_without_deferred_saveObject



68
69
70
# File 'lib/array_to_association_wrapper.rb', line 68

def collection_without_deferred_save
  @association_owner.send("#{@association_name}_without_deferred_save")
end

#defer_association_methods_to(owner, association_name) ⇒ Object



3
4
5
6
# File 'lib/array_to_association_wrapper.rb', line 3

def defer_association_methods_to(owner, association_name)
  @association_owner = owner
  @association_name = association_name
end

#find_with_deferred_save(*args) ⇒ Object Also known as: find



26
27
28
29
30
31
32
# File 'lib/array_to_association_wrapper.rb', line 26

def find_with_deferred_save(*args)
  if @association_owner.present?
    collection_without_deferred_save.send(:find, *args)
  else
    find_without_deferred_save
  end
end

#first_with_deferred_save(*args) ⇒ Object Also known as: first



37
38
39
40
41
42
43
# File 'lib/array_to_association_wrapper.rb', line 37

def first_with_deferred_save(*args)
  if @association_owner.present?
    collection_without_deferred_save.send(:first, *args)
  else
    first_without_deferred_save
  end
end

#include_with_deferred_save?(obj) ⇒ Boolean Also known as: include?

trick collection_name.include?(obj) If you use a collection of SingleTableInheritance and didn’t :select ‘type’ the include? method will not find any subclassed object.

Returns:

  • (Boolean)


11
12
13
14
15
16
17
18
19
20
21
# File 'lib/array_to_association_wrapper.rb', line 11

def include_with_deferred_save?(obj)
  if @association_owner.present?
    if detect { |itm| itm == obj || (itm[:id] == obj[:id] && obj.is_a?(itm.class)) }
      return true
    else
      return false
    end
  else
    include_without_deferred_save?(obj)
  end
end

#last_with_deferred_save(*args) ⇒ Object Also known as: last



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

def last_with_deferred_save(*args)
  if @association_owner.present?
    collection_without_deferred_save.send(:last, *args)
  else
    last_without_deferred_save
  end
end