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
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

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,7 +5,6 @@
<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 %>

View File

@@ -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 %>

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