Last active
December 21, 2015 04:58
-
-
Save crispincornett/6253344 to your computer and use it in GitHub Desktop.
Polymorphic Invoice Charges
This file contains 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 Invoice < ActiveRecord::Base | |
belongs_to :account | |
has_many :charges | |
end | |
class Charge < ActiveRecord::Base | |
belongs_to :invoice | |
belongs_to :chargeable, :polymorphic => true | |
delegate :total, to: :chargeable, prefix: true | |
scope :ordered_by_date, -> { order(:created_at) } | |
def self.line_items_only | |
where(:chargeable_type => "LineItem") | |
end | |
def self.line_items | |
LineItem.where(:id => line_items_only.pluck(:chargeable_id)) | |
end | |
def self.interest_charges_only | |
where(:chargeable_type => "InterestCharge") | |
end | |
def self.interest_charges | |
InterestCharge.where(:id => interest_charges_only.pluck(:chargeable_id)) | |
end | |
def self.adjustments_only | |
where(:chargeable_type => "Adjustment") | |
end | |
def self.adjustments | |
Adjustment.where(:id => adjustments_only.pluck(:chargeable_id)) | |
end | |
end | |
class LineItem < ActiveRecord::Base | |
has_one :charge, :as => :chargeable | |
end | |
class InterestCharge < ActiveRecord::Base | |
has_one :charge, :as => :chargeable | |
end | |
class Adjustment < ActiveRecord::Base | |
has_one :charge, :as => :chargeable | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment