Last active
December 17, 2015 06:28
-
-
Save gogogarrett/5565418 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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