Created
May 13, 2013 22:37
-
-
Save gogogarrett/5572095 to your computer and use it in GitHub Desktop.
Example of how you can use ruby classes to abstract away from using entirely active record objects.
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
| module Purchase | |
| class Item | |
| attr_reader :student, :item | |
| delegate :cost, to: :item | |
| def initialize(student, item) | |
| @student = student | |
| @item = item | |
| end | |
| def purchase | |
| if ::PurchaseCalc.new.possible?(student.points, cost) | |
| point_ledger = ::PointLedger.new(student) | |
| point_ledger.deduct(cost, item) | |
| process | |
| save_objects | |
| end | |
| end | |
| private | |
| def process | |
| end | |
| def save_objects | |
| ::ActiveRecord::Base.transaction do | |
| student.student_profile.save | |
| student.save | |
| end | |
| end | |
| end | |
| end |
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
| module Purchase | |
| class Souvenir < Item | |
| end | |
| end |
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
| module Purchase | |
| class Trip < Item | |
| def process | |
| student.current_location = item.name | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment