Module: ActsAsSubscription::SubscriptionPlan::ClassMethods

Defined in:
lib/acts_as_subscription/subscription_plan.rb

Overview

Class methods for an ActsAsSubscription::SubscriptionPlan instance.

Handles validation of all subscription_plan-related information, such as :

  • code

  • name

  • description

  • price

These class methods should be included in ActiveRecord::Base to provide subscription_plan validation support.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.sync!Object

Synchronizes the local SubscriptionPlan database with the remote backend. This method will try to update existing plans with new data if possible (based on the :code of the plan).

It will NOT however delete any plans locally that have been removed from the reote system. It is the responsibilty of the developer to remove local copies themselves if a plan is removed from the remote subscription backend. For safety it seems more sensible not to automate the removal of plans.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/acts_as_subscription/subscription_plan.rb', line 55

def self.sync!
  plans = ActsAsSubscription::Subscription::Backend.plans
  plans.each do |plan|
    # Try to find an existing plan with this code.
    existing = self.find_by_code(plan[:code])
    
    if existing
      existing.update_attributes(plan)
    else
      existing = self.new(plan)
    end
    
    existing.save
  end
end

Instance Method Details

#acts_as_subscription_planObject

Main entry point - any subclass of ActiveRecord::Base can include the acts_as_subscription_plan method to activate subscription_plan functionality.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/acts_as_subscription/subscription_plan.rb', line 23

def acts_as_subscription_plan
  
  validates :code,
    :presence   => true,
    :uniqueness => {:case_sensitive => false}
  
  validates :name,
    :presence => true
  
  validates :billing_frequency,
    :presence => true
  
  validates :recurring_charge, 
    :presence     => true,
    :format       => { :with => /^\d+??(?:\.\d{0,2})?$/ }, 
    :numericality => {:greater_than_or_equal_to => 0}
  
  attr_accessible :code,
                  :name,
                  :description,
                  :billing_frequency,
                  :recurring_charge,
                  :active
  
  # Synchronizes the local SubscriptionPlan database with the remote backend. This method
  # will try to update existing plans with new data if possible (based on the :code of
  # the plan). 
  #
  # It will *NOT* however delete any plans locally that have been removed from
  # the reote system. It is the responsibilty of the developer to remove local copies
  # themselves if a plan is removed from the remote subscription backend. For safety
  # it seems more sensible not to automate the removal of plans.
  def self.sync!
    plans = ActsAsSubscription::Subscription::Backend.plans
    plans.each do |plan|
      # Try to find an existing plan with this code.
      existing = self.find_by_code(plan[:code])
      
      if existing
        existing.update_attributes(plan)
      else
        existing = self.new(plan)
      end
      
      existing.save
    end
  end
  
  # Returns an array of the current plans, suitable for using with a select 
  # or radio input within a form. This is provided more as an example, and 
  # should be over-ridden in your implementing class if you want to do
  # currency formatting etc.
  def form_options
    plans  = self.find_all_by_active(true)
    result = []
    plans.each do |plan|
      label = plan.name
      if plan.recurring_charge > 0
        label = "#{plan.name} ($#{plan.recurring_charge})"
      end
      result << [label, plan.code]
    end
    
    return result
  end
                  
end

#form_optionsObject

Returns an array of the current plans, suitable for using with a select or radio input within a form. This is provided more as an example, and should be over-ridden in your implementing class if you want to do currency formatting etc.



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/acts_as_subscription/subscription_plan.rb', line 75

def form_options
  plans  = self.find_all_by_active(true)
  result = []
  plans.each do |plan|
    label = plan.name
    if plan.recurring_charge > 0
      label = "#{plan.name} ($#{plan.recurring_charge})"
    end
    result << [label, plan.code]
  end
  
  return result
end