From ffed520aa4b53441248fb2905511ea860bc4eec7 Mon Sep 17 00:00:00 2001 From: Chip Castle Date: Sun, 3 Mar 2024 07:21:13 -0600 Subject: [PATCH] Allow stripe_elements_tag to accept a block --- Changelog.md | 4 ++++ README.md | 17 +++++++++++++++++ app/helpers/stripe/javascript_helper.rb | 6 ++++-- app/views/stripe/_elements.html.erb | 5 +++++ lib/stripe/rails/version.rb | 2 +- test/javascript_helper_spec.rb | 10 ++++++++++ 6 files changed, 41 insertions(+), 3 deletions(-) diff --git a/Changelog.md b/Changelog.md index 27c3744a..7fa90ed4 100644 --- a/Changelog.md +++ b/Changelog.md @@ -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 ! diff --git a/README.md b/README.md index bc5b449e..c6b8cb14 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,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) @@ -322,6 +323,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: diff --git a/app/helpers/stripe/javascript_helper.rb b/app/helpers/stripe/javascript_helper.rb index 32150afa..9b63efd1 100644 --- a/app/helpers/stripe/javascript_helper.rb +++ b/app/helpers/stripe/javascript_helper.rb @@ -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 diff --git a/app/views/stripe/_elements.html.erb b/app/views/stripe/_elements.html.erb index b9ea5631..73f454cd 100644 --- a/app/views/stripe/_elements.html.erb +++ b/app/views/stripe/_elements.html.erb @@ -5,6 +5,11 @@ <%= form_tag submit_path, id: "stripe-form" do %> + <% if local_assigns[:block] %> +
+ <%= capture(&local_assigns[:block]) %> +
+ <% end %> <%= label_tag :card_element, label_text %>
<%= submit_tag submit_button_text %> diff --git a/lib/stripe/rails/version.rb b/lib/stripe/rails/version.rb index c2f21077..c8750a65 100644 --- a/lib/stripe/rails/version.rb +++ b/lib/stripe/rails/version.rb @@ -1,5 +1,5 @@ module Stripe module Rails - VERSION = '2.4.0'.freeze + VERSION = '2.5.0'.freeze end end diff --git a/test/javascript_helper_spec.rb b/test/javascript_helper_spec.rb index 8a321973..d0e3b104 100644 --- a/test/javascript_helper_spec.rb +++ b/test/javascript_helper_spec.rb @@ -77,5 +77,15 @@ end end end + + describe 'with block' do + let(:markup) { ''.html_safe } + + it 'should display block contents' do + block = lambda { markup } + result = view.stripe_elements_tag(submit_path: '/charge', &block) + assert_match %r%%, result + end + end end end