Class: ActiveRecord::Relation

Inherits:
Object
  • Object
show all
Defined in:
lib/citier/relation_methods.rb

Instance Method Summary collapse

Instance Method Details

#apply_finder_options(options) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/citier/relation_methods.rb', line 76

def apply_finder_options(options)
  return relation_apply_finder_options(options) if !@klass.acts_as_citier?
  
  relation = self
  
  # With option :no_children set to true, only records of type self will be returned. 
  # So Root.all(:no_children => true) won't return Child records.
  no_children = options.delete(:no_children)
  if no_children
    relation = clone

    c = @klass
    
    self_type = c.superclass == ActiveRecord::Base ? nil : c.name
    relation = relation.where(:type => self_type)
  end
  
  relation.relation_apply_finder_options(options)
end

#delete_all(conditions = nil) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/citier/relation_methods.rb', line 5

def delete_all(conditions = nil)
  return relation_delete_all(conditions) if !@klass.acts_as_citier?
  
  return relation_delete_all(conditions) if conditions

  deleted = true
  ids = nil
  c = @klass
  
  bind_values.each do |bind_value|
    if bind_value[0].name == "id"
      ids = bind_value[1]
      break
    end
  end
  ids ||= where_values_hash["id"] || where_values_hash[:id]
  where_hash = ids ? { :id => ids } : nil
  
  deleted &= c.base_class.where(where_hash).relation_delete_all
  while c.superclass != ActiveRecord::Base
    if c.const_defined?(:Writable)
      citier_debug("Deleting back up hierarchy #{c}")
      deleted &= c::Writable.where(where_hash).delete_all
    end
    c = c.superclass
  end
  
  deleted
end

#relation_apply_finder_optionsObject



75
# File 'lib/citier/relation_methods.rb', line 75

alias_method :relation_apply_finder_options, :apply_finder_options

#relation_delete_allObject



4
# File 'lib/citier/relation_methods.rb', line 4

alias_method :relation_delete_all, :delete_all

#relation_to_aObject



35
# File 'lib/citier/relation_methods.rb', line 35

alias_method :relation_to_a, :to_a

#to_aObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/citier/relation_methods.rb', line 36

def to_a
  return relation_to_a if !@klass.acts_as_citier?
  
  records = relation_to_a
  
  c = @klass
  
  if records.all? { |record| record.class == c } 
    return records 
  end
  
  full_records = []
  ids_wanted = {}
  
  # Map all the ids wanted per type
  records.each do |record|
    if record.class == c # We don't need to find the record again if this is already the correct one
      full_records << record
      next
    end
    
    ids_wanted[record.class] ||= []
    ids_wanted[record.class] << record.id
  end
  
  # Find all wanted records
  ids_wanted.each do |type_class, ids|
    full_records.push(*type_class.find(ids))
  end
  
  # Make a new array with the found records at the right places
  records.each do |record|              
    full_record = full_records.find { |full_record| full_record.id == record.id }
    record.force_attributes(full_record.instance_variable_get(:@attributes), :merge => true, :clear_caches => false)
  end
  
  return records
end