Skip to content

Commit 70ef420

Browse files
authored
Merge pull request #228 from chip/master
Allow stripe_elements_tag to accept a block
2 parents 1c3ac3a + 219504d commit 70ef420

File tree

6 files changed

+41
-3
lines changed

6 files changed

+41
-3
lines changed

Changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 2.5.0 (2023-03-21)
2+
3+
- Allow `stripe_elements_tag` to accept a block. Thanks @chip !
4+
15
## 2.4.0 (2023-02-04)
26

37
- Add `tax_behavior` attribute to Price. Thanks @szechyjs !

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ This gem can help your rails application integrate with Stripe in the following
2424
- [Configuring your plans and coupons](#configuring-your-plans-and-coupons)
2525

2626
[Stripe Elements](#stripe-elements)
27+
- [Custom Elements](#custom-elements)
2728

2829
[Webhooks](#webhooks)
2930

@@ -320,6 +321,22 @@ Simply include the `stripe_elements_tag` anywhere below the `stripe_javascript_t
320321
<%= stripe_elements_tag submit_path: billing_path %>
321322
```
322323

324+
Additionally, you can pass a block containing custom form elements to stripe_elements_tag:
325+
326+
## Custom Elements
327+
328+
> Stripe::Rails allows you to easily include your own custom form elements
329+
> within the Stripe form by including those form elements in a block passed to
330+
> `stripe_elements_tag`:
331+
332+
```erb
333+
<%= stripe_javascript_tag %>
334+
<%= stripe_elements_tag(submit_path: billing_path) do %>
335+
<%= label_tag 'email', 'Email' %>
336+
<%= text_field :user, :email %>
337+
<% end %>
338+
```
339+
323340
### Configuration options
324341

325342
Stripe::Rails comes bundled with default CSS and Javascript for Stripe elements, making it easy to drop in to your app. You can also specify your own assets paths:

app/helpers/stripe/javascript_helper.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,16 @@ def stripe_javascript_tag(stripe_js_version = DEFAULT_STRIPE_JS_VERSION)
1010

1111
def stripe_elements_tag(submit_path:,
1212
css_path: asset_path("stripe_elements.css"),
13-
js_path: asset_path("stripe_elements.js"))
13+
js_path: asset_path("stripe_elements.js"),
14+
&block)
1415

1516
render partial: 'stripe/elements', locals: {
1617
submit_path: submit_path,
1718
label_text: t('stripe_rails.elements.label_text'),
1819
submit_button_text: t('stripe_rails.elements.submit_button_text'),
1920
css_path: css_path,
20-
js_path: js_path
21+
js_path: js_path,
22+
block: block
2123
}
2224
end
2325
end

app/views/stripe/_elements.html.erb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
</div>
66

77
<%= form_tag submit_path, id: "stripe-form" do %>
8+
<% if local_assigns[:block] %>
9+
<div id="stripe-rails-form-fields">
10+
<%= capture(&local_assigns[:block]) %>
11+
</div>
12+
<% end %>
813
<%= label_tag :card_element, label_text %>
914
<div id="card-element"><!-- A Stripe Element will be inserted here. --></div>
1015
<%= submit_tag submit_button_text %>

lib/stripe/rails/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module Stripe
22
module Rails
3-
VERSION = '2.4.0'.freeze
3+
VERSION = '2.5.0'.freeze
44
end
55
end

test/javascript_helper_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,5 +77,15 @@
7777
end
7878
end
7979
end
80+
81+
describe 'with block' do
82+
let(:markup) { '<input type="text" />'.html_safe }
83+
84+
it 'should display block contents' do
85+
block = lambda { markup }
86+
result = view.stripe_elements_tag(submit_path: '/charge', &block)
87+
assert_match %r%<input type="text" />%, result
88+
end
89+
end
8090
end
8191
end

0 commit comments

Comments
 (0)