Skip to content

Instantly share code, notes, and snippets.

@gogogarrett
Last active December 17, 2015 06:28
Show Gist options
  • Select an option

  • Save gogogarrett/5565418 to your computer and use it in GitHub Desktop.

Select an option

Save gogogarrett/5565418 to your computer and use it in GitHub Desktop.
class PointLedger
attr_reader :user
def initialize(user)
@user = user
end
def add(amount, item)
user.point_transactions.build(amount: amount, item_type: item.class, item_name: item.name)
user.points += amount
user.save
end
def deduct(amount, item)
user.point_transactions.build(amount: -amount, item_type: item.class, item_name: item.name)
user.points -= amount
user.save
end
end
# will eventually have a "Trip" calc that will
# take current users location and end location and see
# if _that_ is .possible?
class PurchaseCalc
def possible?(user, item)
user.points >= item.price
end
end
class PurchaseService
attr_reader :user
def initialize(user)
@user = user
end
def purchase(item)
if PurchaseCalc.new.possible?(user, item)
point_ledger = PointLedger.new(user)
point_ledger.deduct(10, item)
end
end
end
location = Location.find(1)
if Service::Purchase.new(user).purchase(location)
Service::UserLocation.new(user).update(location)
else
render :back, alert: "error"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment