aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--config/initializers/simple_form/safe_submit.rb7
-rw-r--r--config/locales/actions.en.yml1
-rw-r--r--config/locales/actions.fr.yml1
-rw-r--r--lib/af83/simple_form/safe_submit.rb24
-rw-r--r--spec/features/safe_submit_spec.rb9
5 files changed, 42 insertions, 0 deletions
diff --git a/config/initializers/simple_form/safe_submit.rb b/config/initializers/simple_form/safe_submit.rb
new file mode 100644
index 000000000..8d10929eb
--- /dev/null
+++ b/config/initializers/simple_form/safe_submit.rb
@@ -0,0 +1,7 @@
+AF83::SimpleForm::SafeSubmit.decorate_simple_form
+
+if Rails.env.development?
+ ActionDispatch::Reloader.to_prepare do
+ AF83::SimpleForm::SafeSubmit.decorate_simple_form
+ end
+end
diff --git a/config/locales/actions.en.yml b/config/locales/actions.en.yml
index 08d505393..278915526 100644
--- a/config/locales/actions.en.yml
+++ b/config/locales/actions.en.yml
@@ -25,6 +25,7 @@ en:
erase: 'Erase'
create_api_key: "Create an API key"
select: Select
+ wait_for_submission: "Please wait..."
or: "or"
cancel: "Cancel"
back: "Go Back"
diff --git a/config/locales/actions.fr.yml b/config/locales/actions.fr.yml
index 4690c995a..92e16f21e 100644
--- a/config/locales/actions.fr.yml
+++ b/config/locales/actions.fr.yml
@@ -25,6 +25,7 @@ fr:
erase: 'Effacer'
create_api_key: "Créer une clé d'API"
select: Sélectionner
+ wait_for_submission: "Validation..."
or: "ou"
cancel: "Annuler"
back: "Retour"
diff --git a/lib/af83/simple_form/safe_submit.rb b/lib/af83/simple_form/safe_submit.rb
new file mode 100644
index 000000000..d4d84f90d
--- /dev/null
+++ b/lib/af83/simple_form/safe_submit.rb
@@ -0,0 +1,24 @@
+module AF83
+ module SimpleForm
+ module SafeSubmit
+ def self.decorate_simple_form
+ ::SimpleForm::FormBuilder.class_eval do
+ def button(type, *args, &block)
+ options = args.extract_options!.dup
+ options[:class] = [::SimpleForm.button_class, options[:class]].compact
+ if type == :submit
+ options[:data] ||= {}
+ options[:data][:disable_with] ||= I18n.t('actions.wait_for_submission')
+ end
+ args << options
+ if respond_to?(:"#{type}_button")
+ send(:"#{type}_button", *args, &block)
+ else
+ send(type, *args, &block)
+ end
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/spec/features/safe_submit_spec.rb b/spec/features/safe_submit_spec.rb
new file mode 100644
index 000000000..9968d4310
--- /dev/null
+++ b/spec/features/safe_submit_spec.rb
@@ -0,0 +1,9 @@
+RSpec.describe 'SafeSubmit', type: :feature do
+ login_user
+
+ let( :path ){ new_api_key_path() }
+ it 'view shows the corresponding buttons' do
+ visit path
+ expect(page).to have_css('input[type=submit][data-disable-with]')
+ end
+end