Class: PlanFeatures::Pricing

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model
Defined in:
lib/plan_features/pricing.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#featuresObject

Returns the value of attribute features.



5
6
7
# File 'lib/plan_features/pricing.rb', line 5

def features
  @features
end

#intervalObject

Returns the value of attribute interval.



5
6
7
# File 'lib/plan_features/pricing.rb', line 5

def interval
  @interval
end

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/plan_features/pricing.rb', line 5

def name
  @name
end

#plan_identifierObject

Returns the value of attribute plan_identifier.



5
6
7
# File 'lib/plan_features/pricing.rb', line 5

def plan_identifier
  @plan_identifier
end

Returns the value of attribute popular.



5
6
7
# File 'lib/plan_features/pricing.rb', line 5

def popular
  @popular
end

#previousObject

Returns the value of attribute previous.



5
6
7
# File 'lib/plan_features/pricing.rb', line 5

def previous
  @previous
end

#pricesObject

Returns the value of attribute prices.



5
6
7
# File 'lib/plan_features/pricing.rb', line 5

def prices
  @prices
end

#stripe_idObject

Returns the value of attribute stripe_id.



5
6
7
# File 'lib/plan_features/pricing.rb', line 5

def stripe_id
  @stripe_id
end

Class Method Details

.all_plansObject



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/plan_features/pricing.rb', line 30

def self.all_plans
  @all_plans ||= YAML.load_file(::PlanFeatures.configuration.plans_file_path).map do |plan, attributes|
    Pricing.new(
      plan_identifier: plan,
      popular: attributes["popular"],
      name: attributes["name"],
      features: attributes["features"],
      prices: attributes["prices"],
      previous: attributes["previous"]
    )
  end
end

.boostsObject



9
10
11
12
13
14
15
# File 'lib/plan_features/pricing.rb', line 9

def self.boosts
 @boosts ||= begin
  if File.exist?(Rails.root.join("config", "account_boosts.yml"))
    YAML.load_file(Rails.root.join("config", "account_boosts.yml"))
  end
 end
end

.find_by_identifier(id) ⇒ Object



17
18
19
20
21
22
# File 'lib/plan_features/pricing.rb', line 17

def self.find_by_identifier(id)
  plan = Pricing.all_plans.find { |plan| plan.plan_identifier == id.to_s }
  raise "Plan not found" if plan.nil?

  plan
end

.find_by_pricing_id(id, interval: "monthly") ⇒ Object



24
25
26
27
28
# File 'lib/plan_features/pricing.rb', line 24

def self.find_by_pricing_id(id, interval: "monthly")
  Pricing.all_plans.find do |plan|
    !plan.free? && plan.prices && plan.prices[interval]["product"] == id
  end
end

.free_planObject



43
44
45
# File 'lib/plan_features/pricing.rb', line 43

def self.free_plan
  Pricing.all_plans.find { |plan| plan.free? }
end

Instance Method Details

#amount_for(feature) ⇒ Object



67
68
69
# File 'lib/plan_features/pricing.rb', line 67

def amount_for(feature)
  features.dig(feature.to_s, "amount")
end

#current_and_previous_featuresObject



47
48
49
# File 'lib/plan_features/pricing.rb', line 47

def current_and_previous_features
  features.merge(previous_features)
end

#display_featuresObject



110
111
112
113
114
115
116
117
118
# File 'lib/plan_features/pricing.rb', line 110

def display_features
  features.map do |identifier, attributes|
    PricingFeatures::Feature.new(
      description: attributes["description"],
      amount: attributes["amount"],
      hidden: attributes["hidden"],
    )
  end.compact.filter{|f| !f.hidden? }
end

#free?Boolean



71
72
73
# File 'lib/plan_features/pricing.rb', line 71

def free?
  prices.nil?
end

#has_feature?(feature, id: nil) ⇒ Boolean



51
52
53
54
55
# File 'lib/plan_features/pricing.rb', line 51

def has_feature?(feature, id: nil)
  return true if Pricing.boosts&.[](id)&.include?(feature.to_s)

  current_and_previous_features.find { |f| f[0] == feature.to_s }.present?
end

#monthly_pricing_idObject



92
93
94
# File 'lib/plan_features/pricing.rb', line 92

def monthly_pricing_id
  pricing_id(name: :monthly)
end

#paid?Boolean



75
76
77
# File 'lib/plan_features/pricing.rb', line 75

def paid?
  prices&.any?
end

#previous_featuresObject



57
58
59
# File 'lib/plan_features/pricing.rb', line 57

def previous_features
  previous_plan&.current_and_previous_features || {}
end

#previous_planObject



61
62
63
64
65
# File 'lib/plan_features/pricing.rb', line 61

def previous_plan
  Pricing.all_plans.find do |plan|
    plan.plan_identifier == previous
  end
end

#price(name: :monthly) ⇒ Object



100
101
102
103
# File 'lib/plan_features/pricing.rb', line 100

def price(name: :monthly)
  return nil if prices.nil?
  prices[name.to_s]["amount"] / 100
end

#pricing_id(name: :monthly) ⇒ Object



79
80
81
82
83
84
85
# File 'lib/plan_features/pricing.rb', line 79

def pricing_id(interval = "monthly")
  if interval == "monthly"
    monthly_pricing_id
  else
    yearly_pricing_id
  end
end

#yearly_price_monthlyObject



105
106
107
108
# File 'lib/plan_features/pricing.rb', line 105

def yearly_price_monthly
  return nil if prices.nil?
  (prices["yearly"]["amount"].to_f / 100 / 12).round(2)
end

#yearly_pricing_idObject



96
97
98
# File 'lib/plan_features/pricing.rb', line 96

def yearly_pricing_id
  pricing_id(name: :yearly)
end