From 0d18e35c7904a0f4a5f91d4c3f0f0d541a5420af Mon Sep 17 00:00:00 2001 From: michal-rohacek Date: Thu, 8 Jul 2021 08:49:47 +0200 Subject: [PATCH 01/15] Add and configure recaptcha gem --- .env | 4 ++++ Gemfile | 1 + Gemfile.lock | 3 +++ config/initializers/recaptcha.rb | 4 ++++ 4 files changed, 12 insertions(+) create mode 100644 config/initializers/recaptcha.rb diff --git a/.env b/.env index a154b86c..e7345e89 100644 --- a/.env +++ b/.env @@ -7,3 +7,7 @@ GTM_ID= ROLLBAR_ACCESS_TOKEN= NEWRELIC_LICENSE_KEY= SECRET_KEY_BASE= +RECAPTCHA_SITE_KEY_V3= +RECAPTCHA_SECRET_KEY_V3= +RECAPTCHA_SITE_KEY_V2_CHECKBOX= +RECAPTCHA_SECRET_KEY_V2_CHECKBOX= diff --git a/Gemfile b/Gemfile index bff155af..193c184f 100644 --- a/Gemfile +++ b/Gemfile @@ -23,6 +23,7 @@ gem 'rollbar' # Utilities gem 'redis-rails' gem 'rest-client' +gem 'recaptcha' group :development, :test do gem 'dotenv-rails' diff --git a/Gemfile.lock b/Gemfile.lock index 05bea4e9..c3903950 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -172,6 +172,8 @@ GEM rb-fsevent (0.10.4) rb-inotify (0.10.1) ffi (~> 1.0) + recaptcha (5.5.0) + json redis (4.2.2) redis-actionpack (5.2.0) actionpack (>= 5, < 7) @@ -293,6 +295,7 @@ DEPENDENCIES rack-mini-profiler rails (~> 5.2.4.4) rails_12factor + recaptcha redis-rails rest-client rollbar diff --git a/config/initializers/recaptcha.rb b/config/initializers/recaptcha.rb new file mode 100644 index 00000000..2200110f --- /dev/null +++ b/config/initializers/recaptcha.rb @@ -0,0 +1,4 @@ +Recaptcha.configure do |config| + config.site_key = ENV.fetch('RECAPTCHA_SITE_KEY_V3') + config.secret_key = ENV.fetch('RECAPTCHA_SECRET_KEY_V3') +end \ No newline at end of file From 7726cc8afc63967f7cca272787a51602efee5eb3 Mon Sep 17 00:00:00 2001 From: michal-rohacek Date: Thu, 8 Jul 2021 08:51:05 +0200 Subject: [PATCH 02/15] Add registration resource --- app/controllers/registrations_controller.rb | 39 ++++++++++++++ .../services/datahub_controller.rb | 2 + app/helpers/registrations_helper.rb | 25 +++++++++ app/models/registration.rb | 51 +++++++++++++++++++ app/views/registrations/_form.html.erb | 37 ++++++++++++++ app/views/services/autoform/index.html.erb | 47 +++++++++-------- config/routes.rb | 2 + 7 files changed, 181 insertions(+), 22 deletions(-) create mode 100644 app/controllers/registrations_controller.rb create mode 100644 app/helpers/registrations_helper.rb create mode 100644 app/models/registration.rb create mode 100644 app/views/registrations/_form.html.erb diff --git a/app/controllers/registrations_controller.rb b/app/controllers/registrations_controller.rb new file mode 100644 index 00000000..cc43c651 --- /dev/null +++ b/app/controllers/registrations_controller.rb @@ -0,0 +1,39 @@ +class RegistrationsController < ApplicationController + include RegistrationsHelper + + def create + render :create and return unless registration.valid?(:submit) + + if recaptcha_valid? + registration.finish and render :success + else + registration.checkbox_captcha! and render :create + end + end + + private + + def recaptcha_valid? + if registration.checkbox_captcha + verify_checkbox_captcha + else + verify_invisible_captcha + end + end + + def verify_checkbox_captcha + verify_recaptcha(message: 'Skúste to ešte raz.', secret_key: ENV.fetch('RECAPTCHA_SECRET_KEY_V2_CHECKBOX'), model: registration) + end + + def verify_invisible_captcha + verify_recaptcha(message: 'Potvrďte, prosím, že nie ste robot.', minimum_score: 0.5, action: recaptcha_action, model: registration) + end + + def registration_params + params.require(:registration).permit(:service, :email, :checkbox_captcha) + end + + def registration + @registration ||= Registration.new(registration_params) + end +end diff --git a/app/controllers/services/datahub_controller.rb b/app/controllers/services/datahub_controller.rb index 3e69e197..dc65eb0e 100644 --- a/app/controllers/services/datahub_controller.rb +++ b/app/controllers/services/datahub_controller.rb @@ -1,4 +1,6 @@ class Services::DatahubController < ContentController + include RegistrationsHelper + def index @page.title = 'Vyčistené, štruktúrované dáta nielen o firmách. · Datahub'.html_safe @page.og.image = view_context.image_url('fb-datahub.png') diff --git a/app/helpers/registrations_helper.rb b/app/helpers/registrations_helper.rb new file mode 100644 index 00000000..9f826e41 --- /dev/null +++ b/app/helpers/registrations_helper.rb @@ -0,0 +1,25 @@ +module RegistrationsHelper + def registration_form_id(service = controller_name) + "#{service}_registration_form" + end + + def recaptcha_action + "#{controller_name}_registration" + end + + def controller_specific_register_path + polymorphic_path([:services, controller_name, :index]) + end + + def render_registration_form(service: controller_name, registration: nil) + registration ||= Registration.new(service: service) + + if registration.valid?(:render) + render partial: 'registrations/form', object: registration + end + end + + def translate_field(field_name) + translate "registrations.#{field_name}", default: '' + end +end diff --git a/app/models/registration.rb b/app/models/registration.rb new file mode 100644 index 00000000..0edba46a --- /dev/null +++ b/app/models/registration.rb @@ -0,0 +1,51 @@ +class Registration + include ActiveModel::Model + include ActiveModel::Attributes + + SUBMIT_MAPPINGS = HashWithIndifferentAccess.new( + autoform: { + url: 'https://docs.google.com/forms/d/1TpYNJfBQVGt4lmKP-wrXGkok2bo-Y6mzpmeUsJTnRis/formResponse', + email: 'entry.204431983', + other: { domain: 'entry.1349114640' } + }, + slovensko_sk_api: { + url: 'https://docs.google.com/forms/d/e/1FAIpQLSfUuAjnqGjDvSc-Miy6bP0xODXsjr6g04hGAeYlYkJo-3Iu1Q/formResponse', + email: 'emailAddress', + }, + datahub: { + url: 'https://docs.google.com/forms/d/e/1FAIpQLSdgW4Hf2fEhX3cpTkoYJTaIVs8pWrTFrItt9Hj_9ZD36yPLZQ/formResponse', + email: 'entry.1902802364', + } + ) + + attr_accessor :email, :service, :other_fields + attribute :checkbox_captcha, :boolean, default: false + + validates :service, inclusion: SUBMIT_MAPPINGS.keys, on: :render + validates :email, format: { with: URI::MailTo::EMAIL_REGEXP, message: 'Zadajte email v správnom tvare' }, on: :submit + # validates :other_fields, each: { presence: true }, on: :submit + + def mappings + data[:other].reverse_merge(email: data[:email]) + end + + def finish + args = attributes.transform_keys { |key| mappings[key] }.symbolize_keys! + + RestClient.post(data[:url], **args) + end + + private + + def checkbox_captcha! + checkbox_captcha = true + end + + def data + SUBMIT_MAPPINGS[service] + end + + def attributes + other_fields.reverse_merge(email: email) + end +end diff --git a/app/views/registrations/_form.html.erb b/app/views/registrations/_form.html.erb new file mode 100644 index 00000000..8664709c --- /dev/null +++ b/app/views/registrations/_form.html.erb @@ -0,0 +1,37 @@ +
+
+ <%= form_with model: form do |f| %> + <% if form.errors.any? %> + <% form.errors.values.flatten.each do |message| %> + <%= render 'services/share/flash_message', { type: :alert, message: message } %> + <% end %> + <% end %> + + <% if form.checkbox_captcha %> + <%= recaptcha_tags(site_key: ENV.fetch('RECAPTCHA_SITE_KEY_V2_CHECKBOX')) %> + <% else %> + <%= recaptcha_v3(action: recaptcha_action) %> + <% end %> + +
+ <% form.mappings.keys.each do |field| %> +
+ <%= f.label field, translate_field(field), class: 'control-label' %> +
+ <%= f.text_field field, class: 'form-control input-lg' %> +
+
+ <% end %> + + <%= f.hidden_field :service, value: form.service %> + <%= f.hidden_field :checkbox_captcha, value: form.checkbox_captcha %> + +
+ <%= button_tag :submit, class: 'btn btn-default btn-lg btn-strong', id: 'submit-button' do %> + Zaregistrovať + <% end %> +
+
+ <% end %> +
+
diff --git a/app/views/services/autoform/index.html.erb b/app/views/services/autoform/index.html.erb index 8f12af3e..00137877 100644 --- a/app/views/services/autoform/index.html.erb +++ b/app/views/services/autoform/index.html.erb @@ -157,29 +157,32 @@
- - <%= form_tag 'https://docs.google.com/forms/d/1TpYNJfBQVGt4lmKP-wrXGkok2bo-Y6mzpmeUsJTnRis/formResponse', target: 'form-result', id: 'autoform-form' do %> -
-
- <%= label_tag 'entry.204431983', 'Email', class: 'control-label' %> - <%= email_field_tag 'entry.204431983', nil, class: 'form-control input-lg', id: 'autoform-email' %> -
-
-
-
- <%= label_tag 'entry.1349114640', 'Doména', class: 'control-label' %> - <%= text_field_tag 'entry.1349114640', nil, class: 'form-control input-lg', id: 'autoform-domain' %> -

Napríklad: www.websupport.sk

-
-
-
- <%= label_tag :a, ' '.html_safe %>
- <%= button_tag id: 'submit_to_autoform', class: 'btn btn-default btn-lg' do %> - Zaregistrovať - <% end %> -
- <% end %> + + <%#= form_tag 'https://docs.google.com/forms/d/1TpYNJfBQVGt4lmKP-wrXGkok2bo-Y6mzpmeUsJTnRis/formResponse', target: 'form-result', id: 'autoform-form' do %> + + + <%#= label_tag 'entry.204431983', 'Email', class: 'control-label' %> + <%#= email_field_tag 'entry.204431983', nil, class: 'form-control input-lg', id: 'autoform-email' %> + + + + + <%#= label_tag 'entry.1349114640', 'Doména', class: 'control-label' %> + <%#= text_field_tag 'entry.1349114640', nil, class: 'form-control input-lg', id: 'autoform-domain' %> + + + + + + <%#= button_tag id: 'submit_to_autoform', class: 'btn btn-default btn-lg' do %> + + <%# end %> + + <%# end %>
+ + <%= render_registration_form %> +

diff --git a/config/routes.rb b/config/routes.rb index 0c04e862..1c10c8ae 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -42,6 +42,8 @@ resources :slovensko_sk_api, path: 'slovensko-sk-api' end + resources :registrations + resource 'open_data', path: 'otvorene-data' resource 'open_api', path: 'otvorene-api' From 6da4fa79ace5ee8de80591034b93af82c084f4c5 Mon Sep 17 00:00:00 2001 From: michal-rohacek Date: Thu, 8 Jul 2021 08:51:42 +0200 Subject: [PATCH 03/15] Expand registration form onto other controllers --- app/views/registrations/create.js.erb | 1 + app/views/registrations/success.js.erb | 1 + app/views/services/datahub/index.html.erb | 24 +------------ .../services/slovensko_sk_api/index.html.erb | 36 ++++++++++--------- 4 files changed, 22 insertions(+), 40 deletions(-) create mode 100644 app/views/registrations/create.js.erb create mode 100644 app/views/registrations/success.js.erb diff --git a/app/views/registrations/create.js.erb b/app/views/registrations/create.js.erb new file mode 100644 index 00000000..8e8ec5d1 --- /dev/null +++ b/app/views/registrations/create.js.erb @@ -0,0 +1 @@ +$('#<%= registration_form_id(@registration.service) %>').html("<%= j render partial: 'registrations/form', object: @registration %>"); diff --git a/app/views/registrations/success.js.erb b/app/views/registrations/success.js.erb new file mode 100644 index 00000000..91d5d5d7 --- /dev/null +++ b/app/views/registrations/success.js.erb @@ -0,0 +1 @@ +$('#<%= registration_form_id(@registration.service) %>').html("<%= j render 'services/share/flash_message', { type: :notice, message: 'Ďakujeme za Váš záujem. Budeme Vás kontaktovať cez zadaný email.' } %>"); diff --git a/app/views/services/datahub/index.html.erb b/app/views/services/datahub/index.html.erb index 5a7e92f0..7a7682eb 100644 --- a/app/views/services/datahub/index.html.erb +++ b/app/views/services/datahub/index.html.erb @@ -132,30 +132,8 @@

Jednoduchá registrácia na prístup k SQL rozhraniu a API. Kompletné dáta zdrojových registrov. API na konsolidované a prepojené dáta. 256 dopytov mesačne pre kohokoľvek, viac po dohode.

- - + <%= render_registration_form %> - -
- <%= form_tag 'https://docs.google.com/forms/d/e/1FAIpQLSdgW4Hf2fEhX3cpTkoYJTaIVs8pWrTFrItt9Hj_9ZD36yPLZQ/formResponse', target: 'form-result', id: 'datahub-form' do %> -
-
- <%= label_tag 'entry.1902802364', 'Email', class: 'control-label' %> - <%= email_field_tag 'entry.1902802364', nil, class: 'form-control input-lg', id: 'datahub-email' %> -
-
-
- <%= label_tag :a, ' '.html_safe %>
- <%= button_tag id: 'submit_to_datahub', class: 'btn btn-default btn-lg btn-strong' do %> - Zaregistrovať - <% end %> -
- <% end %> -

diff --git a/app/views/services/slovensko_sk_api/index.html.erb b/app/views/services/slovensko_sk_api/index.html.erb index 1b71fb49..43293020 100644 --- a/app/views/services/slovensko_sk_api/index.html.erb +++ b/app/views/services/slovensko_sk_api/index.html.erb @@ -88,23 +88,25 @@ <%= render 'services/share/flash_message', {type: :notice, message: 'Ďakujeme, za Váš záujem. Budeme Vás kontaktovať cez zadaný email.'} %>
- -
- <%= form_tag 'https://docs.google.com/forms/d/e/1FAIpQLSfUuAjnqGjDvSc-Miy6bP0xODXsjr6g04hGAeYlYkJo-3Iu1Q/formResponse', target: 'form-result', id: 'sk-api-form' do %> -
-
- <%= label_tag 'emailAddress', 'Email', class: 'control-label' %> - <%= email_field_tag 'emailAddress', nil, class: 'form-control input-lg', id: 'sk-api-email' %> -
-
-
- <%= label_tag :a, ' '.html_safe %>
- <%= button_tag id: 'submit_to_datahub', class: 'btn btn-default btn-lg btn-strong' do %> - Odoslať - <% end %> -
- <% end %> -
+ + + <%#= form_tag 'https://docs.google.com/forms/d/e/1FAIpQLSfUuAjnqGjDvSc-Miy6bP0xODXsjr6g04hGAeYlYkJo-3Iu1Q/formResponse', target: 'form-result', id: 'sk-api-form' do %> + + + <%#= label_tag 'emailAddress', 'Email', class: 'control-label' %> + <%#= email_field_tag 'emailAddress', nil, class: 'form-control input-lg', id: 'sk-api-email' %> + + + + + <%#= button_tag id: 'submit_to_datahub', class: 'btn btn-default btn-lg btn-strong' do %> + + <%# end %> + + <%# end %> + + + <%= render_registration_form %>
From 5df22341f451b533307d53b5b7952fd60a3f70a6 Mon Sep 17 00:00:00 2001 From: michal-rohacek Date: Thu, 8 Jul 2021 08:51:58 +0200 Subject: [PATCH 04/15] Refine code a bit --- app/views/services/share/_flash_message.html.erb | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/app/views/services/share/_flash_message.html.erb b/app/views/services/share/_flash_message.html.erb index 310cb099..22493a3e 100644 --- a/app/views/services/share/_flash_message.html.erb +++ b/app/views/services/share/_flash_message.html.erb @@ -1,7 +1,4 @@ -<% -css_class = (type == :alert) ? 'danger' : 'success' -%> -
diff --git a/config/locales/docs/sk.yml b/config/locales/docs/sk.yml index a7fc6008..80490b78 100644 --- a/config/locales/docs/sk.yml +++ b/config/locales/docs/sk.yml @@ -1,7 +1,25 @@ sk: - registrations: - email: Email - domain: Doména + email: Email + domain: Doména + + activemodel: + attributes: + registration: + email: Email + autoform_registration: + domain: Doména + errors: + models: + registration: + attributes: + email: + invalid: je v nesprávnom tvare + autoform_registration: + attributes: + domain: + blank: nie je uvedená + + docs: timestamp_columns: ×tamp_columns diff --git a/config/routes.rb b/config/routes.rb index 1c10c8ae..f6be7808 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -43,6 +43,7 @@ end resources :registrations + resources :autoform_registrations, controller: :registrations, path: 'registrations' resource 'open_data', path: 'otvorene-data' resource 'open_api', path: 'otvorene-api' @@ -72,8 +73,8 @@ get '422', to: 'errors#unacceptable' get '500', to: 'errors#internal_error' - # redirect all /api request to to datahub.ekosystem - match '/api/(*endpoint)', to: redirect(host: 'datahub.ekosystem.slovensko.digital'), via: :all + # redirect all /api request to proper datahub.ekosystem api + match '/api/(*endpoint)', to: redirect(host: Environment.api_host), via: :all # backward compatibility for generating url in views concern :syncable do From ff7cc03900996eb4d5dd3c4f706854c4d2de38f0 Mon Sep 17 00:00:00 2001 From: michal-rohacek Date: Wed, 21 Jul 2021 18:55:11 +0200 Subject: [PATCH 08/15] Add newlines --- app/controllers/application_controller.rb | 2 ++ app/lib/environment.rb | 2 +- app/views/registrations/_form.html.erb | 2 +- config/initializers/recaptcha.rb | 2 +- 4 files changed, 5 insertions(+), 3 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index de858d9e..d7156dc6 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -9,11 +9,13 @@ def rollout r.activate!(params[:rollout].to_sym) if params[:rollout] r end + helper_method :rollout def api_host Environment.api_host end + helper_method :api_host def store_referral diff --git a/app/lib/environment.rb b/app/lib/environment.rb index 6864fc98..58620481 100644 --- a/app/lib/environment.rb +++ b/app/lib/environment.rb @@ -4,4 +4,4 @@ module Environment def api_host @host ||= URI.parse(ENV.fetch('DATAHUB_URL')).host end -end \ No newline at end of file +end diff --git a/app/views/registrations/_form.html.erb b/app/views/registrations/_form.html.erb index c6849d48..c8f20b39 100644 --- a/app/views/registrations/_form.html.erb +++ b/app/views/registrations/_form.html.erb @@ -32,4 +32,4 @@ <% end %>
-
\ No newline at end of file + diff --git a/config/initializers/recaptcha.rb b/config/initializers/recaptcha.rb index 2200110f..6f50c4c8 100644 --- a/config/initializers/recaptcha.rb +++ b/config/initializers/recaptcha.rb @@ -1,4 +1,4 @@ Recaptcha.configure do |config| config.site_key = ENV.fetch('RECAPTCHA_SITE_KEY_V3') config.secret_key = ENV.fetch('RECAPTCHA_SECRET_KEY_V3') -end \ No newline at end of file +end From dd18af46712cac36e2b19176a5690d92f73805a0 Mon Sep 17 00:00:00 2001 From: michal-rohacek Date: Wed, 21 Jul 2021 18:55:37 +0200 Subject: [PATCH 09/15] Delete unnecessary lines --- .../services/datahub_controller.rb | 2 -- app/views/services/autoform/index.html.erb | 33 ------------------- 2 files changed, 35 deletions(-) diff --git a/app/controllers/services/datahub_controller.rb b/app/controllers/services/datahub_controller.rb index dc65eb0e..3e69e197 100644 --- a/app/controllers/services/datahub_controller.rb +++ b/app/controllers/services/datahub_controller.rb @@ -1,6 +1,4 @@ class Services::DatahubController < ContentController - include RegistrationsHelper - def index @page.title = 'Vyčistené, štruktúrované dáta nielen o firmách. · Datahub'.html_safe @page.og.image = view_context.image_url('fb-datahub.png') diff --git a/app/views/services/autoform/index.html.erb b/app/views/services/autoform/index.html.erb index 00137877..aaea2628 100644 --- a/app/views/services/autoform/index.html.erb +++ b/app/views/services/autoform/index.html.erb @@ -148,39 +148,6 @@

Registrácia

Jednoduchá registrácia, až 256 vyhľadávaní mesačne pre kohokoľvek. Viac vyhľadávaní po dohode.

- - - -
-
- - <%#= form_tag 'https://docs.google.com/forms/d/1TpYNJfBQVGt4lmKP-wrXGkok2bo-Y6mzpmeUsJTnRis/formResponse', target: 'form-result', id: 'autoform-form' do %> - - - <%#= label_tag 'entry.204431983', 'Email', class: 'control-label' %> - <%#= email_field_tag 'entry.204431983', nil, class: 'form-control input-lg', id: 'autoform-email' %> - - - - - <%#= label_tag 'entry.1349114640', 'Doména', class: 'control-label' %> - <%#= text_field_tag 'entry.1349114640', nil, class: 'form-control input-lg', id: 'autoform-domain' %> - - - - - - <%#= button_tag id: 'submit_to_autoform', class: 'btn btn-default btn-lg' do %> - - <%# end %> - - <%# end %> -
- <%= render_registration_form %>
From 41c87663b1c6b4faf64ffca97e0cafd9196b3975 Mon Sep 17 00:00:00 2001 From: michal-rohacek Date: Wed, 21 Jul 2021 18:57:08 +0200 Subject: [PATCH 10/15] Change how Registration (sub)classes are resolved --- app/controllers/registrations_controller.rb | 2 +- app/helpers/registrations_helper.rb | 12 +----------- app/models/registration.rb | 17 +++++++++++++++-- 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/app/controllers/registrations_controller.rb b/app/controllers/registrations_controller.rb index bea49c7f..504e8edb 100644 --- a/app/controllers/registrations_controller.rb +++ b/app/controllers/registrations_controller.rb @@ -2,7 +2,7 @@ class RegistrationsController < ApplicationController include RegistrationsHelper def create - @registration ||= new_registration(registration_params) + @registration ||= Registration.from(registration_params) if @registration.save { validate_captcha! } render :create diff --git a/app/helpers/registrations_helper.rb b/app/helpers/registrations_helper.rb index bff5669d..abe51340 100644 --- a/app/helpers/registrations_helper.rb +++ b/app/helpers/registrations_helper.rb @@ -3,18 +3,8 @@ def recaptcha_action(service = controller_name) "#{service}_registration" end - def new_registration(args) - raise unless args[:service] - - klass = (args[:service] == 'autoform') ? AutoformRegistration : Registration - klass.new(args) - end - def render_registration_form(service: controller_name, model: nil) - unless model - model = new_registration(service: service) - return if model.invalid?(:render) - end + model ||= Registration.from(service: service) render partial: 'registrations/form', object: model end diff --git a/app/models/registration.rb b/app/models/registration.rb index 2f9db110..0810a1a7 100644 --- a/app/models/registration.rb +++ b/app/models/registration.rb @@ -1,6 +1,5 @@ class Registration include ActiveModel::Model - include ActiveModel::Attributes FORM_URL = 'https://docs.google.com/forms/d/e/1FAIpQLScswqdDYxXtjUDW7Crw0aro3Au87R1dVmHIYyA5UH4jrZNZ5g/formResponse' @@ -13,7 +12,6 @@ class Registration attr_accessor :email, :service, :score validates :email, format: { with: URI::MailTo::EMAIL_REGEXP }, on: :submit - validates :service, inclusion: %w(datahub autoform slovensko_sk_api), on: :render def save return false unless valid?(:submit) @@ -42,4 +40,19 @@ def mapping end private_constant :REQUEST_MAPPING + + def self.from(args) + class_for(args[:service]).new(args) + end + + def self.class_for(service) + case service + when 'autoform' + AutoformRegistration + when 'datahub', 'slovensko_sk_api' + Registration + else + raise "Service is nil or unknown: #{service}" + end + end end From 4ffa498b6a1ebc459d82fa70bc2c68a455cb01e9 Mon Sep 17 00:00:00 2001 From: michal-rohacek Date: Wed, 21 Jul 2021 21:32:39 +0200 Subject: [PATCH 11/15] Fix dotenv files to make tests pass --- .env | 2 +- .env.test | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/.env b/.env index 8f9bbf15..125983ae 100644 --- a/.env +++ b/.env @@ -1,5 +1,5 @@ DATAHUB_URL= -AUTOFORM_URL=https://example.com +AUTOFORM_URL= AUTOFORM_ACCESS_TOKEN= AUTOFORM_PRIVATE_ACCESS_TOKEN= GOVBOX_FORM_ENDPOINT= diff --git a/.env.test b/.env.test index 7b14e879..b4a6147c 100644 --- a/.env.test +++ b/.env.test @@ -1,5 +1,12 @@ +DATAHUB_URL=https://example.com AUTOFORM_URL=https://example.com AUTOFORM_ACCESS_TOKEN= AUTOFORM_PRIVATE_ACCESS_TOKEN= GOVBOX_FORM_ENDPOINT=https://example.com GA_TRACKING_ID= +GTM_ID= +ROLLBAR_ACCESS_TOKEN= +NEWRELIC_LICENSE_KEY= +SECRET_KEY_BASE= +RECAPTCHA_SITE_KEY_V3= +RECAPTCHA_SECRET_KEY_V3= \ No newline at end of file From 26d02dcb7024334ddf37121a1c4a9d1ebd81d3f6 Mon Sep 17 00:00:00 2001 From: michal-rohacek Date: Thu, 22 Jul 2021 14:26:45 +0200 Subject: [PATCH 12/15] Add recaptcha keys for local testing purposes --- .env | 4 ++-- .env.test | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.env b/.env index 125983ae..723fec5d 100644 --- a/.env +++ b/.env @@ -8,5 +8,5 @@ GTM_ID= ROLLBAR_ACCESS_TOKEN= NEWRELIC_LICENSE_KEY= SECRET_KEY_BASE= -RECAPTCHA_SITE_KEY_V3= -RECAPTCHA_SECRET_KEY_V3= +RECAPTCHA_SITE_KEY_V3=6LcNQjAbAAAAAPNGbQNxDu0RCKOFOHRdopkJ4bU4 +RECAPTCHA_SECRET_KEY_V3=6LcNQjAbAAAAAFvDD1DQJfwWVsF4npB9pI928TpU diff --git a/.env.test b/.env.test index b4a6147c..1e7e5cf8 100644 --- a/.env.test +++ b/.env.test @@ -8,5 +8,5 @@ GTM_ID= ROLLBAR_ACCESS_TOKEN= NEWRELIC_LICENSE_KEY= SECRET_KEY_BASE= -RECAPTCHA_SITE_KEY_V3= -RECAPTCHA_SECRET_KEY_V3= \ No newline at end of file +RECAPTCHA_SITE_KEY_V3=6LcNQjAbAAAAAPNGbQNxDu0RCKOFOHRdopkJ4bU4 +RECAPTCHA_SECRET_KEY_V3=6LcNQjAbAAAAAFvDD1DQJfwWVsF4npB9pI928TpU \ No newline at end of file From 10832c8e6582a188b4b4547d79132909c778d04e Mon Sep 17 00:00:00 2001 From: michal-rohacek Date: Thu, 22 Jul 2021 14:26:54 +0200 Subject: [PATCH 13/15] Refine controller a tiny bit --- app/controllers/registrations_controller.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/app/controllers/registrations_controller.rb b/app/controllers/registrations_controller.rb index 504e8edb..cfc5e0d9 100644 --- a/app/controllers/registrations_controller.rb +++ b/app/controllers/registrations_controller.rb @@ -1,6 +1,4 @@ class RegistrationsController < ApplicationController - include RegistrationsHelper - def create @registration ||= Registration.from(registration_params) @@ -14,7 +12,7 @@ def create private def validate_captcha! - captcha_result = verify_recaptcha(minimum_score: 0.5, action: recaptcha_action(@registration.service), model: @registration, message: 'Nastala chyba. Ak problém pretrváva aj v inom prehliadači alebo zariadení, kontaktujte nás.') + captcha_result = verify_recaptcha(minimum_score: 0.5, action: helpers.recaptcha_action(@registration.service), model: @registration, message: 'Nastala chyba. Ak problém pretrváva aj v inom prehliadači alebo zariadení, kontaktujte nás.') @registration.score = recaptcha_reply['score'] captcha_result From 1e227f8c00e495c12b67cc031fe25c790674b34b Mon Sep 17 00:00:00 2001 From: michal-rohacek Date: Tue, 27 Jul 2021 20:28:12 +0200 Subject: [PATCH 14/15] Refine and/or simplify few method calls --- app/controllers/registrations_controller.rb | 2 +- app/helpers/registrations_helper.rb | 4 ++-- app/lib/environment.rb | 4 +--- app/models/registration.rb | 2 +- .../registrations/_registration.html.erb} | 12 ++++++------ app/views/{ => services}/registrations/create.js.erb | 0 app/views/{ => services}/registrations/new.js.erb | 0 7 files changed, 11 insertions(+), 13 deletions(-) rename app/views/{registrations/_form.html.erb => services/registrations/_registration.html.erb} (71%) rename app/views/{ => services}/registrations/create.js.erb (100%) rename app/views/{ => services}/registrations/new.js.erb (100%) diff --git a/app/controllers/registrations_controller.rb b/app/controllers/registrations_controller.rb index cfc5e0d9..a82776bf 100644 --- a/app/controllers/registrations_controller.rb +++ b/app/controllers/registrations_controller.rb @@ -1,6 +1,6 @@ class RegistrationsController < ApplicationController def create - @registration ||= Registration.from(registration_params) + @registration = Registration.build_from(registration_params) if @registration.save { validate_captcha! } render :create diff --git a/app/helpers/registrations_helper.rb b/app/helpers/registrations_helper.rb index abe51340..7d6d5443 100644 --- a/app/helpers/registrations_helper.rb +++ b/app/helpers/registrations_helper.rb @@ -4,8 +4,8 @@ def recaptcha_action(service = controller_name) end def render_registration_form(service: controller_name, model: nil) - model ||= Registration.from(service: service) + model ||= Registration.build_from(service: service) - render partial: 'registrations/form', object: model + render model end end diff --git a/app/lib/environment.rb b/app/lib/environment.rb index 58620481..a84ccabe 100644 --- a/app/lib/environment.rb +++ b/app/lib/environment.rb @@ -1,7 +1,5 @@ module Environment - extend self - - def api_host + def self.api_host @host ||= URI.parse(ENV.fetch('DATAHUB_URL')).host end end diff --git a/app/models/registration.rb b/app/models/registration.rb index 0810a1a7..998a4665 100644 --- a/app/models/registration.rb +++ b/app/models/registration.rb @@ -41,7 +41,7 @@ def mapping private_constant :REQUEST_MAPPING - def self.from(args) + def self.build_from(args) class_for(args[:service]).new(args) end diff --git a/app/views/registrations/_form.html.erb b/app/views/services/registrations/_registration.html.erb similarity index 71% rename from app/views/registrations/_form.html.erb rename to app/views/services/registrations/_registration.html.erb index c8f20b39..1993fa69 100644 --- a/app/views/registrations/_form.html.erb +++ b/app/views/services/registrations/_registration.html.erb @@ -1,17 +1,17 @@ -
+
- <%= form_with model: form, scope: :registration do |f| %> - <% if form.errors.any? %> - <% form.errors.full_messages.each do |message| %> + <%= form_with model: registration, scope: :registration do |f| %> + <% if registration.errors.any? %> + <% registration.errors.full_messages.each do |message| %> <%= render 'services/share/flash_message', { type: :alert, message: message } %> <% end %> <% end %> - <%= recaptcha_v3(action: recaptcha_action(form.service)) %> + <%= recaptcha_v3(action: recaptcha_action(registration.service)) %>
- <% form.user_input_fields.each do |field| %> + <% registration.user_input_fields.each do |field| %>
<%= f.label field, t(field), class: 'control-label' %> diff --git a/app/views/registrations/create.js.erb b/app/views/services/registrations/create.js.erb similarity index 100% rename from app/views/registrations/create.js.erb rename to app/views/services/registrations/create.js.erb diff --git a/app/views/registrations/new.js.erb b/app/views/services/registrations/new.js.erb similarity index 100% rename from app/views/registrations/new.js.erb rename to app/views/services/registrations/new.js.erb From d95d1de92a948a0ca0884fcfcb738395d79e49b3 Mon Sep 17 00:00:00 2001 From: michal-rohacek Date: Wed, 28 Jul 2021 05:13:19 +0200 Subject: [PATCH 15/15] Revert some changes --- app/helpers/registrations_helper.rb | 2 +- .../_form.html.erb} | 12 ++++++------ app/views/{services => }/registrations/create.js.erb | 0 app/views/{services => }/registrations/new.js.erb | 0 4 files changed, 7 insertions(+), 7 deletions(-) rename app/views/{services/registrations/_registration.html.erb => registrations/_form.html.erb} (71%) rename app/views/{services => }/registrations/create.js.erb (100%) rename app/views/{services => }/registrations/new.js.erb (100%) diff --git a/app/helpers/registrations_helper.rb b/app/helpers/registrations_helper.rb index 7d6d5443..f9075e79 100644 --- a/app/helpers/registrations_helper.rb +++ b/app/helpers/registrations_helper.rb @@ -6,6 +6,6 @@ def recaptcha_action(service = controller_name) def render_registration_form(service: controller_name, model: nil) model ||= Registration.build_from(service: service) - render model + render partial: 'registrations/form', object: model end end diff --git a/app/views/services/registrations/_registration.html.erb b/app/views/registrations/_form.html.erb similarity index 71% rename from app/views/services/registrations/_registration.html.erb rename to app/views/registrations/_form.html.erb index 1993fa69..c8f20b39 100644 --- a/app/views/services/registrations/_registration.html.erb +++ b/app/views/registrations/_form.html.erb @@ -1,17 +1,17 @@ -
+
- <%= form_with model: registration, scope: :registration do |f| %> - <% if registration.errors.any? %> - <% registration.errors.full_messages.each do |message| %> + <%= form_with model: form, scope: :registration do |f| %> + <% if form.errors.any? %> + <% form.errors.full_messages.each do |message| %> <%= render 'services/share/flash_message', { type: :alert, message: message } %> <% end %> <% end %> - <%= recaptcha_v3(action: recaptcha_action(registration.service)) %> + <%= recaptcha_v3(action: recaptcha_action(form.service)) %>
- <% registration.user_input_fields.each do |field| %> + <% form.user_input_fields.each do |field| %>
<%= f.label field, t(field), class: 'control-label' %> diff --git a/app/views/services/registrations/create.js.erb b/app/views/registrations/create.js.erb similarity index 100% rename from app/views/services/registrations/create.js.erb rename to app/views/registrations/create.js.erb diff --git a/app/views/services/registrations/new.js.erb b/app/views/registrations/new.js.erb similarity index 100% rename from app/views/services/registrations/new.js.erb rename to app/views/registrations/new.js.erb