Class: Effective::Subscripter

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model
Defined in:
app/models/effective/subscripter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#customerObject

Returns the value of attribute customer.



7
8
9
# File 'app/models/effective/subscripter.rb', line 7

def customer
  @customer
end

#stripe_plan_idObject

Returns the value of attribute stripe_plan_id.



8
9
10
# File 'app/models/effective/subscripter.rb', line 8

def stripe_plan_id
  @stripe_plan_id
end

#stripe_tokenObject

Returns the value of attribute stripe_token.



8
9
10
# File 'app/models/effective/subscripter.rb', line 8

def stripe_token
  @stripe_token
end

#subscribableObject

Returns the value of attribute subscribable.



7
8
9
# File 'app/models/effective/subscripter.rb', line 7

def subscribable
  @subscribable
end

#userObject

Returns the value of attribute user.



7
8
9
# File 'app/models/effective/subscripter.rb', line 7

def user
  @user
end

Instance Method Details

#current_planObject



29
30
31
32
# File 'app/models/effective/subscripter.rb', line 29

def current_plan
  return nil unless subscribable
  subscribable.subscription.blank? ? EffectiveOrders.stripe_plans['trial'] : subscribable.subscription.plan
end

#destroy!Object



62
63
64
65
66
67
68
69
70
71
# File 'app/models/effective/subscripter.rb', line 62

def destroy!
  return true unless subscription && subscription.persisted? && customer.stripe_subscription.present?

  raise 'is invalid' unless valid?

  subscription.destroy!
  customer.subscriptions.reload

  sync! && customer.save!
end

#planObject



34
35
36
# File 'app/models/effective/subscripter.rb', line 34

def plan
  EffectiveOrders.stripe_plans[stripe_plan_id]
end

#reload!Object



73
74
75
76
77
78
# File 'app/models/effective/subscripter.rb', line 73

def reload!
  @stripe_token = nil
  @stripe_plan_id = nil
  @customer = nil
  @subscription = nil
end

#save!Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/models/effective/subscripter.rb', line 42

def save!
  return true if (plan == current_plan) && stripe_token.blank?  # No work to do

  raise 'is invalid' unless valid?

  begin
    build! && sync! && customer.save!
  rescue => e
    reload!

    self.errors.add(:base, e.message)
    raise e
  end
end

#subscribe!(stripe_plan_id) ⇒ Object



57
58
59
60
# File 'app/models/effective/subscripter.rb', line 57

def subscribe!(stripe_plan_id)
  self.stripe_plan_id = stripe_plan_id
  save!
end

#token_required?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'app/models/effective/subscripter.rb', line 38

def token_required?
  customer.token_required?
end