Module: PlusPlus::Model

Defined in:
lib/plus_plus/base.rb

Instance Method Summary collapse

Instance Method Details

#plus_plus_on_create_or_destroy(association, column, options) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# 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