Skip to content

Instantly share code, notes, and snippets.

@adamgamble
Created February 6, 2012 15:29
Show Gist options
  • Save adamgamble/1752669 to your computer and use it in GitHub Desktop.
Save adamgamble/1752669 to your computer and use it in GitHub Desktop.
module Block
class Associated < Base
def self.associate_many(field_name)
lambda {
field :"#{field_name}_uids", :type => Array, :default => []
define_method :"#{field_name}_uids=" do |arr|
arr.shift if arr.first == '0'
Rails.logger.warn "************************** TEST"
super(arr.collect {|uid| uid.kind_of?(BSON::ObjectId) ? uid : BSON::ObjectId(uid)})
end
define_method :"#{field_name}=" do |nodes|
self.attributes["#{field_name}_uids"] = nodes.collect(&:uid)
end
define_method :"#{field_name}" do
uids = send(:"#{field_name}_uids")
return [] if uids.nil? || uids.empty?
#uids.collect! {|uid| uid.kind_of?(BSON::ObjectId) ? uid : BSON::ObjectId(uid)}
docs = self.class.superclass.viewable.any_in(:uid => uids)
uids.collect do |uid|
docs.detect {|doc| doc.uid == uid }
end.compact
end
}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment