Sha256: ed472fa1e72b87cdc8b9cf19abdc312e7f40ddf90bd0fd60dc9c9cb655876ca1

Contents?: true

Size: 1.31 KB

Versions: 5

Compression:

Stored size: 1.31 KB

Contents

class StripeInvoiceItem < StripeModelCallbacks::ApplicationRecord
  belongs_to :stripe_customer, optional: true, primary_key: "stripe_id"
  belongs_to :stripe_invoice, optional: true, primary_key: "stripe_id"
  belongs_to :stripe_subscription, optional: true, primary_key: "stripe_id"
  belongs_to :stripe_subscription_item, optional: true, primary_key: "stripe_id"
  belongs_to :stripe_plan, optional: true, primary_key: "stripe_id"

  monetize :amount_cents

  def self.stripe_class
    Stripe::InvoiceItem
  end

  def assign_from_stripe(object)
    check_object_is_stripe_class(object, [Stripe::InvoiceItem, Stripe::InvoiceLineItem])
    assign_attributes(
      amount: Money.new(object.amount, object.currency),
      stripe_customer_id: object.try(:customer),
      metadata: JSON.generate(object.metadata),
      period_start: Time.zone.at(object.period.start),
      period_end: Time.zone.at(object.period.end),
      stripe_plan_id: object.plan&.id,
      stripe_subscription_id: object.subscription
    )

    self.stripe_subscription_item_id = object.subscription_item if object.respond_to?(:subscription_item)

    StripeModelCallbacks::AttributesAssignerService.execute!(
      model: self, stripe_model: object,
      attributes: %w[
        currency description discountable id livemode proration quantity
      ]
    )
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
stripe_model_callbacks-0.1.7 lib/stripe_model_callbacks/models/stripe_invoice_item.rb
stripe_model_callbacks-0.1.6 lib/stripe_model_callbacks/models/stripe_invoice_item.rb
stripe_model_callbacks-0.1.5 lib/stripe_model_callbacks/models/stripe_invoice_item.rb
stripe_model_callbacks-0.1.4 lib/stripe_model_callbacks/models/stripe_invoice_item.rb
stripe_model_callbacks-0.1.3 lib/stripe_model_callbacks/models/stripe_invoice_item.rb