From b0d4a597338b88ff368f13befaf258bb3dd47986 Mon Sep 17 00:00:00 2001 From: Andrew Poland Date: Mon, 13 May 2024 13:36:29 -0400 Subject: [PATCH 1/2] add recaptcha to job post request form --- Gemfile | 1 + Gemfile.lock | 2 ++ app/controllers/job_post_requests_controller.rb | 4 ++-- app/views/job_post_requests/new.html.haml | 1 + config/initializers/recaptcha.rb | 4 ++++ 5 files changed, 10 insertions(+), 2 deletions(-) create mode 100644 config/initializers/recaptcha.rb diff --git a/Gemfile b/Gemfile index a0f50d3..e5de06a 100644 --- a/Gemfile +++ b/Gemfile @@ -35,6 +35,7 @@ gem "test-unit" gem "rack-cache" gem "httparty" +gem "recaptcha" group :development, :test do gem "foreman" diff --git a/Gemfile.lock b/Gemfile.lock index 294b8ff..a6f1d91 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -254,6 +254,7 @@ GEM rb-readline (0.5.5) rdoc (6.6.3.1) psych (>= 4.0.0) + recaptcha (5.16.0) redcarpet (3.6.0) regexp_parser (2.9.0) reline (0.4.3) @@ -399,6 +400,7 @@ DEPENDENCIES rack-cache rails (~> 7.1) rb-readline + recaptcha redcarpet rspec-rails (~> 6.0.2) rubocop diff --git a/app/controllers/job_post_requests_controller.rb b/app/controllers/job_post_requests_controller.rb index b48d2f7..b30fa1e 100644 --- a/app/controllers/job_post_requests_controller.rb +++ b/app/controllers/job_post_requests_controller.rb @@ -5,7 +5,7 @@ def new def create @job_post_request = JobPostRequest.new(job_post_request_params.merge!({ id: 13 })) - if @job_post_request.valid? + if verify_recaptcha(model: @job_post_request) && @job_post_request.valid? @user = User.find_or_create_by!(name: @job_post_request.name, email: @job_post_request.email) @job = Job.new( title: @job_post_request.title, @@ -18,7 +18,7 @@ def create SystemMailer.job_post_request(@job_post_request, @job).deliver_now redirect_to new_job_post_request_path, notice: "Your job post request was sent successfully!" else - flash[:notice] = "There was a problem with sending your job post request" + flash[:notice] = "There was a problem with sending your job post request. Verify all fields are present and the reCAPTCHA is checked." render :new end end diff --git a/app/views/job_post_requests/new.html.haml b/app/views/job_post_requests/new.html.haml index 5709b7b..71e4c14 100644 --- a/app/views/job_post_requests/new.html.haml +++ b/app/views/job_post_requests/new.html.haml @@ -38,6 +38,7 @@ %a{ href: '#' } Preview .tab-container.active = f.input :description, as: :text + = recaptcha_tags .input %label   = f.button :submit, 'Send' diff --git a/config/initializers/recaptcha.rb b/config/initializers/recaptcha.rb new file mode 100644 index 0000000..95f0b2c --- /dev/null +++ b/config/initializers/recaptcha.rb @@ -0,0 +1,4 @@ +Recaptcha.configure do |config| + config.site_key = ENV['RECAPTCHA_SITE_KEY'] + config.secret_key = ENV['RECAPTCHA_SECRET_KEY'] +end \ No newline at end of file From 9fc459b94c8529336597444f2f838c90c2abbc0b Mon Sep 17 00:00:00 2001 From: Andrew Poland Date: Mon, 13 May 2024 13:42:30 -0400 Subject: [PATCH 2/2] add recaptcha to job post request form --- .rubocop.yml | 5 ++++- app/controllers/job_post_requests_controller.rb | 4 +++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index c247528..68069c4 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -16,4 +16,7 @@ AllCops: - 'vendor/**/*' Style/StringLiterals: - EnforcedStyle: double_quotes \ No newline at end of file + EnforcedStyle: double_quotes + +Metrics/MethodLength: + Max: 100 \ No newline at end of file diff --git a/app/controllers/job_post_requests_controller.rb b/app/controllers/job_post_requests_controller.rb index b30fa1e..f82b257 100644 --- a/app/controllers/job_post_requests_controller.rb +++ b/app/controllers/job_post_requests_controller.rb @@ -18,7 +18,9 @@ def create SystemMailer.job_post_request(@job_post_request, @job).deliver_now redirect_to new_job_post_request_path, notice: "Your job post request was sent successfully!" else - flash[:notice] = "There was a problem with sending your job post request. Verify all fields are present and the reCAPTCHA is checked." + flash[:notice] = + "There was a problem with sending your job post request. "\ + "Verify all fields are present and the reCAPTCHA is checked." render :new end end