Class: MembershipCancellation

Inherits:
Object
  • Object
show all
Defined in:
app/models/membership_cancellation.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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_membershipsObject (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_idsObject (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_atObject



15
16
17
# File 'app/models/membership_cancellation.rb', line 15

def expired_at
  @expired_at ||= 1.second.ago
end

#membershipsObject



19
20
21
# File 'app/models/membership_cancellation.rb', line 19

def memberships
  @memberships ||= Membership.where(id: membership_ids)
end

#non_refundablesObject



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

#ownerObject



68
69
70
# File 'app/models/membership_cancellation.rb', line 68

def owner
  @owner ||= memberships.first.item.order.person
end

#performObject



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_amountObject



31
32
33
# File 'app/models/membership_cancellation.rb', line 31

def refund_amount
  @refund_amount ||= refundables.map(&:price).sum
end

#refund_available?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'app/models/membership_cancellation.rb', line 27

def refund_available?
  !refundables.blank?
end

#refundable_ordersObject



43
44
45
# File 'app/models/membership_cancellation.rb', line 43

def refundable_orders
  @refundable_orders ||= refundables.map(&:item).map(&:order).uniq
end

#refundablesObject



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