Module: StripeSaas::Plan
Instance Method Summary collapse
- #add_feature(feature, value, display_value = nil) ⇒ Object
- #allows?(feature) ⇒ Boolean
- #boolean_plan_features ⇒ Object
- #feature_value(feature) ⇒ Object
- #free? ⇒ Boolean
- #has_feature?(feature) ⇒ Boolean
- #is_downgrade_from?(plan) ⇒ Boolean
- #is_upgrade_from?(plan) ⇒ Boolean
- #metadata ⇒ Object
- #metadata=(metadata_hash) ⇒ Object
- #non_boolean_plan_features ⇒ Object
Instance Method Details
#add_feature(feature, value, display_value = nil) ⇒ Object
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
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_features ⇒ Object
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
16 17 18 |
# File 'app/concerns/stripe_saas/plan.rb', line 16 def free? price_cents.zero? end |
#has_feature?(feature) ⇒ 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
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
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 |
#metadata ⇒ Object
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 () self. = .to_json end |
#non_boolean_plan_features ⇒ Object
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 |