Module: XBar::Rails2::Association

Defined in:
lib/xbar/rails2/association.rb

Instance Method Summary collapse

Instance Method Details

#association_accessor_methods(reflection, association_proxy_class) ⇒ Object



4
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/xbar/rails2/association.rb', line 4

def association_accessor_methods(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)

    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|
    association = association_instance_get(reflection.name)
    reload_connection()
    if association.nil? || association.target != new_value
      association = association_proxy_class.new(self, reflection)
    end

    if association_proxy_class == ActiveRecord::Associations::HasOneThroughAssociation
      association.create_through_record(new_value)
      if new_record?
        association_instance_set(reflection.name, new_value.nil? ? nil : association)
      else
        self.send(reflection.name, new_value)
      end
    else
      association.replace(new_value)
      association_instance_set(reflection.name, new_value.nil? ? nil : association)
    end
  end

  define_method("set_#{reflection.name}_target") do |target|
    reload_connection()
    return if target.nil? and association_proxy_class == ActiveRecord::Associations::BelongsToAssociation
    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



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
# File 'lib/xbar/rails2/association.rb', line 104

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
      ret_val = association.send(constructor, attributees, replace_existing)
    else
      ret_val = association.send(constructor, attributees)
    end

    if should_set_current_shard?
      ret_val.current_shard = self.current_shard
    end

    return ret_val
  end
end

#collection_accessor_methods(reflection, association_proxy_class, writer = true) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/xbar/rails2/association.rb', line 84

def collection_accessor_methods(reflection, association_proxy_class, writer = true)
  collection_reader_method(reflection, association_proxy_class)

  if writer
    define_method("#{reflection.name}=") do |new_value|
      reload_connection()
      # Loads proxy class instance (defined in collection_reader_method) if not already loaded
      association = send(reflection.name)
      association.replace(new_value)
      association
    end

    define_method("#{reflection.name.to_s.singularize}_ids=") do |new_value|
      reload_connection()
      ids = (new_value || []).reject { |nid| nid.blank? }.map(&:to_i)
      send("#{reflection.name}=", reflection.klass.find(ids).index_by(&:id).values_at(*ids))
    end
  end
end

#collection_reader_method(reflection, association_proxy_class) ⇒ Object



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

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
      send(reflection.name).all(:select => "#{reflection.quoted_table_name}.#{reflection.klass.primary_key}").map(&:id)
    end
  end
end