Uploaded image for project: 'Mongoid'
  1. Mongoid
  2. MONGOID-685

Callbacks trickling down into embedded documents

    • Type: Icon: Task Task
    • Resolution: Done
    • 12_01_17
    • Affects Version/s: None
    • Component/s: None
    • Labels:

      I know there are performance issues with this trickling down (and possibly other downsides too that I haven't thought of), still if would be nice if there was an option to have callbacks trickle down, similar to the :autosave flag on relations.

      This code should do the trick:

      module Mongoid #:nodoc:
      module Associations #:nodoc:
      module EmbeddedCallbacks

      1. bubble callbacks to embedded associations
        def run_callbacks(kind, *args, &block)
      2. now bubble callbacks down
        self.relations.each_pair do |name, meta|
        if meta.relation == Mongoid::Relations::Embedded::Many
        self.send(name).each { |doc| doc.send(:run_callbacks, kind, *args, &block) }

        elsif meta.association == Mongoid::Relations::Embedded::One
        self.send(name).send(:run_callbacks, kind, *args, &block)
        end
        end
        super(kind, *args, &block) # defer to parent
        end

      end
      end
      end

            Assignee:
            Unassigned Unassigned
            Reporter:
            sandstrom sandstrom
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: