2010-06-17 02:39:09 +02:00
|
|
|
module CouchRest
|
2010-06-20 22:01:11 +02:00
|
|
|
module Model
|
2010-06-17 02:39:09 +02:00
|
|
|
module Associations
|
|
|
|
|
2010-08-04 03:55:17 +02:00
|
|
|
# Basic support for relationships between CouchRest::Model::Base
|
2010-06-17 02:39:09 +02:00
|
|
|
|
|
|
|
def self.included(base)
|
|
|
|
base.extend(ClassMethods)
|
|
|
|
end
|
|
|
|
|
|
|
|
module ClassMethods
|
|
|
|
|
2010-06-17 15:02:33 +02:00
|
|
|
# Define an association that this object belongs to.
|
|
|
|
#
|
2010-06-17 02:39:09 +02:00
|
|
|
def belongs_to(attrib, *options)
|
|
|
|
opts = {
|
|
|
|
:foreign_key => attrib.to_s + '_id',
|
2010-06-17 15:02:33 +02:00
|
|
|
:class_name => attrib.to_s.camelcase,
|
|
|
|
:proxy => nil
|
2010-06-17 02:39:09 +02:00
|
|
|
}
|
|
|
|
case options.first
|
|
|
|
when Hash
|
|
|
|
opts.merge!(options.first)
|
|
|
|
end
|
|
|
|
|
|
|
|
begin
|
|
|
|
opts[:class] = opts[:class_name].constantize
|
|
|
|
rescue
|
2010-06-17 15:02:33 +02:00
|
|
|
raise "Unable to convert class name into Constant for #{self.name}##{attrib}"
|
2010-06-17 02:39:09 +02:00
|
|
|
end
|
|
|
|
|
2010-06-17 15:02:33 +02:00
|
|
|
prop = property(opts[:foreign_key], opts)
|
2010-06-17 02:39:09 +02:00
|
|
|
|
|
|
|
create_belongs_to_getter(attrib, prop, opts)
|
|
|
|
create_belongs_to_setter(attrib, prop, opts)
|
|
|
|
|
|
|
|
prop
|
|
|
|
end
|
|
|
|
|
2010-06-17 15:02:33 +02:00
|
|
|
# Provide access to a collection of objects where the associated
|
|
|
|
# property contains a list of the collection item ids.
|
|
|
|
#
|
|
|
|
# The following:
|
|
|
|
#
|
|
|
|
# collection_of :groups
|
|
|
|
#
|
|
|
|
# creates a pseudo property called "groups" which allows access
|
2010-06-20 22:01:11 +02:00
|
|
|
# to a CollectionOfProxy object. Adding, replacing or removing entries in this
|
2010-06-17 15:02:33 +02:00
|
|
|
# proxy will cause the matching property array, in this case "group_ids", to
|
|
|
|
# be kept in sync.
|
|
|
|
#
|
|
|
|
# Any manual changes made to the collection ids property (group_ids), unless replaced, will require
|
2010-06-20 22:01:11 +02:00
|
|
|
# a reload of the CollectionOfProxy for the two sets of data to be in sync:
|
2010-06-17 15:02:33 +02:00
|
|
|
#
|
|
|
|
# group_ids = ['123']
|
|
|
|
# groups == [Group.get('123')]
|
|
|
|
# group_ids << '321'
|
|
|
|
# groups == [Group.get('123')]
|
|
|
|
# groups(true) == [Group.get('123'), Group.get('321')]
|
|
|
|
#
|
|
|
|
# Of course, saving the parent record will store the collection ids as they are
|
|
|
|
# found.
|
|
|
|
#
|
2010-06-20 22:01:11 +02:00
|
|
|
# The CollectionOfProxy supports the following array functions, anything else will cause
|
2010-06-17 15:02:33 +02:00
|
|
|
# a mismatch between the collection objects and collection ids:
|
|
|
|
#
|
|
|
|
# groups << obj
|
|
|
|
# groups.push obj
|
|
|
|
# groups.unshift obj
|
|
|
|
# groups[0] = obj
|
|
|
|
# groups.pop == obj
|
|
|
|
# groups.shift == obj
|
|
|
|
#
|
|
|
|
# Addtional options match those of the the belongs_to method.
|
|
|
|
#
|
2010-06-18 01:24:49 +02:00
|
|
|
# NOTE: This method is *not* recommended for large collections or collections that change
|
|
|
|
# frequently! Use with prudence.
|
|
|
|
#
|
2010-06-17 15:02:33 +02:00
|
|
|
def collection_of(attrib, *options)
|
|
|
|
opts = {
|
|
|
|
:foreign_key => attrib.to_s.singularize + '_ids',
|
|
|
|
:class_name => attrib.to_s.singularize.camelcase,
|
|
|
|
:proxy => nil
|
|
|
|
}
|
|
|
|
case options.first
|
|
|
|
when Hash
|
|
|
|
opts.merge!(options.first)
|
|
|
|
end
|
|
|
|
begin
|
|
|
|
opts[:class] = opts[:class_name].constantize
|
|
|
|
rescue
|
|
|
|
raise "Unable to convert class name into Constant for #{self.name}##{attrib}"
|
|
|
|
end
|
|
|
|
opts[:readonly] = true
|
|
|
|
|
|
|
|
prop = property(opts[:foreign_key], [], opts)
|
|
|
|
|
|
|
|
create_collection_of_property_setter(attrib, prop, opts)
|
|
|
|
create_collection_of_getter(attrib, prop, opts)
|
|
|
|
create_collection_of_setter(attrib, prop, opts)
|
|
|
|
|
|
|
|
prop
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2010-06-17 02:39:09 +02:00
|
|
|
def create_belongs_to_getter(attrib, property, options)
|
2010-06-17 15:02:33 +02:00
|
|
|
base = options[:proxy] || options[:class_name]
|
2010-06-17 02:39:09 +02:00
|
|
|
class_eval <<-EOS, __FILE__, __LINE__ + 1
|
|
|
|
def #{attrib}
|
2010-06-21 17:03:32 +02:00
|
|
|
@#{attrib} ||= #{options[:foreign_key]}.nil? ? nil : #{base}.get(self.#{options[:foreign_key]})
|
2010-06-17 02:39:09 +02:00
|
|
|
end
|
|
|
|
EOS
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_belongs_to_setter(attrib, property, options)
|
|
|
|
class_eval <<-EOS, __FILE__, __LINE__ + 1
|
|
|
|
def #{attrib}=(value)
|
|
|
|
self.#{options[:foreign_key]} = value.nil? ? nil : value.id
|
2010-06-17 15:02:33 +02:00
|
|
|
@#{attrib} = value
|
|
|
|
end
|
|
|
|
EOS
|
|
|
|
end
|
|
|
|
|
|
|
|
### collection_of support methods
|
|
|
|
|
|
|
|
def create_collection_of_property_setter(attrib, property, options)
|
2010-06-20 22:01:11 +02:00
|
|
|
# ensure CollectionOfProxy is nil, ready to be reloaded on request
|
2010-06-17 15:02:33 +02:00
|
|
|
class_eval <<-EOS, __FILE__, __LINE__ + 1
|
|
|
|
def #{options[:foreign_key]}=(value)
|
|
|
|
@#{attrib} = nil
|
|
|
|
write_attribute("#{options[:foreign_key]}", value)
|
2010-06-17 02:39:09 +02:00
|
|
|
end
|
|
|
|
EOS
|
|
|
|
end
|
|
|
|
|
2010-06-17 15:02:33 +02:00
|
|
|
def create_collection_of_getter(attrib, property, options)
|
|
|
|
base = options[:proxy] || options[:class_name]
|
|
|
|
class_eval <<-EOS, __FILE__, __LINE__ + 1
|
|
|
|
def #{attrib}(reload = false)
|
|
|
|
return @#{attrib} unless @#{attrib}.nil? or reload
|
|
|
|
ary = self.#{options[:foreign_key]}.collect{|i| #{base}.get(i)}
|
2010-06-20 22:01:11 +02:00
|
|
|
@#{attrib} = ::CouchRest::CollectionOfProxy.new(ary, self, '#{options[:foreign_key]}')
|
2010-06-17 15:02:33 +02:00
|
|
|
end
|
|
|
|
EOS
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_collection_of_setter(attrib, property, options)
|
|
|
|
class_eval <<-EOS, __FILE__, __LINE__ + 1
|
|
|
|
def #{attrib}=(value)
|
2010-06-20 22:01:11 +02:00
|
|
|
@#{attrib} = ::CouchRest::CollectionOfProxy.new(value, self, '#{options[:foreign_key]}')
|
2010-06-17 15:02:33 +02:00
|
|
|
end
|
|
|
|
EOS
|
|
|
|
end
|
|
|
|
|
2010-06-17 02:39:09 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
2010-06-17 15:02:33 +02:00
|
|
|
|
|
|
|
# Special proxy for a collection of items so that adding and removing
|
|
|
|
# to the list automatically updates the associated property.
|
2010-06-20 22:01:11 +02:00
|
|
|
class CollectionOfProxy < Array
|
2010-06-17 15:02:33 +02:00
|
|
|
attr_accessor :property
|
|
|
|
attr_accessor :casted_by
|
|
|
|
|
|
|
|
def initialize(array, casted_by, property)
|
|
|
|
self.property = property
|
|
|
|
self.casted_by = casted_by
|
2010-06-18 01:24:49 +02:00
|
|
|
(array ||= []).compact!
|
2010-06-17 15:02:33 +02:00
|
|
|
casted_by[property.to_s] = [] # replace the original array!
|
2010-06-18 01:24:49 +02:00
|
|
|
array.compact.each do |obj|
|
2010-08-04 11:54:02 +02:00
|
|
|
check_obj(obj)
|
2010-06-17 15:02:33 +02:00
|
|
|
casted_by[property.to_s] << obj.id
|
|
|
|
end
|
|
|
|
super(array)
|
|
|
|
end
|
2010-08-04 03:55:17 +02:00
|
|
|
|
2010-06-17 15:02:33 +02:00
|
|
|
def << obj
|
2010-08-04 11:54:02 +02:00
|
|
|
check_obj(obj)
|
2010-06-17 15:02:33 +02:00
|
|
|
casted_by[property.to_s] << obj.id
|
|
|
|
super(obj)
|
|
|
|
end
|
2010-08-04 03:55:17 +02:00
|
|
|
|
2010-06-17 15:02:33 +02:00
|
|
|
def push(obj)
|
2010-08-04 11:54:02 +02:00
|
|
|
check_obj(obj)
|
2010-06-17 15:02:33 +02:00
|
|
|
casted_by[property.to_s].push obj.id
|
|
|
|
super(obj)
|
|
|
|
end
|
2010-08-04 03:55:17 +02:00
|
|
|
|
2010-06-17 15:02:33 +02:00
|
|
|
def unshift(obj)
|
2010-08-04 11:54:02 +02:00
|
|
|
check_obj(obj)
|
2010-06-17 15:02:33 +02:00
|
|
|
casted_by[property.to_s].unshift obj.id
|
|
|
|
super(obj)
|
|
|
|
end
|
|
|
|
|
|
|
|
def []= index, obj
|
2010-08-04 11:54:02 +02:00
|
|
|
check_obj(obj)
|
2010-06-17 15:02:33 +02:00
|
|
|
casted_by[property.to_s][index] = obj.id
|
|
|
|
super(index, obj)
|
|
|
|
end
|
|
|
|
|
|
|
|
def pop
|
|
|
|
casted_by[property.to_s].pop
|
|
|
|
super
|
|
|
|
end
|
2010-08-04 03:55:17 +02:00
|
|
|
|
2010-06-17 15:02:33 +02:00
|
|
|
def shift
|
|
|
|
casted_by[property.to_s].shift
|
|
|
|
super
|
|
|
|
end
|
2010-08-04 11:54:02 +02:00
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def check_obj(obj)
|
|
|
|
raise "Object cannot be added to #{casted_by.class.to_s}##{property.to_s} collection unless saved" if obj.new?
|
|
|
|
end
|
|
|
|
|
2010-06-17 15:02:33 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2010-06-17 02:39:09 +02:00
|
|
|
end
|