class Upmin::Association

Attributes

model[R]
name[R]

Public Class Methods

new(model, assoc_name, options = {}) click to toggle source
# File lib/upmin/association.rb, line 6
def initialize(model, assoc_name, options = {})
  if model.class.active_record?
    extend Upmin::ActiveRecord::Association
  elsif model.class.data_mapper?
    extend Upmin::DataMapper::Association
  else
    raise ArgumentError.new(model)
  end

  @model = model
  @name = assoc_name.to_sym
end

Public Instance Methods

collection?() click to toggle source
# File lib/upmin/association.rb, line 43
def collection?
  raise NotImplementedError
end
empty?() click to toggle source
# File lib/upmin/association.rb, line 47
def empty?
  if collection?
    return value.count == 0
  else
    return ![value].flatten.any?
  end
end
title() click to toggle source
# File lib/upmin/association.rb, line 24
def title
  return name.to_s.humanize
end
type() click to toggle source
# File lib/upmin/association.rb, line 39
def type
  raise NotImplementedError
end
upmin_values(options = {}) click to toggle source
# File lib/upmin/association.rb, line 28
def upmin_values(options = {})
  options[:limit] ||= 5
  if collection?
    vals = [value.limit(options[:limit])].flatten
  else
    vals = [value]
  end

  return vals.map(&:upmin_model)
end
value() click to toggle source
# File lib/upmin/association.rb, line 19
def value
  # TODO(jon): Add some way to handle exceptions.
  return model.send(name)
end

Private Instance Methods

infer_type_from_value() click to toggle source
# File lib/upmin/association.rb, line 59
def infer_type_from_value
  if first = [value].flatten.first
    type = first.class.name.underscore
    if collection?
      return type.pluralize.to_sym
    else
      return type.to_sym
    end
  else
    return :unknown
  end
end