refactoring recaptcha
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
class DebatesController < ApplicationController
|
||||
include RecaptchaHelper
|
||||
before_action :set_debate, only: [:show, :edit, :update]
|
||||
before_action :authenticate_user!, except: [:show, :index]
|
||||
before_action :validate_ownership, only: [:edit, :update]
|
||||
@@ -51,7 +52,7 @@ class DebatesController < ApplicationController
|
||||
end
|
||||
|
||||
def verify_captcha?
|
||||
return true unless Rails.application.secrets.recaptcha_public_key
|
||||
return true unless recaptcha_keys?
|
||||
verify_recaptcha(model: @debate)
|
||||
end
|
||||
|
||||
|
||||
8
app/helpers/recaptcha_helper.rb
Normal file
8
app/helpers/recaptcha_helper.rb
Normal file
@@ -0,0 +1,8 @@
|
||||
module RecaptchaHelper
|
||||
|
||||
def recaptcha_keys?
|
||||
Recaptcha.configuration.public_key.present? &&
|
||||
Recaptcha.configuration.private_key.present?
|
||||
end
|
||||
|
||||
end
|
||||
@@ -5,14 +5,13 @@
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
|
||||
<title><%= content_for?(:title) ? yield(:title) : "Participación" %></title>
|
||||
|
||||
<%= stylesheet_link_tag "application" %>
|
||||
<%= javascript_include_tag "vendor/modernizr" %>
|
||||
<%= javascript_include_tag "application", 'data-turbolinks-track' => true %>
|
||||
<%= csrf_meta_tags %>
|
||||
<script src="https://www.google.com/recaptcha/api.js?hl=<%= I18n.locale %>"></script>
|
||||
</head>
|
||||
<body>
|
||||
<body>
|
||||
|
||||
<%= render 'layouts/header' %>
|
||||
|
||||
@@ -29,5 +28,5 @@
|
||||
<%= yield %>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</body>
|
||||
</html>
|
||||
@@ -1,3 +1,3 @@
|
||||
<% if Rails.application.secrets.recaptcha_public_key %>
|
||||
<%= recaptcha_tags ajax: true %>
|
||||
<% if recaptcha_keys? %>
|
||||
<%= recaptcha_tags ajax: true, hl: I18n.locale %>
|
||||
<% end %>
|
||||
22
spec/helpers/recaptcha_helper_spec.rb
Normal file
22
spec/helpers/recaptcha_helper_spec.rb
Normal file
@@ -0,0 +1,22 @@
|
||||
require 'rails_helper'
|
||||
|
||||
describe RecaptchaHelper do
|
||||
|
||||
describe "#recaptcha_keys?" do
|
||||
|
||||
it "should be true if Recaptcha keys are configured" do
|
||||
allow(Recaptcha.configuration).to receive(:public_key).and_return("akjasf")
|
||||
allow(Recaptcha.configuration).to receive(:private_key).and_return("akjasf4532")
|
||||
|
||||
expect(helper.recaptcha_keys?).to be true
|
||||
end
|
||||
|
||||
it "should be false if Recaptcha keys are not configured" do
|
||||
allow(Recaptcha.configuration).to receive(:public_key).and_return(nil)
|
||||
allow(Recaptcha.configuration).to receive(:private_key).and_return(nil)
|
||||
|
||||
expect(helper.recaptcha_keys?).to be false
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user