Module: StripeSaas::Plan

Extended by:
ActiveSupport::Concern
Included in:
Plan
Defined in:
app/concerns/stripe_saas/plan.rb

Instance Method Summary collapse

Instance Method Details

#add_feature(feature, value, display_value = nil) ⇒ Object

Raises:

  • (ActiveRecord::RecordNotFound)


20
21
22
23
24
25
26
27
28
29
# File 'app/concerns/stripe_saas/plan.rb', line 20

def add_feature(feature, value, display_value=nil)
  feature = Feature.find_by(name: feature.to_s) if feature.is_a?(String) || feature.is_a?(Symbol)

  raise(ActiveRecord::RecordNotFound, "The feature #{feature.to_s} does not exist") if feature.nil?

  plan_features.find_or_create_by(feature: feature).update({
    value: value,
    display_value: display_value
  })
end

#allows?(feature) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
54
# File 'app/concerns/stripe_saas/plan.rb', line 51

def allows?(feature)
  plan_feature = plan_features.joins(:feature).find_by(features: {name: feature.to_s})
  plan_feature.feature.feature_type == 'boolean' ? plan_feature.value : (plan_feature.value > 0)
end

#boolean_plan_featuresObject



39
40
41
# File 'app/concerns/stripe_saas/plan.rb', line 39

def boolean_plan_features
  plan_features.joins(:feature).where(features: {feature_type: 'boolean'}).order("features.display_order")
end

#feature_value(feature) ⇒ Object



47
48
49
# File 'app/concerns/stripe_saas/plan.rb', line 47

def feature_value(feature)
  plan_features.joins(:feature).find_by(features: {name: feature.to_s}).value
end

#free?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'app/concerns/stripe_saas/plan.rb', line 16

def free?
  price_cents.zero?
end

#has_feature?(feature) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
34
35
36
37
# File 'app/concerns/stripe_saas/plan.rb', line 31

def has_feature?(feature)
  if feature.is_a?(String)
    !features.find_by(name: feature).nil?
  else
    features.any? { |f| f.id == feature.id }
  end
end

#is_downgrade_from?(plan) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
# File 'app/concerns/stripe_saas/plan.rb', line 12

def is_downgrade_from?(plan)
  !is_upgrade_from?(plan)
end

#is_upgrade_from?(plan) ⇒ Boolean

Returns:

  • (Boolean)


8
9
10
# File 'app/concerns/stripe_saas/plan.rb', line 8

def is_upgrade_from?(plan)
  (price_cents || 0) >= (plan.price_cents || 0)
end

#metadataObject



56
57
58
# File 'app/concerns/stripe_saas/plan.rb', line 56

def 
  self..present? ? JSON::parse(self.) : {}
end

#metadata=(metadata_hash) ⇒ Object



60
61
62
# File 'app/concerns/stripe_saas/plan.rb', line 60

def metadata=()
  self. = .to_json
end

#non_boolean_plan_featuresObject



43
44
45
# File 'app/concerns/stripe_saas/plan.rb', line 43

def non_boolean_plan_features
  plan_features.joins(:feature).where.not(features: {feature_type: 'boolean'}).order("features.display_order")
end