Skip to content

Instantly share code, notes, and snippets.

@brentd
Forked from zerobearing2/embedded_callbacks.rb
Created November 14, 2010 01:06
Show Gist options
  • Save brentd/675794 to your computer and use it in GitHub Desktop.
Save brentd/675794 to your computer and use it in GitHub Desktop.
# encoding: utf-8
module Mongoid #:nodoc:
module Associations #:nodoc:
module EmbeddedCallbacks
# bubble callbacks to embedded assocaitions
def run_callbacks(*args)
# now bubble callbacks down
self.associations.each_pair do |name, meta|
if meta.association == Mongoid::Associations::EmbedsMany
self.send(name).each{|doc| doc.send(:run_callbacks, args)}
elsif meta.association == Mongoid::Associations::EmbedsOne
if embedded_doc = self.send(name)
embedded_doc.send(:run_callbacks, args)
end
end
end
super(args) # defer to parent
end
end
end
end
class Photo
include Mongoid::Document
include Mongoid::Timestamps
mount_uploader :image, ImageFileUploader
embedded_in :some_model, :inverse_of => :photos
validates_presence_of :image
end
class SomeModel
include Mongoid::Document
include Mongoid::Associations::EmbeddedCallbacks
include Mongoid::Timestamps
embeds_many :photos
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment