refactoring recaptcha

This commit is contained in:
rgarcia
2015-07-24 19:55:09 +02:00
committed by Juanjo Bazán
parent 034c62f3b9
commit daab640dd2
5 changed files with 36 additions and 6 deletions

View File

@@ -1,4 +1,5 @@
class DebatesController < ApplicationController class DebatesController < ApplicationController
include RecaptchaHelper
before_action :set_debate, only: [:show, :edit, :update] before_action :set_debate, only: [:show, :edit, :update]
before_action :authenticate_user!, except: [:show, :index] before_action :authenticate_user!, except: [:show, :index]
before_action :validate_ownership, only: [:edit, :update] before_action :validate_ownership, only: [:edit, :update]
@@ -51,7 +52,7 @@ class DebatesController < ApplicationController
end end
def verify_captcha? def verify_captcha?
return true unless Rails.application.secrets.recaptcha_public_key return true unless recaptcha_keys?
verify_recaptcha(model: @debate) verify_recaptcha(model: @debate)
end end

View File

@@ -0,0 +1,8 @@
module RecaptchaHelper
def recaptcha_keys?
Recaptcha.configuration.public_key.present? &&
Recaptcha.configuration.private_key.present?
end
end

View File

@@ -5,14 +5,13 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title><%= content_for?(:title) ? yield(:title) : "Participación" %></title> <title><%= content_for?(:title) ? yield(:title) : "Participación" %></title>
<%= stylesheet_link_tag "application" %> <%= stylesheet_link_tag "application" %>
<%= javascript_include_tag "vendor/modernizr" %> <%= javascript_include_tag "vendor/modernizr" %>
<%= javascript_include_tag "application", 'data-turbolinks-track' => true %> <%= javascript_include_tag "application", 'data-turbolinks-track' => true %>
<%= csrf_meta_tags %> <%= csrf_meta_tags %>
<script src="https://www.google.com/recaptcha/api.js?hl=<%= I18n.locale %>"></script> <script src="https://www.google.com/recaptcha/api.js?hl=<%= I18n.locale %>"></script>
</head> </head>
<body> <body>
<%= render 'layouts/header' %> <%= render 'layouts/header' %>
@@ -29,5 +28,5 @@
<%= yield %> <%= yield %>
</div> </div>
</div> </div>
</body> </body>
</html> </html>

View File

@@ -1,3 +1,3 @@
<% if Rails.application.secrets.recaptcha_public_key %> <% if recaptcha_keys? %>
<%= recaptcha_tags ajax: true %> <%= recaptcha_tags ajax: true, hl: I18n.locale %>
<% end %> <% end %>

View 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