Class: PaidUp::Plan

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/paid_up/plan.rb

Overview

PaidUp Plan model

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#stripe_dataObject

Returns the value of attribute stripe_data.



17
18
19
# File 'app/models/paid_up/plan.rb', line 17

def stripe_data
  @stripe_data
end

Instance Method Details

#adjust?(discount) ⇒ Boolean

Returns:

  • (Boolean)


87
88
89
# File 'app/models/paid_up/plan.rb', line 87

def adjust?(discount)
  discount.present? && discount.coupon.present? && !amount.zero?
end

#adjusted_amount(discount) ⇒ Object



71
72
73
74
75
76
77
# File 'app/models/paid_up/plan.rb', line 71

def adjusted_amount(discount)
  return amount unless adjust?(discount)
  adjusted = amount
  adjusted -= (discount.coupon.percent_off || 0) * 0.01 * adjusted
  adjusted -= (discount.coupon.amount_off || 0)
  [adjusted, 0].max
end

#adjusted_money(discount) ⇒ Object



83
84
85
# File 'app/models/paid_up/plan.rb', line 83

def adjusted_money(discount)
  Money.new(adjusted_amount(discount), currency)
end

#amountObject



67
68
69
# File 'app/models/paid_up/plan.rb', line 67

def amount
  stripe_data&.amount || 0
end

#chargeObject



91
92
93
# File 'app/models/paid_up/plan.rb', line 91

def charge
  money.amount
end

#currencyObject



95
96
97
98
99
100
101
# File 'app/models/paid_up/plan.rb', line 95

def currency
  if stripe_data.present?
    stripe_data.currency.upcase
  else
    :default_currency.l.upcase
  end
end

#feature_setting(feature_name) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/models/paid_up/plan.rb', line 42

def feature_setting(feature_name)
  feature = PaidUp::Feature.find_by_slug!(feature_name)
  raw = plan_feature_settings.where(feature: feature_name)
  case feature.setting_type
  when 'boolean'
    raw&.first&.setting == 1
  when 'table_rows', 'rolify_rows'
    raw&.first&.setting || 0
  else
    raise :error_handling_feature_setting.l feature: feature
  end
end

#feature_unlimited?(feature_name) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
# File 'app/models/paid_up/plan.rb', line 55

def feature_unlimited?(feature_name)
  feature_setting(feature_name) == PaidUp::Unlimited.to_i
end

#intervalObject



59
60
61
# File 'app/models/paid_up/plan.rb', line 59

def interval
  stripe_data&.interval || :default_interval.l
end

#interval_countObject



63
64
65
# File 'app/models/paid_up/plan.rb', line 63

def interval_count
  stripe_data&.interval_count || 1
end

#moneyObject



79
80
81
# File 'app/models/paid_up/plan.rb', line 79

def money
  Money.new(amount, currency)
end

#reload(*args, &blk) ⇒ Object



36
37
38
39
40
# File 'app/models/paid_up/plan.rb', line 36

def reload(*args, &blk)
  super(*args, &blk)
  load_stripe_data
  self
end