module ThingTank::Callbacks

Public Class Methods

included(klass) click to toggle source
# File lib/thingtank/callbacks.rb, line 3
def self.included(klass)
  klass.class_eval do

    before_validation do
      # update_attributes seems to be intentionally broken for mass_assign_any_attribute, see
      # http://groups.google.com/group/couchrest/browse_thread/thread/3b6aeb6469a0ea35?pli=1
      # try to fix it be changing a property (ugly hack), also see https://github.com/couchrest/couchrest_model/issues/114
      #disable_dirty #true
      couchrest_attribute_will_change!('update_me') # or update_me_will_change!
    end

    before_save do
      @dependencies.save_all()
    end

    # mimic the destroy_document method from https://github.com/langalex/couch_potato/blob/master/lib/couch_potato/database.rb
    before_destroy do
      ok = true
      (self["characters"] || []).each do |klass|
        real_klass = self.class.const_get("Character").__subclasses()[klass.to_sym]
        document = self.as(real_klass)
        (ok = false) if false == document.run_callbacks(:destroy) do
          true
        end
      end
      ok
    end
  end
end