Class: PurchaseKit::PaywallBuilder

Inherits:
Object
  • Object
show all
Defined in:
app/helpers/purchase_kit/paywall_helper.rb

Instance Method Summary collapse

Constructor Details

#initialize(template) ⇒ PaywallBuilder

Returns a new instance of PaywallBuilder.



40
41
42
43
# File 'app/helpers/purchase_kit/paywall_helper.rb', line 40

def initialize(template)
  @template = template
  @current_product = nil
end

Instance Method Details

#plan_option(product:, selected: false, input_class: nil, **options, &block) ⇒ Object



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
# File 'app/helpers/purchase_kit/paywall_helper.rb', line 45

def plan_option(product:, selected: false, input_class: nil, **options, &block)
  input_id = "purchasekit_plan_#{product.id.to_s.parameterize.underscore}"

  radio = @template.radio_button_tag(
    :product_id,
    product.id,
    selected,
    id: input_id,
    class: input_class,
    required: true,
    autocomplete: "off",
    data: {
      purchasekit__paywall_target: "planRadio",
      apple_store_product_id: product.apple_product_id,
      google_store_product_id: product.google_product_id,
      google_store_base_plan_id: product.google_base_plan_id
    }
  )

  @current_product = product
  label = @template.label_tag(input_id, **options) { @template.capture(&block) }
  @current_product = nil

  radio + label
end

#price(**options, &block) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'app/helpers/purchase_kit/paywall_helper.rb', line 71

def price(**options, &block)
  raise "price must be called within a plan_option block" unless @current_product

  data = (options.delete(:data) || {}).merge(
    purchasekit__paywall_target: "price",
    apple_store_product_id: @current_product.apple_product_id,
    google_store_product_id: @current_product.google_product_id,
    google_store_base_plan_id: @current_product.google_base_plan_id
  )

  loading_content = block ? @template.capture(&block) : "Loading..."
  @template.(:span, loading_content, data: data, **options)
end

#restore(text = "Restore purchases", url: nil, **options) ⇒ Object



94
95
96
97
98
99
100
101
102
# File 'app/helpers/purchase_kit/paywall_helper.rb', line 94

def restore(text = "Restore purchases", url: nil, **options)
  data = (options.delete(:data) || {}).merge(
    purchasekit__paywall_target: "restoreButton",
    action: "purchasekit--paywall#restore"
  )
  data[:restore_url] = url if url

  @template.(:button, text, type: "button", data: data, **options)
end

#submit(text = "Subscribe", **options) ⇒ Object



85
86
87
88
89
90
91
92
# File 'app/helpers/purchase_kit/paywall_helper.rb', line 85

def submit(text = "Subscribe", **options)
  data = (options.delete(:data) || {}).merge(
    purchasekit__paywall_target: "submitButton",
    turbo_submits_with: text
  )

  @template.submit_tag(text, disabled: true, data: data, **options)
end