Class: StripeModelCallbacks::ApplicationRecord

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
PublicActivity::Model
Defined in:
app/models/stripe_model_callbacks/application_record.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#stripe_object=(value) ⇒ Object (writeonly)

Sets the attribute stripe_object

Parameters:

  • value

    the value to set the attribute stripe_object to.



7
8
9
# File 'app/models/stripe_model_callbacks/application_record.rb', line 7

def stripe_object=(value)
  @stripe_object = value
end

Class Method Details

.check_object_is_stripe_class(object, allowed = nil) ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'app/models/stripe_model_callbacks/application_record.rb', line 9

def self.check_object_is_stripe_class(object, allowed = nil)
  raise "'stripe_class' not defined on #{name}" unless respond_to?(:stripe_class)

  # Ignore general objects
  return if object.class.name == "Stripe::StripeObject" # rubocop:disable Style/ClassEqualityComparison:

  allowed ||= [stripe_class]

  raise "Expected #{object.class.name} to be a #{allowed.map(&:name).join(", ")}" unless allowed.any? { |stripe_class| object.is_a?(stripe_class) }
end

.create_from_stripe!(object) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'app/models/stripe_model_callbacks/application_record.rb', line 24

def self.create_from_stripe!(object)
  check_object_is_stripe_class(object)

  model = new
  model.stripe_object = object
  model.assign_from_stripe(object)
  model.save!
  model
end

.create_on_stripe!(attributes) ⇒ Object



34
35
36
37
# File 'app/models/stripe_model_callbacks/application_record.rb', line 34

def self.create_on_stripe!(attributes)
  object = stripe_class.create(attributes)
  create_from_stripe!(object)
end

Instance Method Details

#check_object_is_stripe_class(object, allowed = nil) ⇒ Object



20
21
22
# File 'app/models/stripe_model_callbacks/application_record.rb', line 20

def check_object_is_stripe_class(object, allowed = nil)
  self.class.check_object_is_stripe_class(object, allowed)
end

#destroy_on_stripeObject



67
68
69
70
71
72
# File 'app/models/stripe_model_callbacks/application_record.rb', line 67

def destroy_on_stripe
  to_stripe.delete
  update!(deleted_at: Time.zone.now) if respond_to?(:deleted_at)
  reload_from_stripe!
  true
end

#destroy_on_stripe!Object

Raises:

  • (ActiveRecord::RecordInvalid)


74
75
76
# File 'app/models/stripe_model_callbacks/application_record.rb', line 74

def destroy_on_stripe!
  raise ActiveRecord::RecordInvalid, self unless destroy_on_stripe
end

#reload!(*args, &blk) ⇒ Object



48
49
50
51
# File 'app/models/stripe_model_callbacks/application_record.rb', line 48

def reload!(*args, &blk)
  @to_stripe = nil
  super
end

#reload_from_stripe!Object



43
44
45
46
# File 'app/models/stripe_model_callbacks/application_record.rb', line 43

def reload_from_stripe!
  assign_from_stripe(to_stripe)
  save!
end

#to_stripeObject



39
40
41
# File 'app/models/stripe_model_callbacks/application_record.rb', line 39

def to_stripe
  @to_stripe ||= self.class.stripe_class.retrieve(stripe_id)
end

#update_on_stripe(attributes) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'app/models/stripe_model_callbacks/application_record.rb', line 53

def update_on_stripe(attributes)
  attributes.each do |key, value|
    to_stripe.__send__("#{key}=", value)
  end

  to_stripe.save
  reload_from_stripe!
  true
end

#update_on_stripe!(attributes) ⇒ Object

Raises:

  • (ActiveRecord::RecordInvalid)


63
64
65
# File 'app/models/stripe_model_callbacks/application_record.rb', line 63

def update_on_stripe!(attributes)
  raise ActiveRecord::RecordInvalid, self unless update_on_stripe(attributes)
end