Class: Payment
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Payment
- Defined in:
- app/models/payment.rb
Instance Method Summary collapse
- #actions ⇒ Object
-
#build_source(params) ⇒ Object
With nested attributes, Rails calls build_ for the nested model which won’t work for a polymorphic association.
- #can_credit? ⇒ Boolean
- #credit(amount) ⇒ Object
- #credit_allowed ⇒ Object
- #offsets_total ⇒ Object
- #payment_source ⇒ Object
- #process! ⇒ Object
Instance Method Details
#actions ⇒ Object
85 86 87 88 |
# File 'app/models/payment.rb', line 85 def actions return [] unless payment_source and payment_source.respond_to? :actions payment_source.actions.select { |action| !payment_source.respond_to?("can_#{action}?") or payment_source.send("can_#{action}?", self) } end |
#build_source(params) ⇒ Object
With nested attributes, Rails calls build_ for the nested model which won’t work for a polymorphic association
72 73 74 75 76 |
# File 'app/models/payment.rb', line 72 def build_source(params) if payment_method and payment_method.payment_source_class self.source = payment_method.payment_source_class.new(params) end end |
#can_credit? ⇒ Boolean
61 62 63 |
# File 'app/models/payment.rb', line 61 def can_credit? credit_allowed > 0 end |
#credit(amount) ⇒ Object
65 66 67 68 69 |
# File 'app/models/payment.rb', line 65 def credit(amount) return if amount > credit_allowed started_processing! source.credit(self, amount) end |
#credit_allowed ⇒ Object
57 58 59 |
# File 'app/models/payment.rb', line 57 def credit_allowed amount - offsets_total end |
#offsets_total ⇒ Object
53 54 55 |
# File 'app/models/payment.rb', line 53 def offsets_total offsets.map(&:amount).sum end |
#payment_source ⇒ Object
90 91 92 |
# File 'app/models/payment.rb', line 90 def payment_source source.is_a?(Payment) ? source.source : source end |
#process! ⇒ Object
78 79 80 81 82 83 |
# File 'app/models/payment.rb', line 78 def process! if !processing? and source and source.respond_to?(:process!) started_processing! source.process!(self) # source is responsible for updating the payment state when it's done processing end end |