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



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

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
# File 'lib/plan_features/pricing.rb', line 9

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

.find_by_identifier(id) ⇒ Object



15
16
17
18
19
20
# File 'lib/plan_features/pricing.rb', line 15

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



22
23
24
25
26
# File 'lib/plan_features/pricing.rb', line 22

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



41
42
43
# File 'lib/plan_features/pricing.rb', line 41

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

Instance Method Details

#current_and_previous_featuresObject



45
46
47
# File 'lib/plan_features/pricing.rb', line 45

def current_and_previous_features
  features.merge(previous_features)
end

#display_featuresObject



92
93
94
95
96
97
98
99
100
101
# File 'lib/plan_features/pricing.rb', line 92

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

#free?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/plan_features/pricing.rb', line 69

def free?
  prices.nil?
end

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

Returns:

  • (Boolean)


49
50
51
52
53
# File 'lib/plan_features/pricing.rb', line 49

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

#limit_for(feature) ⇒ Object



65
66
67
# File 'lib/plan_features/pricing.rb', line 65

def limit_for(feature)
  features.dig(feature.to_s, "limit")
end

#paid?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/plan_features/pricing.rb', line 73

def paid?
  prices&.any?
end

#previous_featuresObject



55
56
57
# File 'lib/plan_features/pricing.rb', line 55

def previous_features
  previous_plan&.current_and_previous_features || {}
end

#previous_planObject



59
60
61
62
63
# File 'lib/plan_features/pricing.rb', line 59

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

#price(name: :monthly) ⇒ Object



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

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

#pricing_id(name: :monthly) ⇒ Object



77
78
79
80
# File 'lib/plan_features/pricing.rb', line 77

def pricing_id(name: :monthly)
  return nil if prices.nil?
  prices[name.to_s]["product"]
end

#yearly_price_monthlyObject



87
88
89
90
# File 'lib/plan_features/pricing.rb', line 87

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