module ActiveRecordUuid::AssociationMethods

Public Instance Methods

belongs_to(name, options = {}) click to toggle source
Calls superclass method
# File lib/active_record_uuid/extensions/association_methods.rb, line 13
def belongs_to(name, options = {})
  options = uuid_assoc_options(:belongs_to, name, options)
  super
end
has_and_belongs_to_many(name, options = {}, &extension) click to toggle source
Calls superclass method
# File lib/active_record_uuid/extensions/association_methods.rb, line 18
def has_and_belongs_to_many(name, options = {}, &extension)
  options = uuid_assoc_options(:has_and_belongs_to_many, name, options)
  super
end
has_many(name, options = {}, &extension) click to toggle source
Calls superclass method
# File lib/active_record_uuid/extensions/association_methods.rb, line 3
def has_many(name, options = {}, &extension)
  options = uuid_assoc_options(:has_many, name, options)
  super
end
has_one(name, options = {}) click to toggle source
Calls superclass method
# File lib/active_record_uuid/extensions/association_methods.rb, line 8
def has_one(name, options = {})
  options = uuid_assoc_options(:has_one, name, options)
  super
end

Private Instance Methods

uuid_assoc_options(macro, association_name, options) click to toggle source
# File lib/active_record_uuid/extensions/association_methods.rb, line 24
def uuid_assoc_options(macro, association_name, options)
  opts = {}
  
  # Set class_name only if not a has-through relation or poly relation
  if options[:through].blank? and options[:as].blank? and options[:class_name].blank? and !self.name.match(/::/)
    opts[:class_name] = "::#{association_name.to_s.singularize.camelize}"
  end
  
  # Set foreign_key only if not passed
  if options[:foreign_key].blank?
    case macro
    when :has_many, :has_one
      opts[:foreign_key] = uuid_foreign_key(self.name)
    when :belongs_to
      opts[:foreign_key] = uuid_foreign_key(association_name)
    when :has_and_belongs_to_many
      opts[:foreign_key] = uuid_foreign_key(self.name)
      opts[:association_foreign_key] = uuid_foreign_key(association_name)
    end
  end

  options.merge(opts)
end
uuid_foreign_key(name) click to toggle source
# File lib/active_record_uuid/extensions/association_methods.rb, line 48
def uuid_foreign_key(name)
  name.to_s.singularize.underscore.downcase + "_uuid"
end