Atomic updates should invoke callbacks

XMLWordPrintableJSON

    • Type: Task
    • Resolution: Done
    • 4.0.0 final
    • Affects Version/s: None
    • Component/s: None
    • None
    • None
    • None
    • None
    • None
    • None
    • None
    • None

      It could have been like this by design, but it's pretty common to want to invoke callbacks for atomic updates. A valid scenario is invalidating a cache (see https://github.com/dblock/mongoid-cached-json/issues/7).

      If not invoking before_save/update on an inc is by design, maybe there should be a built-in callback for inc or similar atomic operations?

      require 'spec_helper'
      
      class Gadget
        include Mongoid::Document
      
        field :count, type: Integer
        before_update :called
        before_save :called
      
        def callback_count
          @callback_count
        end
      
        def called
          @callback_count = @callback_count.to_i + 1
        end
      end
      
      describe "inc" do
        it "calls the callback" do
          gadget = Gadget.create!
          gadget.callback_count.should == 1 # before_save
          gadget.update_attributes!({ count: 10 })
          gadget.callback_count.should == 3 # before save and update
          gadget.inc(:count, 1)
          gadget.callback_count.should == 4
        end
      end
      

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

              Created:
              Updated:
              Resolved: