Module: BigRecord::BrAssociations::ClassMethods

Defined in:
lib/big_record/br_associations.rb

Instance Method Summary

Instance Method Details

- (Object) belongs_to_big_record(association_id, options = {}) Also known as: belongs_to_bigrecord



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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/big_record/br_associations.rb', line 58

def belongs_to_big_record(association_id, options = {})
  if options.include?(:class_name) && !options.include?(:foreign_key)
    ::ActiveSupport::Deprecation.warn(
    "The inferred foreign_key name will change in Rails 2.0 to use the association name instead of its class name when they differ.  When using :class_name in belongs_to, use the :foreign_key option to explicitly set the key name to avoid problems in the transition.",
    caller)
  end

  reflection = create_belongs_to_big_record_reflection(association_id, options)

  if reflection.options[:polymorphic]
    association_accessor_methods_big_record(reflection, BelongsToPolymorphicAssociation)

    module_eval do
      before_save "association = instance_variable_get(\"@\#{reflection.name}\")\nif association && association.target\nif association.new_record?\nassociation.save(true)\nend\n\nif association.updated?\nself[\"\#{reflection.primary_key_name}\"] = association.id\nself[\"\#{reflection.options[:foreign_type]}\"] = association.class.base_class.name.to_s\nend\nend\n"
    end
  else
    association_accessor_methods_big_record(reflection, BelongsToAssociation)
    association_constructor_method_big_record(:build,  reflection, BelongsToAssociation)
    association_constructor_method_big_record(:create, reflection, BelongsToAssociation)

    module_eval do
      before_save "association = instance_variable_get(\"@\#{reflection.name}\")\nif !association.nil?\nif association.new_record?\nassociation.save(true)\nend\n\nif association.updated?\nself[\"\#{reflection.primary_key_name}\"] = association.id\nend\nend\n"
    end
  end

  if options[:counter_cache]
    cache_column = options[:counter_cache] == true ?
      "#{self.to_s.underscore.pluralize}_count" :
      options[:counter_cache]

    module_eval(
      "after_create '#{reflection.name}.class.increment_counter(\"#{cache_column}\", #{reflection.primary_key_name})" +
      " unless #{reflection.name}.nil?'"
    )

    module_eval(
      "before_destroy '#{reflection.name}.class.decrement_counter(\"#{cache_column}\", #{reflection.primary_key_name})" +
      " unless #{reflection.name}.nil?'"
    )
  end
end

- (Object) belongs_to_many(association_id, options = {})



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/big_record/br_associations.rb', line 128

def belongs_to_many(association_id, options = {})
  if options.include?(:class_name) && !options.include?(:foreign_key)
    ::ActiveSupport::Deprecation.warn(
    "The inferred foreign_key name will change in Rails 2.0 to use the association name instead of its class name when they differ.  When using :class_name in belongs_to, use the :foreign_key option to explicitly set the key name to avoid problems in the transition.",
    caller)
  end

  reflection = create_belongs_to_many_reflection(association_id, options)

  association_accessor_methods_big_record(reflection, BelongsToManyAssociation)
  association_constructor_method_big_record(:build,  reflection, BelongsToManyAssociation)
  association_constructor_method_big_record(:create, reflection, BelongsToManyAssociation)

  module_eval do
    before_save "association = instance_variable_get(\"@\#{reflection.name}\")\nif !association.nil?\nassociation.each do |r|\nr.save(true) if r.new_record?\nend\n\nif association.updated?\nself[\"\#{reflection.primary_key_name}\"] = association.collect{|r| r.id}\nend\nend\n"
  end

end

- (Object) has_and_belongs_to_many_big_records(association_id, options = {}, &extension) Also known as: has_and_belongs_to_many_bigrecords



160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/big_record/br_associations.rb', line 160

def has_and_belongs_to_many_big_records(association_id, options = {}, &extension)
  reflection = create_has_and_belongs_to_many_big_records_reflection(association_id, options, &extension)

  collection_accessor_methods(reflection, HasAndBelongsToManyAssociation)

  # Don't use a before_destroy callback since users' before_destroy
  # callbacks will be executed after the association is wiped out.
  old_method = "destroy_without_habtm_shim_for_#{reflection.name}"
  class_eval "alias_method :\#{old_method}, :destroy_without_callbacks\ndef destroy_without_callbacks\n\#{reflection.name}.clear\n\#{old_method}\nend\n"

  add_association_callbacks(reflection.name, options)
end

- (Object) has_many_big_records(association_id, options = {}, &extension) Also known as: has_many_bigrecords



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/big_record/br_associations.rb', line 18

def has_many_big_records(association_id, options = {}, &extension)
  reflection = create_has_many_big_records_reflection(association_id, options, &extension)

  configure_dependency_for_has_many(reflection)

  if options[:through]
    collection_reader_method(reflection, HasManyThroughAssociation)
  else
    add_association_callbacks(reflection.name, reflection.options)
    collection_accessor_methods(reflection, HasManyAssociation)
  end
end

- (Object) has_one_big_record(association_id, options = {}) Also known as: has_one_bigrecord



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

def has_one_big_record(association_id, options = {})
  reflection = create_has_one_big_record_reflection(association_id, options)

  module_eval do
    after_save "association = instance_variable_get(\"@\#{reflection.name}\")\nif !association.nil? && (new_record? || association.new_record? || association[\"\#{reflection.primary_key_name}\"] != id)\nassociation[\"\#{reflection.primary_key_name}\"] = id\nassociation.save(true)\nend\n"
  end

  association_accessor_methods_big_record(reflection, HasOneAssociation)
  association_constructor_method_big_record(:build,  reflection, HasOneAssociation)
  association_constructor_method_big_record(:create, reflection, HasOneAssociation)

  configure_dependency_for_has_one(reflection)
end