Skip to content

Instantly share code, notes, and snippets.

@zerobearing2
Created September 5, 2010 19:01

Revisions

  1. zerobearing2 revised this gist Sep 5, 2010. 1 changed file with 1 addition and 4 deletions.
    5 changes: 1 addition & 4 deletions embedded_callbacks.rb
    Original file line number Diff line number Diff line change
    @@ -5,8 +5,6 @@ module EmbeddedCallbacks

    # bubble callbacks to embedded assocaitions
    def run_callbacks(*args)
    parent_callback_result = super(args) # defer to parent

    # now bubble callbacks down
    self.associations.each_pair do |name, meta|
    if meta.association == Mongoid::Associations::EmbedsMany
    @@ -15,8 +13,7 @@ def run_callbacks(*args)
    self.send(name).send(:run_callbacks, args)
    end
    end

    parent_callback_result
    super(args) # defer to parent
    end

    end
  2. zerobearing2 created this gist Sep 5, 2010.
    24 changes: 24 additions & 0 deletions embedded_callbacks.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    # encoding: utf-8
    module Mongoid #:nodoc:
    module Associations #:nodoc:
    module EmbeddedCallbacks

    # bubble callbacks to embedded assocaitions
    def run_callbacks(*args)
    parent_callback_result = super(args) # defer to parent

    # 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
    self.send(name).send(:run_callbacks, args)
    end
    end

    parent_callback_result
    end

    end
    end
    end
    8 changes: 8 additions & 0 deletions photo.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    class Photo
    include Mongoid::Document
    include Mongoid::Timestamps

    mount_uploader :image, ImageFileUploader
    embedded_in :some_model, :inverse_of => :photos
    validates_presence_of :image
    end
    6 changes: 6 additions & 0 deletions some_model.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    class SomeModel
    include Mongoid::Document
    include Mongoid::Associations::EmbeddedCallbacks
    include Mongoid::Timestamps
    embeds_many :photos
    end