Class: Wbase::Subscription

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.currentObject



30
31
32
# File 'app/models/wbase/subscription.rb', line 30

def self.current
  where('paid_thru > ?', 7.days.ago)
end

.monthly_valueObject



38
39
40
41
# File 'app/models/wbase/subscription.rb', line 38

def self.monthly_value
  current.where(plan: Plan.monthly).sum(:current_price) +
    current.where(plan: Plan.annual).sum(:current_price).fdiv(12)
end

Instance Method Details

#current?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'app/models/wbase/subscription.rb', line 61

def current?
  paid_thru && paid_thru > 7.days.ago
end

#monthly_valueObject



43
44
45
46
47
48
49
# File 'app/models/wbase/subscription.rb', line 43

def monthly_value
  if plan.interval.include?("month")
    current_price
  else
    current_price.fdiv(12)
  end
end

#stripe_customerObject



34
35
36
# File 'app/models/wbase/subscription.rb', line 34

def stripe_customer
  @stripe_customer ||= Stripe::Customer.retrieve(stripe_id)
end

#trial?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'app/models/wbase/subscription.rb', line 65

def trial?
  created_at > 14.days.ago
end

#trial_days_leftObject



69
70
71
72
# File 'app/models/wbase/subscription.rb', line 69

def trial_days_left
  return 0 unless trial?
  (conversion_day.to_date - Date.current.to_date).to_i
end

#update_paid_thru!Object



51
52
53
54
55
56
57
58
59
# File 'app/models/wbase/subscription.rb', line 51

def update_paid_thru!
  if stripe_customer && stripe_customer.try(:subscriptions).try(:first)
    remote_paid_thru = Time.at(stripe_customer.subscriptions.first.try(:current_period_end))
    if remote_paid_thru > paid_thru
      return self.update(paid_thru: remote_paid_thru)
    end
  end
  false
end