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.
|
2011-04-04 01:10:31 +02:00
|
|
|
#
|
|
|
|
# An attribute will be created matching the name of the attribute
|
|
|
|
# with '_id' on the end, or the foreign key (:foreign_key) provided.
|
|
|
|
#
|
|
|
|
# Searching for the assocated object is performed using a string
|
|
|
|
# (:proxy) to be evaulated in the context of the owner. Typically
|
|
|
|
# this will be set to the class name (:class_name), or determined
|
|
|
|
# automatically if the owner belongs to a proxy object.
|
|
|
|
#
|
|
|
|
# If the association owner is proxied by another model, than an attempt will
|
|
|
|
# be made to automatically determine the correct place to request
|
|
|
|
# the documents. Typically, this is a method with the pluralized name of the
|
|
|
|
# association inside owner's owner, or proxy.
|
|
|
|
#
|
|
|
|
# For example, imagine a company acts as a proxy for invoices and clients.
|
|
|
|
# If an invoice belongs to a client, the invoice will need to access the
|
|
|
|
# list of clients via the proxy. So a request to search for the associated
|
|
|
|
# client from an invoice would look like:
|
|
|
|
#
|
|
|
|
# self.company.clients
|
|
|
|
#
|
|
|
|
# If the name of the collection proxy is not the pluralized assocation name,
|
|
|
|
# it can be set with the :proxy_name option.
|
|
|
|
#
|
2010-06-17 02:39:09 +02:00
|
|
|
def belongs_to(attrib, *options)
|
2011-04-05 20:41:24 +02:00
|
|
|
opts = merge_belongs_to_association_options(attrib, options.first)
|
2010-06-17 02:39:09 +02:00
|
|
|
|
2011-04-05 20:41:24 +02:00
|
|
|
property(opts[:foreign_key], opts)
|
2010-06-17 02:39:09 +02:00
|
|
|
|
2011-04-05 20:41:24 +02:00
|
|
|
create_belongs_to_getter(attrib, opts)
|
|
|
|
create_belongs_to_setter(attrib, opts)
|
2010-06-17 02:39:09 +02:00
|
|
|
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)
|
2011-04-05 20:41:24 +02:00
|
|
|
opts = merge_belongs_to_association_options(attrib, options.first)
|
2011-04-04 01:10:31 +02:00
|
|
|
opts[:foreign_key] = opts[:foreign_key].pluralize
|
2010-06-17 15:02:33 +02:00
|
|
|
opts[:readonly] = true
|
|
|
|
|
2011-04-05 20:41:24 +02:00
|
|
|
property(opts[:foreign_key], [], opts)
|
2010-06-17 15:02:33 +02:00
|
|
|
|
2011-04-05 20:41:24 +02:00
|
|
|
create_collection_of_property_setter(attrib, opts)
|
|
|
|
create_collection_of_getter(attrib, opts)
|
|
|
|
create_collection_of_setter(attrib, opts)
|
2010-06-17 15:02:33 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2011-04-05 20:41:24 +02:00
|
|
|
def merge_belongs_to_association_options(attrib, options = nil)
|
2011-04-04 01:10:31 +02:00
|
|
|
opts = {
|
2011-04-05 20:41:24 +02:00
|
|
|
:foreign_key => attrib.to_s.singularize + '_id',
|
|
|
|
:class_name => attrib.to_s.singularize.camelcase,
|
2011-04-04 01:10:31 +02:00
|
|
|
:proxy_name => attrib.to_s.pluralize
|
|
|
|
}
|
2011-04-05 20:41:24 +02:00
|
|
|
opts.merge!(options) if options.is_a?(Hash)
|
2011-04-04 01:10:31 +02:00
|
|
|
|
|
|
|
# Generate a string for the proxy method call
|
|
|
|
# Assumes that the proxy_owner_method from "proxyable" is available.
|
|
|
|
if opts[:proxy].to_s.empty?
|
|
|
|
opts[:proxy] = if proxy_owner_method
|
|
|
|
"self.#{proxy_owner_method}.#{opts[:proxy_name]}"
|
|
|
|
else
|
2011-04-05 20:41:24 +02:00
|
|
|
opts[:class_name]
|
2011-04-04 01:10:31 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
opts
|
|
|
|
end
|
|
|
|
|
2011-04-05 20:41:24 +02:00
|
|
|
def create_belongs_to_getter(attrib, options)
|
2010-06-17 02:39:09 +02:00
|
|
|
class_eval <<-EOS, __FILE__, __LINE__ + 1
|
|
|
|
def #{attrib}
|
2011-04-04 01:10:31 +02:00
|
|
|
@#{attrib} ||= #{options[:foreign_key]}.nil? ? nil : #{options[:proxy]}.get(self.#{options[:foreign_key]})
|
2010-06-17 02:39:09 +02:00
|
|
|
end
|
|
|
|
EOS
|
|
|
|
end
|
|
|
|
|
2011-04-05 20:41:24 +02:00
|
|
|
def create_belongs_to_setter(attrib, options)
|
2010-06-17 02:39:09 +02:00
|
|
|
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
|
|
|
|
|
2011-04-05 20:41:24 +02:00
|
|
|
def create_collection_of_property_setter(attrib, 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
|
|
|
|
|
2011-04-05 20:41:24 +02:00
|
|
|
def create_collection_of_getter(attrib, options)
|
2010-06-17 15:02:33 +02:00
|
|
|
class_eval <<-EOS, __FILE__, __LINE__ + 1
|
|
|
|
def #{attrib}(reload = false)
|
|
|
|
return @#{attrib} unless @#{attrib}.nil? or reload
|
2011-04-05 20:41:24 +02:00
|
|
|
ary = self.#{options[:foreign_key]}.collect{|i| #{options[:proxy]}.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
|
|
|
|
|
2011-04-05 20:41:24 +02:00
|
|
|
def create_collection_of_setter(attrib, options)
|
2010-06-17 15:02:33 +02:00
|
|
|
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
|