diff --git a/.gitignore b/.gitignore index 0d307089f..05906bf13 100644 --- a/.gitignore +++ b/.gitignore @@ -17,4 +17,5 @@ /tmp /spec/examples.txt -/config/database.yml \ No newline at end of file +/config/database.yml +/config/secrets.yml diff --git a/Gemfile b/Gemfile index 5dfb2f7c5..f2ef657e5 100644 --- a/Gemfile +++ b/Gemfile @@ -35,6 +35,7 @@ gem 'acts-as-taggable-on' gem "responders" gem 'foundation-rails' gem 'acts_as_votable' +gem "recaptcha", :require => "recaptcha/rails" group :development, :test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console diff --git a/Gemfile.lock b/Gemfile.lock index 0b070914b..34de0c6d2 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -137,6 +137,7 @@ GEM thor (>= 0.18.1, < 2.0) rake (10.4.2) rdoc (4.2.0) + recaptcha (0.4.0) responders (2.1.0) railties (>= 4.2.0, < 5) rspec-core (3.3.1) @@ -210,6 +211,7 @@ DEPENDENCIES jquery-rails pg rails (= 4.2.3) + recaptcha responders rspec-rails (~> 3.0) sass-rails (~> 5.0) diff --git a/app/assets/javascripts/comments.js.coffee b/app/assets/javascripts/comments.js.coffee new file mode 100644 index 000000000..a1635b42c --- /dev/null +++ b/app/assets/javascripts/comments.js.coffee @@ -0,0 +1,9 @@ +jQuery -> + + toggle_comment = (id) -> + $("#js-comment-form-#{id}").toggle() + + $('.js-add-comment-link').click -> + id = $(this).data().id + toggle_comment(id) + false \ No newline at end of file diff --git a/app/controllers/debates_controller.rb b/app/controllers/debates_controller.rb index 9ad043bb6..672f41d20 100644 --- a/app/controllers/debates_controller.rb +++ b/app/controllers/debates_controller.rb @@ -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] @@ -24,8 +25,11 @@ class DebatesController < ApplicationController def create @debate = Debate.new(debate_params) @debate.author = current_user - @debate.save - respond_with @debate + if verify_captcha? and @debate.save + redirect_to @debate, notice: t('flash.actions.create.notice', resource_name: 'Debate') + else + render :new + end end def update @@ -47,4 +51,9 @@ class DebatesController < ApplicationController raise ActiveRecord::RecordNotFound unless @debate.editable_by?(current_user) end + def verify_captcha? + return true unless recaptcha_keys? + verify_recaptcha(model: @debate) + end + end diff --git a/app/helpers/comments_helper.rb b/app/helpers/comments_helper.rb new file mode 100644 index 000000000..8e8aae90c --- /dev/null +++ b/app/helpers/comments_helper.rb @@ -0,0 +1,11 @@ +module CommentsHelper + + def comment_link_text(parent) + parent.class == Debate ? "Comentar" : "Responder" + end + + def comment_button_text(parent) + parent.class == Debate ? "Publicar comentario" : "Publicar respuesta" + end + +end \ No newline at end of file diff --git a/app/helpers/recaptcha_helper.rb b/app/helpers/recaptcha_helper.rb new file mode 100644 index 000000000..9e6fa90e6 --- /dev/null +++ b/app/helpers/recaptcha_helper.rb @@ -0,0 +1,8 @@ +module RecaptchaHelper + + def recaptcha_keys? + Recaptcha.configuration.public_key.present? && + Recaptcha.configuration.private_key.present? + end + +end \ No newline at end of file diff --git a/app/views/comments/_form.html.erb b/app/views/comments/_form.html.erb index b309a34ef..4550aa2b4 100644 --- a/app/views/comments/_form.html.erb +++ b/app/views/comments/_form.html.erb @@ -1,7 +1,11 @@ -<%= form_for [@debate, Comment.new] do |f| %> - <%= f.text_area :body %> - <%= f.hidden_field :commentable_type, value: parent.class %> - <%= f.hidden_field :commentable_id, value: parent.id %> - - <%= f.submit 'Publicar comentario', :class => "button radius" %> -<% end %> \ No newline at end of file +<%= link_to comment_link_text(parent), "", class: "js-add-comment-link", data: {'id': parent.id} %> + +
diff --git a/app/views/debates/_form.html.erb b/app/views/debates/_form.html.erb index 9d0af3892..df43ab8b0 100644 --- a/app/views/debates/_form.html.erb +++ b/app/views/debates/_form.html.erb @@ -30,6 +30,8 @@ Acepto la política de privacidad y el aviso legal <% end %> + <%= render 'shared/captcha' %> +<%= notice %>
<% end %> @@ -22,11 +22,11 @@ <% if alert %><%= alert %>
<% end %> - +