Class: SolidusSubscriptions::Subscription

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Interval
Defined in:
app/models/solidus_subscriptions/subscription.rb

Constant Summary collapse

PROCESSING_STATES =
[:pending, :failed, :success]

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Interval

included, #interval

Class Method Details

.processing_statesObject



71
72
73
# File 'app/models/solidus_subscriptions/subscription.rb', line 71

def self.processing_states
  PROCESSING_STATES
end

.ransackable_scopes(_auth_object = nil) ⇒ Object



67
68
69
# File 'app/models/solidus_subscriptions/subscription.rb', line 67

def self.ransackable_scopes(_auth_object = nil)
  [:in_processing_state]
end

Instance Method Details

#advance_actionable_dateDate

Advance the actionable date to the next_actionable_date value. Will modify the record.

subscription will be eligible to be processed.

Returns:

  • (Date)

    The next date after the current actionable_date this



170
171
172
173
# File 'app/models/solidus_subscriptions/subscription.rb', line 170

def advance_actionable_date
  update! actionable_date: next_actionable_date
  actionable_date
end

#can_be_canceled?Boolean

This method determines if a subscription may be canceled. Canceled subcriptions will not be processed. By default subscriptions may always be canceled. If this method is overriden to return false, the subscription will be moved to the :pending_cancellation state until it is canceled again and this condition is true.

USE CASE: Subscriptions can only be canceled more than 10 days before they are processed. Override this method to be:

def can_be_canceled?

return true if actionable_date.nil?
(actionable_date - 10.days.from_now.to_date) > 0

end

If a user cancels this subscription less than 10 days before it will be processed the subscription will be bumped into the :pending_cancellation state instead of being canceled. Susbcriptions pending cancellation will still be processed.

Returns:

  • (Boolean)


128
129
130
131
# File 'app/models/solidus_subscriptions/subscription.rb', line 128

def can_be_canceled?
  return true if actionable_date.nil?
  (actionable_date - Config.minimum_cancellation_notice).future?
end

#can_be_deactivated?Boolean

This method determines if a subscription can be deactivated. A deactivated subscription will not be processed. By default a subscription can be deactivated if the end_date defined on subscription_line_item is less than the current date In this case the subscription has been fulfilled and should not be processed again. Subscriptions without an end_date value cannot be deactivated.

Returns:

  • (Boolean)


149
150
151
# File 'app/models/solidus_subscriptions/subscription.rb', line 149

def can_be_deactivated?
  active? && line_item.end_date && actionable_date > line_item.end_date
end

#line_item_builderSolidusSubscriptions::LineItemBuilder

Get the builder for the subscription_line_item. This will be an object that can generate the appropriate line item for the subscribable object



180
181
182
# File 'app/models/solidus_subscriptions/subscription.rb', line 180

def line_item_builder
  LineItemBuilder.new(line_items)
end

#next_actionable_dateDate

Get the date after the current actionable_date where this subscription will be actionable again

Returns:

  • (Date)

    The current actionable_date plus 1 interval. The next date after the current actionable_date this subscription will be eligible to be processed.



159
160
161
162
163
# File 'app/models/solidus_subscriptions/subscription.rb', line 159

def next_actionable_date
  return nil unless active?
  new_date = (actionable_date || Time.zone.now)
  (new_date + interval).beginning_of_minute
end

#processing_stateString

The state of the last attempt to process an installment associated to this subscrtipion

Returns:

  • (String)

    pending if the no installments have been processed, failed if the last installment has not been fulfilled and, success if the last installment was fulfilled.



190
191
192
193
# File 'app/models/solidus_subscriptions/subscription.rb', line 190

def processing_state
  return 'pending' if installments.empty?
  installments.last.fulfilled? ? 'success' : 'failed'
end

#skipObject



133
134
135
136
137
138
139
140
# File 'app/models/solidus_subscriptions/subscription.rb', line 133

def skip
  check_successive_skips_exceeded
  check_total_skips_exceeded

  return if errors.any?

  advance_actionable_date
end