module Ardm::Ar::Associations::ClassMethods

Public Instance Methods

belongs_to(name, *args) click to toggle source
Calls superclass method
# File lib/ardm/ar/associations.rb, line 69
def belongs_to(name, *args)
  options = args.shift || {}

  if String === options || Class === options # belongs_to :name, 'Class', options: 'here'
    options = (args.last || {}).merge(:model => options.to_s)
  end

  unless Hash === options
    raise ArgumentError, "bad belongs_to #{name} options format #{options.inspect}"
  end

  if options.has_key?(:key) || options.has_key?(:unique)
    raise Ardm::NotImplemented, "belongs to :key and :unique are not implemented."
  end

  options.delete(:default)
  required = options.delete(:required)
  opts = Ardm::Ar::Associations.convert_options(self, options)
  super name, *opts

  model = self
  Ardm::Ar::Finalize.on_finalize do
    return @child_key if defined?(@child_key)

    properties = model.properties
    assoc = reflect_on_association(name)

    property_name = assoc.foreign_key
    target_property = assoc.klass.properties.key.first

    properties[property_name] || begin
      source_key_options = Ardm::Ext::Hash.only(target_property.options, :length, :precision, :scale, :min, :max).update(
        :index    => name,
        :required => (required == false ? false : true),
        :key      => false,
        :unique   => false
      )
      model.property(property_name, target_property.to_child_key, source_key_options)
    end

  end
  nil
end
dump_associations_hash(options) click to toggle source
# File lib/ardm/ar/associations.rb, line 51
def dump_associations_hash(options)
  options.inject({}) do |new_attrs, (key, value)|
    if reflection = reflect_on_association(key.to_sym)
      if value.is_a?(ActiveRecord::Base)
        new_attrs[reflection.foreign_key] = value.id
        if reflection.respond_to?(:polymorphic?) && reflection.polymorphic?
          new_attrs[reflection.foreign_type] = value.class.base_class
        end
      else
        new_attrs[reflection.foreign_key] = value
      end
    else
      new_attrs[key] = value
    end
    new_attrs
  end
end
has(count, name, *args) click to toggle source
# File lib/ardm/ar/associations.rb, line 117
def has(count, name, *args)
  options = args.shift || {}

  if String === options || Class === options # has n, :name, 'Class', options: 'here'
    options = (args.last || {}).merge(:model => options.to_s)
  end

  unless Hash === options
    raise ArgumentError, "bad has #{count} options format #{options.inspect}"
  end

  options[:order] = Ardm::Ar::Query.order(self, options[:order]) if options[:order]
  opts = Ardm::Ar::Associations.convert_options(self, options, :through, :order, :source)

  case count
  when 1      then has_one  name, *opts
  when "many" then has_many name, *opts
  end
end
n() click to toggle source
# File lib/ardm/ar/associations.rb, line 113
def n
  "many"
end
relationships() click to toggle source

The reflections returned here don’t look like datamapper relationships. @todo improve this if needed with a wrapper

# File lib/ardm/ar/associations.rb, line 47
def relationships
  reflections
end