Skip to content

Instantly share code, notes, and snippets.

@gogogarrett
Created May 13, 2013 22:37
Show Gist options
  • Select an option

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

Select an option

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.
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
module Purchase
class Souvenir < Item
end
end
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