Skip to content

Commit

Permalink
Merge pull request #228 from chip/master
Browse files Browse the repository at this point in the history
Allow stripe_elements_tag to accept a block
  • Loading branch information
tansengming authored Mar 22, 2024
2 parents 1c3ac3a + 219504d commit 70ef420
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 3 deletions.
4 changes: 4 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.5.0 (2023-03-21)

- Allow `stripe_elements_tag` to accept a block. Thanks @chip !

## 2.4.0 (2023-02-04)

- Add `tax_behavior` attribute to Price. Thanks @szechyjs !
Expand Down
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ This gem can help your rails application integrate with Stripe in the following
- [Configuring your plans and coupons](#configuring-your-plans-and-coupons)

[Stripe Elements](#stripe-elements)
- [Custom Elements](#custom-elements)

[Webhooks](#webhooks)

Expand Down Expand Up @@ -320,6 +321,22 @@ Simply include the `stripe_elements_tag` anywhere below the `stripe_javascript_t
<%= stripe_elements_tag submit_path: billing_path %>
```

Additionally, you can pass a block containing custom form elements to stripe_elements_tag:

## Custom Elements

> Stripe::Rails allows you to easily include your own custom form elements
> within the Stripe form by including those form elements in a block passed to
> `stripe_elements_tag`:

```erb
<%= stripe_javascript_tag %>
<%= stripe_elements_tag(submit_path: billing_path) do %>
<%= label_tag 'email', 'Email' %>
<%= text_field :user, :email %>
<% end %>
```

### Configuration options

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:
Expand Down
6 changes: 4 additions & 2 deletions app/helpers/stripe/javascript_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ def stripe_javascript_tag(stripe_js_version = DEFAULT_STRIPE_JS_VERSION)

def stripe_elements_tag(submit_path:,
css_path: asset_path("stripe_elements.css"),
js_path: asset_path("stripe_elements.js"))
js_path: asset_path("stripe_elements.js"),
&block)

render partial: 'stripe/elements', locals: {
submit_path: submit_path,
label_text: t('stripe_rails.elements.label_text'),
submit_button_text: t('stripe_rails.elements.submit_button_text'),
css_path: css_path,
js_path: js_path
js_path: js_path,
block: block
}
end
end
Expand Down
5 changes: 5 additions & 0 deletions app/views/stripe/_elements.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
</div>

<%= form_tag submit_path, id: "stripe-form" do %>
<% if local_assigns[:block] %>
<div id="stripe-rails-form-fields">
<%= capture(&local_assigns[:block]) %>
</div>
<% end %>
<%= label_tag :card_element, label_text %>
<div id="card-element"><!-- A Stripe Element will be inserted here. --></div>
<%= submit_tag submit_button_text %>
Expand Down
2 changes: 1 addition & 1 deletion lib/stripe/rails/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Stripe
module Rails
VERSION = '2.4.0'.freeze
VERSION = '2.5.0'.freeze
end
end
10 changes: 10 additions & 0 deletions test/javascript_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,15 @@
end
end
end

describe 'with block' do
let(:markup) { '<input type="text" />'.html_safe }

it 'should display block contents' do
block = lambda { markup }
result = view.stripe_elements_tag(submit_path: '/charge', &block)
assert_match %r%<input type="text" />%, result
end
end
end
end

0 comments on commit 70ef420

Please sign in to comment.