Module: ThingTank::Callbacks

Included in:
ThingTank
Defined in:
lib/thingtank/callbacks.rb

Class Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# 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