module PlusPlus::Model

Public Instance Methods

plus_plus_on_create_or_destroy(association, column, options) click to toggle source
# File lib/plus_plus/base.rb, line 56
def plus_plus_on_create_or_destroy(association, column, options)
  association_model = self.send(association)
  raise "No association #{association}" unless association_model
  return if options.has_key?(:if) && !self.instance_exec(&options[:if])
  return if options.has_key?(:unless) && self.instance_exec(&options[:unless])
  value = if options[:value]
    options[:value].respond_to?(:call) ? self.instance_exec(&options[:value]) : options[:value]
  else
    1
  end
  offset  = self.destroyed? ? -(value) : value
  new_val = association_model.send(column) + offset
  association_model.send options[:update_method] || :update_columns, {column => new_val}
end