Sha256: 7cc62d7ab1032fdde5c97db32855b1684a47da9a6a25b6204a08f20bb73eb02a

Contents?: true

Size: 1.13 KB

Versions: 1

Compression:

Stored size: 1.13 KB

Contents

require "increment_with_sql/version"
require "active_record"

module IncrementWithSql
  class NotPersistedError < StandardError; end
  class InvalidAttributeError < StandardError; end

  def increment_with_sql!(attribute, by = 1)
    raise(NotPersistedError, "Record not persisted") if new_record?
    raise(InvalidAttributeError, "Invalid attribute #{attribute}") unless attribute_names.include?(attribute.to_s)

    self.class.transaction do
      self.class.where(:id => id).update_all("#{self.class.connection.quote_column_name attribute} = CASE WHEN #{self.class.connection.quote_column_name attribute} IS NULL THEN 0 ELSE #{self.class.connection.quote_column_name attribute} END + #{by.to_i}")

      send "#{attribute}=", self.class.unscoped.where(:id => id).select(attribute).first.send(attribute)

      if respond_to?(:clear_attribute_changes, true)
        send :clear_attribute_changes, [attribute]
      else
        changed_attributes.except! attribute.to_s
      end
    end 

    self
  end 

  def decrement_with_sql!(attribute, by = 1)
    increment_with_sql! attribute, by * -1
  end 
end

ActiveRecord::Base.send :include, IncrementWithSql

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
increment_with_sql-0.0.3 lib/increment_with_sql.rb