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)


103
104
105
# File 'app/models/paid_up/plan.rb', line 103

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

#adjusted_amount(discount) ⇒ Object



87
88
89
90
91
92
93
# File 'app/models/paid_up/plan.rb', line 87

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



99
100
101
# File 'app/models/paid_up/plan.rb', line 99

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

#amountObject



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

def amount
  stripe_data&.amount || 0
end

#chargeObject



107
108
109
# File 'app/models/paid_up/plan.rb', line 107

def charge
  money.amount
end

#currencyObject



111
112
113
114
115
116
117
# File 'app/models/paid_up/plan.rb', line 111

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

#feature_setting(feature_name) ⇒ Object



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

def feature_setting(feature_name)
  @feature_settings[feature_name]
end

#feature_unlimited?(feature_name) ⇒ Boolean

Returns:

  • (Boolean)


71
72
73
# File 'app/models/paid_up/plan.rb', line 71

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

#intervalObject



75
76
77
# File 'app/models/paid_up/plan.rb', line 75

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

#interval_countObject



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

def interval_count
  stripe_data&.interval_count || 1
end

#load_feature_settingsObject



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

def load_feature_settings
  @feature_settings = {}
  settings = {}

  plan_feature_settings.each do |feature_setting|
    settings[feature_setting.feature] = feature_setting.setting
  end

  PaidUp::Feature.all.each do |feature|
    slug = feature.slug
    @feature_settings[slug] = parse_setting_by_type(feature.setting_type, settings[slug])
  end
end

#moneyObject



95
96
97
# File 'app/models/paid_up/plan.rb', line 95

def money
  Money.new(amount, currency)
end

#parse_setting_by_type(setting_type, setting) ⇒ Object



56
57
58
59
60
61
62
63
64
65
# File 'app/models/paid_up/plan.rb', line 56

def parse_setting_by_type(setting_type, setting)
  case setting_type
  when 'boolean'
    setting == 1
  when 'table_rows', 'rolify_rows'
    setting || 0
  else
    raise :error_handling_feature_setting.l feature: feature
  end
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