Class: MembershipCancellation
- Inherits:
-
Object
- Object
- MembershipCancellation
- Defined in:
- app/models/membership_cancellation.rb
Instance Attribute Summary collapse
-
#cancelled_memberships ⇒ Object
readonly
Returns the value of attribute cancelled_memberships.
-
#membership_ids ⇒ Object
readonly
Returns the value of attribute membership_ids.
Class Method Summary collapse
Instance Method Summary collapse
- #expired_at ⇒ Object
-
#initialize(membership_ids = nil) ⇒ MembershipCancellation
constructor
A new instance of MembershipCancellation.
- #memberships ⇒ Object
- #non_refundables ⇒ Object
- #owner ⇒ Object
- #perform ⇒ Object
- #refund_amount ⇒ Object
- #refund_available? ⇒ Boolean
- #refundable_orders ⇒ Object
- #refundables ⇒ Object
- #refundables_for(order) ⇒ Object
Constructor Details
#initialize(membership_ids = nil) ⇒ MembershipCancellation
Returns a new instance of MembershipCancellation.
5 6 7 8 |
# File 'app/models/membership_cancellation.rb', line 5 def initialize(membership_ids=nil) @cancelled_memberships = [] @membership_ids = Array.wrap(membership_ids) unless membership_ids.nil? end |
Instance Attribute Details
#cancelled_memberships ⇒ Object (readonly)
Returns the value of attribute cancelled_memberships.
2 3 4 |
# File 'app/models/membership_cancellation.rb', line 2 def cancelled_memberships @cancelled_memberships end |
#membership_ids ⇒ Object (readonly)
Returns the value of attribute membership_ids.
3 4 5 |
# File 'app/models/membership_cancellation.rb', line 3 def membership_ids @membership_ids end |
Class Method Details
.enqueue(membership_ids) ⇒ Object
10 11 12 13 |
# File 'app/models/membership_cancellation.rb', line 10 def self.enqueue(membership_ids) membership_ids = Array.wrap(membership_ids) Delayed::Job.enqueue(MembershipCancellation.new(membership_ids)) end |
Instance Method Details
#expired_at ⇒ Object
15 16 17 |
# File 'app/models/membership_cancellation.rb', line 15 def expired_at @expired_at ||= 1.second.ago end |
#memberships ⇒ Object
19 20 21 |
# File 'app/models/membership_cancellation.rb', line 19 def memberships @memberships ||= Membership.where(id: membership_ids) end |
#non_refundables ⇒ Object
23 24 25 |
# File 'app/models/membership_cancellation.rb', line 23 def non_refundables @non_refundables ||= memberships.select { |m| !m.item.refundable? || !m.item.order.credit?} end |
#owner ⇒ Object
68 69 70 |
# File 'app/models/membership_cancellation.rb', line 68 def owner @owner ||= memberships.first.item.order.person end |
#perform ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'app/models/membership_cancellation.rb', line 47 def perform Order.transaction do # Handle refunds refundable_orders.each do |order| # Find refundable memberships for this order refundables = refundables_for(order) # Submit the refund refund = Refund.new(order, refundables.map(&:item)) refund.submit(:send_email_confirmation => true) cancel_memberships!(refundables) if refund.successful? end # Cancel non-refundable memberships cancel_memberships!(non_refundables) end end |
#refund_amount ⇒ Object
31 32 33 |
# File 'app/models/membership_cancellation.rb', line 31 def refund_amount @refund_amount ||= refundables.map(&:price).sum end |
#refund_available? ⇒ Boolean
27 28 29 |
# File 'app/models/membership_cancellation.rb', line 27 def refund_available? !refundables.blank? end |
#refundable_orders ⇒ Object
43 44 45 |
# File 'app/models/membership_cancellation.rb', line 43 def refundable_orders @refundable_orders ||= refundables.map(&:item).map(&:order).uniq end |
#refundables ⇒ Object
35 36 37 |
# File 'app/models/membership_cancellation.rb', line 35 def refundables @refundables ||= memberships.select {|m| m.item.refundable? && m.item.order.credit? } end |
#refundables_for(order) ⇒ Object
39 40 41 |
# File 'app/models/membership_cancellation.rb', line 39 def refundables_for(order) refundables.select { |r| r.item.order.id == order.id } end |