Module: Royal::Points

Extended by:
ActiveSupport::Concern
Defined in:
lib/royal/points.rb

Instance Method Summary collapse

Instance Method Details

#add_points(amount, reason: nil, pointable: nil) ⇒ Integer

Adds a number of points to the record’s current points balance.

Parameters:

  • amount (Integer)

    The number of points to add to the blance.

  • reason (String, nil) (defaults to: nil)

    An optional reason to store with the balance change.

  • pointable (ActiveRecord::Base, nil) (defaults to: nil)

    An optional record to associate to the balance change.

Returns:

  • (Integer)

    Returns the new points balance.



27
28
29
# File 'lib/royal/points.rb', line 27

def add_points(amount, reason: nil, pointable: nil)
  point_balances.apply_change_to_points(self, amount, reason: reason, pointable: pointable)
end

#current_pointsInteger

Returns the current number of points in the record’s balance.

Returns:

  • (Integer)


17
18
19
# File 'lib/royal/points.rb', line 17

def current_points
  point_balances.order(sequence: :desc).limit(1).pluck(:balance).first || 0
end

#subtract_points(amount, reason: nil, pointable: nil) ⇒ Integer

Subtracts a number of points to the record’s current points balance.

Parameters:

  • amount (Integer)

    The number of points to subtract from the balance.

  • reason (String, nil) (defaults to: nil)

    An optional reason to store with the balance change.

  • pointable (ActiveRecord::Base, nil) (defaults to: nil)

    An optional record to associate to the balance change.

Returns:

  • (Integer)

    Returns the new points balance.



37
38
39
# File 'lib/royal/points.rb', line 37

def subtract_points(amount, reason: nil, pointable: nil)
  point_balances.apply_change_to_points(self, -amount, reason: reason, pointable: pointable)
end