Method: Recognition::Extensions::ActiveRecord::ClassMethods#recognize
- Defined in:
- lib/recognition/extensions/active_record.rb
#recognize(recognizable, condition) ⇒ Object
to be called from other models
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/recognition/extensions/active_record.rb', line 24 def recognize recognizable, condition require "recognition/models/recognizer" include Recognition::Models::Recognizer self.recognitions ||= {} self.recognitions[condition[:for]] = { recognizable: recognizable, bucket: condition[:group], gain: condition[:gain], loss: condition[:loss], maximum: condition[:maximum] } # Due to the lack of ActiveRecord before_filter, # we will have to alias the original method in order to intercept if [:create, :update, :destroy].include? condition[:for] include ActiveModel::Dirty else method = condition[:for] define_method "#{method}_with_recognition" do |*args| if self.send("#{method}_without_recognition", *args) Recognition::Database.update_points self, condition[:for], self.class.recognitions[condition[:for]] end end alias_method_chain method, 'recognition' end # For actions that can be intercepted using ActiveRecord callbacks before_destroy :recognize_destroying after_save :recognize_updating # We use after_save for creation to make sure all associations # have been persisted after_save :recognize_creating end |