Module: StripeSaas::Subscription

Extended by:
ActiveSupport::Concern
Defined in:
app/concerns/stripe_saas/subscription.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.find_customer(subscription_or_owner) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'app/concerns/stripe_saas/subscription.rb', line 120

def self.find_customer(subscription_or_owner)
  if subscription_or_owner.class.to_s.downcase.to_sym == StripeSaas.subscriptions_owned_by
    owner = subscription_or_owner
  else
    owner = subscription_or_owner.send StripeSaas.subscriptions_owned_by
  end
  # Return whatever we belong to.
  # If this object doesn't respond to 'name', please update owner_description.
  if StripeSaas.customer_accessor
    if StripeSaas.customer_accessor.kind_of?(Array)
      StripeSaas.customer_accessor.inject(subscription_or_owner) {|o, a| o.send(a); o }
    else
      owner.send StripeSaas.customer_accessor
    end
  else
    owner
  end
end

Instance Method Details

#changing_plans?Boolean

Returns:

  • (Boolean)


154
155
156
# File 'app/concerns/stripe_saas/subscription.rb', line 154

def changing_plans?
  plan_id_changed?
end

#coupon_code=(new_code) ⇒ Object

Set a Stripe coupon code that will be used when a new Stripe customer (a.k.a. StripeSaas subscription) is created



112
113
114
# File 'app/concerns/stripe_saas/subscription.rb', line 112

def coupon_code=(new_code)
  @coupon_code = new_code
end

#describe_difference(plan_to_describe) ⇒ Object

TODO: this does not belong in here - need a presenter



167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'app/concerns/stripe_saas/subscription.rb', line 167

def describe_difference(plan_to_describe)
  if plan.nil?
    if persisted?
      "Upgrade"
    end
  else
    if plan_to_describe.is_upgrade_from?(plan)
      "Upgrade"
    else
      "Downgrade"
    end
  end
end

#downgrading?Boolean

Returns:

  • (Boolean)


158
159
160
# File 'app/concerns/stripe_saas/subscription.rb', line 158

def downgrading?
  plan.present? and plan_id_was.present? and plan_id_was > self.plan_id
end

#subscription_ownerObject



116
117
118
# File 'app/concerns/stripe_saas/subscription.rb', line 116

def subscription_owner
  StripeSaas::Subscription.find_customer(self)
end

#subscription_owner=(owner) ⇒ Object



139
140
141
142
# File 'app/concerns/stripe_saas/subscription.rb', line 139

def subscription_owner=(owner)
  # e.g. @subscription.user = @owner
  send StripeSaas.owner_assignment_sym, owner
end

#subscription_owner_descriptionObject



144
145
146
147
148
# File 'app/concerns/stripe_saas/subscription.rb', line 144

def subscription_owner_description
  # assuming owner responds to name.
  # we should check for whether it responds to this or not.
  "#{subscription_owner.try(:name) || subscription_owner.try(:id) || subscription_owner.try(:email)}"
end

#subscription_owner_emailObject



150
151
152
# File 'app/concerns/stripe_saas/subscription.rb', line 150

def subscription_owner_email
  "#{subscription_owner.try(:email)}"
end

#upgrading?Boolean

Returns:

  • (Boolean)


162
163
164
# File 'app/concerns/stripe_saas/subscription.rb', line 162

def upgrading?
  (plan_id_was.present? and plan_id_was < plan_id) or plan_id_was.nil?
end