Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion app/cells/folio/console/publishable_inputs/item/show.slim
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ div class=class_name data=data
input_html: input_html('input', placeholder: t('.to')),
atom_setting: "#{field}_until"


- if field == :published
== cell('folio/console/ui/warning_ribbon',
t('.unpublished'),
Expand Down
2 changes: 1 addition & 1 deletion app/cells/folio/devise/invitations/new/show.slim
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.f-devise-invitations-new[
data=data
class=(model[:author_registration] ? "f-devise-invitations-new--author-registration" : nil)
class=author_registration_css_class
]
= form do |f|
= title model[:title]
Expand Down
4 changes: 4 additions & 0 deletions app/cells/folio/devise/invitations/new_cell.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,8 @@ def terms_agreement_label
t(".terms_agreement", terms_link:, privacy_link:)
end
end

def author_registration_css_class
model[:author_registration] ? "f-devise-invitations-new--author-registration" : nil
end
end
17 changes: 17 additions & 0 deletions app/cells/folio/newsletter_subscriptions/form/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ $(document).on('submit', '.f-newsletter-subscriptions-form__form', function (e)
const $response = $(response)
$wrap.replaceWith($response)

// Re-render reCAPTCHA if present
const $recaptcha = $response.find('.g-recaptcha')
if (typeof grecaptcha !== 'undefined' && $recaptcha.length) {
grecaptcha.render($recaptcha[0])
}

$response.trigger('folio:submitted')

if ($response.find('.f-newsletter-subscriptions-form__message').length) {
Expand All @@ -23,3 +29,14 @@ $(document).on('submit', '.f-newsletter-subscriptions-form__form', function (e)
}
})
})

// Initialize reCAPTCHA on page load
$(document).on('turbolinks:load turbo:load', function () {
if (typeof grecaptcha !== 'undefined' && grecaptcha.render) {
$('.f-newsletter-subscriptions-form__recaptcha .g-recaptcha').each(function () {
if ($(this).html() === '') {
grecaptcha.render(this)
}
})
}
})
3 changes: 3 additions & 0 deletions app/cells/folio/newsletter_subscriptions/form/form.sass
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
flex-direction: column
z-index: 2

&__recaptcha
margin-bottom: $spacer

&__form
position: relative
z-index: 1
Expand Down
6 changes: 6 additions & 0 deletions app/cells/folio/newsletter_subscriptions/form/show.slim
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ div class=wrap_class
.position-relative
= input(f)

- if ENV['RECAPTCHA_SITE_KEY'].present? && \
ENV['RECAPTCHA_SECRET_KEY'].present?
.f-newsletter-subscriptions-form__recaptcha.form-group
= recaptcha_tags(script: false)
= f.error :verified_captcha, class: 'invalid-feedback d-block'

button.f-newsletter-subscriptions-form__btn[
type="submit"
class=button_class
Expand Down
1 change: 1 addition & 0 deletions app/cells/folio/newsletter_subscriptions/form_cell.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

class Folio::NewsletterSubscriptions::FormCell < Folio::ApplicationCell
include SimpleForm::ActionViewExtensions::FormHelper
include ::Recaptcha::ClientHelper

def newsletter_subscription
@newsletter_subscription ||= model || Folio::NewsletterSubscription.new(email: "@")
Expand Down
10 changes: 10 additions & 0 deletions app/controllers/folio/newsletter_subscriptions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ def create
attrs = newsletter_subscription_params

@newsletter_subscription = Folio::NewsletterSubscription.new(attrs)
@newsletter_subscription = check_recaptcha_if_needed(@newsletter_subscription)

if !Rails.application.config.folio_site_is_a_singleton
@newsletter_subscription.site = current_site
Expand Down Expand Up @@ -33,4 +34,13 @@ def cell_options_params
{}
end
end

def check_recaptcha_if_needed(newsletter_subscription)
if ENV["RECAPTCHA_SITE_KEY"].present? &&
ENV["RECAPTCHA_SECRET_KEY"].present?
newsletter_subscription.verified_captcha = verify_recaptcha(model: newsletter_subscription)
end

newsletter_subscription
end
end
10 changes: 10 additions & 0 deletions app/models/folio/newsletter_subscription.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
class Folio::NewsletterSubscription < Folio::ApplicationRecord
include Folio::BelongsToSite

attr_accessor :verified_captcha

has_sanitized_fields :email

belongs_to :subscribable, polymorphic: true,
Expand All @@ -16,6 +18,7 @@ class Folio::NewsletterSubscription < Folio::ApplicationRecord
uniqueness: { scope: :site_id }

validate :validate_belongs_to_subscribable_site
validate :validate_verified_captcha

default_scope { order(id: :desc) }

Expand Down Expand Up @@ -93,6 +96,13 @@ def validate_belongs_to_subscribable_site

errors.add(:site, :invalid) unless site.in?(self.class.subscribable_sites)
end

def validate_verified_captcha
return if verified_captcha == true
return if verified_captcha.nil?

errors.add(:verified_captcha, :invalid)
end
end

# == Schema Information
Expand Down
3 changes: 3 additions & 0 deletions config/locales/activerecord.cs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ cs:
active: Aktivní
created_at: Zapsáno dne
email: E-mailová adresa
verified_captcha: Ověření reCAPTCHA
folio/page:
parent_id: Nadřazená stránka
title: Název stránky
Expand Down Expand Up @@ -155,6 +156,8 @@ cs:
attributes:
email:
invalid: není platná
verified_captcha:
invalid: Ověření reCAPTCHA selhalo. Prosím zkuste to znovu.
folio/user:
attributes:
phone:
Expand Down
3 changes: 3 additions & 0 deletions config/locales/activerecord.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ en:
folio/newsletter_subscription:
created_at: Subscribed at
email: E-mail address
verified_captcha: reCAPTCHA verification
folio/page:
parent_id: Parent page
title: Page title
Expand Down Expand Up @@ -115,6 +116,8 @@ en:
attributes:
email:
invalid: Invalid e-mail address
verified_captcha:
invalid: reCAPTCHA verification failed. Please try again.
folio/user:
attributes:
phone:
Expand Down
4 changes: 3 additions & 1 deletion test/cells/folio/leads/form_cell_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class Folio::Leads::FormCellTest < Cell::TestCase

test "shows note from option" do
html = cell("folio/leads/form", nil, note: "foo").(:show)
assert_equal "foo", html.find("textarea").value
# Find textarea that is NOT inside recaptcha div
textarea = html.all("textarea").reject { |t| t[:name] == "g-recaptcha-response" }.first
assert_equal "foo", textarea.value
end
end