Module: Octopus::Association

Defined in:
lib/octopus/association.rb

Instance Method Summary collapse

Instance Method Details

#association_accessor_methods(reflection, association_proxy_class) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/octopus/association.rb', line 85

def association_accessor_methods(reflection, association_proxy_class)
  define_method(reflection.name) do |*params|
    reload_connection()
    force_reload = params.first unless params.empty?
    association = association_instance_get(reflection.name)

    if association.nil? || force_reload
      association = association_proxy_class.new(self, reflection)
      retval = force_reload ? reflection.klass.uncached { association.reload } : association.reload
      if retval.nil? and association_proxy_class == ActiveRecord::Associations::BelongsToAssociation
        association_instance_set(reflection.name, nil)
        return nil
      end
      association_instance_set(reflection.name, association)
    end

    association.target.nil? ? nil : association
  end

  define_method("loaded_#{reflection.name}?") do
    reload_connection() 
    association = association_instance_get(reflection.name)
    association && association.loaded?
  end

  define_method("#{reflection.name}=") do |new_value|
    reload_connection() 
    association = association_instance_get(reflection.name)

    if association.nil? || association.target != new_value
      association = association_proxy_class.new(self, reflection)
    end

    association.replace(new_value)
    association_instance_set(reflection.name, new_value.nil? ? nil : association)
  end

  define_method("set_#{reflection.name}_target") do |target|
    return if target.nil? and association_proxy_class == ActiveRecord::Associations::BelongsToAssociation
    reload_connection() 
    association = association_proxy_class.new(self, reflection)
    association.target = target
    association_instance_set(reflection.name, association)
  end
end

#association_constructor_method(constructor, reflection, association_proxy_class) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/octopus/association.rb', line 59

def association_constructor_method(constructor, reflection, association_proxy_class)
  define_method("#{constructor}_#{reflection.name}") do |*params|
    reload_connection() 
    attributees      = params.first unless params.empty?
    replace_existing = params[1].nil? ? true : params[1]
    association      = association_instance_get(reflection.name)

    unless association
      association = association_proxy_class.new(self, reflection)
      association_instance_set(reflection.name, association)
    end

    if association_proxy_class == ActiveRecord::Associations::HasOneAssociation
      return_val = association.send(constructor, attributees, replace_existing)
    else
      return_val = association.send(constructor, attributees)
    end

    if should_set_current_shard?
      return_val.current_shard = self.current_shard
    end

    return_val
  end
end

#collection_reader_method(reflection, association_proxy_class) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/octopus/association.rb', line 26

def collection_reader_method(reflection, association_proxy_class)
  define_method(reflection.name) do |*params|
    force_reload = params.first unless params.empty?
    reload_connection() 

    association = association_instance_get(reflection.name)

    unless association
      association  = association_proxy_class.new(self, reflection)
      association_instance_set(reflection.name, association)
    end

    reflection.klass.uncached { association.reload } if force_reload

    association
  end

  define_method("#{reflection.name.to_s.singularize}_ids") do
    reload_connection()        
    if send(reflection.name).loaded? || reflection.options[:finder_sql]
      send(reflection.name).map(&:id)
    else
      if reflection.through_reflection && reflection.source_reflection.belongs_to?
        through = reflection.through_reflection
        primary_key = reflection.source_reflection.primary_key_name
        send(through.name).select("DISTINCT #{through.quoted_table_name}.#{primary_key}").map!(&:"#{primary_key}")
      else
        send(reflection.name).select("#{reflection.quoted_table_name}.#{reflection.klass.primary_key}").except(:includes).map!(&:id)
      end
    end
  end
end

#default_octopus_opts(options) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/octopus/association.rb', line 12

def default_octopus_opts(options)
  if options[:before_add].is_a?(Array)
    options[:before_add] << :set_connection
  else
    options[:before_add] = :set_connection
  end

  if options[:before_remove].is_a?(Array)
    options[:before_remove] << :set_connection
  else
    options[:before_remove] = :set_connection
  end
end

#has_and_belongs_to_many(association_id, options = {}, &extension) ⇒ Object



7
8
9
10
# File 'lib/octopus/association.rb', line 7

def has_and_belongs_to_many(association_id, options = {}, &extension)
  default_octopus_opts(options)
  super(association_id, options, &extension)    
end

#has_many(association_id, options = {}, &extension) ⇒ Object



2
3
4
5
# File 'lib/octopus/association.rb', line 2

def has_many(association_id, options = {}, &extension)
  default_octopus_opts(options)
  super(association_id, options, &extension)
end