diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb
index 0a0018c9f..fb5e481a0 100644
--- a/app/controllers/comments_controller.rb
+++ b/app/controllers/comments_controller.rb
@@ -1,6 +1,10 @@
class CommentsController < ApplicationController
+ include CustomUrlsHelper
+
before_action :authenticate_user!, only: :create
before_action :load_commentable, only: :create
+ before_action :verify_resident_for_commentable!, only: :create
+ before_action :verify_comments_open!, only: [:create, :vote]
before_action :build_comment, only: :create
load_and_authorize_resource
@@ -75,6 +79,22 @@ class CommentsController < ApplicationController
notifiable = comment.commentable
end
Notification.add(notifiable.author_id, notifiable) unless comment.author_id == notifiable.author_id
- end
+ end
-end
\ No newline at end of file
+ def verify_resident_for_commentable!
+ return if current_user.administrator? || current_user.moderator?
+
+ if @commentable.respond_to?(:comments_for_verified_residents_only?) && @commentable.comments_for_verified_residents_only?
+ verify_resident!
+ end
+ end
+
+ def verify_comments_open!
+ return if current_user.administrator? || current_user.moderator?
+
+ if @commentable.respond_to?(:comments_closed?) && @commentable.comments_closed?
+ redirect_to @commentable, alert: t('comments.comments_closed')
+ end
+ end
+
+end
diff --git a/app/helpers/comments_helper.rb b/app/helpers/comments_helper.rb
index c3532b578..643b3f0c5 100644
--- a/app/helpers/comments_helper.rb
+++ b/app/helpers/comments_helper.rb
@@ -75,4 +75,22 @@ module CommentsHelper
end
end
+ def require_verified_resident_for_commentable?(commentable, current_user)
+ return false if current_user.administrator? || current_user.moderator?
+
+ commentable.respond_to?(:comments_for_verified_residents_only?) && commentable.comments_for_verified_residents_only? && !current_user.residence_verified?
+ end
+
+ def comments_closed_for_commentable?(commentable)
+ commentable.respond_to?(:comments_closed?) && commentable.comments_closed?
+ end
+
+ def comments_closed_text(commentable)
+ if commentable.class == Legislation::Question
+ t("legislation.questions.comments.comments_closed")
+ else
+ t("comments.comments_closed")
+ end
+ end
+
end
diff --git a/app/models/legislation/question.rb b/app/models/legislation/question.rb
index 5ab0ea355..374c43eb5 100644
--- a/app/models/legislation/question.rb
+++ b/app/models/legislation/question.rb
@@ -27,4 +27,16 @@ class Legislation::Question < ActiveRecord::Base
def answer_for_user(user)
answers.where(user: user).first
end
+
+ def comments_for_verified_residents_only?
+ true
+ end
+
+ def comments_closed?
+ !comments_open?
+ end
+
+ def comments_open?
+ process.open_phase?(:debate)
+ end
end
diff --git a/app/views/comments/_comment.html.erb b/app/views/comments/_comment.html.erb
index 4960baba8..969995ee0 100644
--- a/app/views/comments/_comment.html.erb
+++ b/app/views/comments/_comment.html.erb
@@ -81,7 +81,7 @@
<%= t("comments.comment.responses", count: 0) %>
<% end %>
- <% if user_signed_in? %>
+ <% if user_signed_in? && !comments_closed_for_commentable?(comment.commentable) && !require_verified_resident_for_commentable?(comment.commentable, current_user) %>
|
<%= link_to(comment_link_text(comment), "",
class: "js-add-comment-link", data: {'id': dom_id(comment)}) %>
diff --git a/app/views/comments/_comment_tree.html.erb b/app/views/comments/_comment_tree.html.erb
index 746ab9225..c72bd8bcf 100644
--- a/app/views/comments/_comment_tree.html.erb
+++ b/app/views/comments/_comment_tree.html.erb
@@ -13,14 +13,25 @@
<%= render 'shared/wide_order_selector', i18n_namespace: "comments" %>
<% if user_signed_in? %>
- <%= render 'comments/form', {commentable: commentable, parent_id: nil, toggeable: false} %>
+ <% if comments_closed_for_commentable?(commentable) %>
+
+
+ <%= t("comments.verified_only", verify_account: link_to(t("comments.verify_account"), verification_path )).html_safe %>
+
+ <% else %>
+ <%= render 'comments/form', {commentable: commentable, parent_id: nil, toggeable: false} %>
+ <% end %>
<% else %>
-
- <%= t("debates.show.login_to_comment",
- signin: link_to(t("votes.signin"), new_user_session_path),
- signup: link_to(t("votes.signup"), new_user_registration_path)).html_safe %>
+
+
+ <%= t("debates.show.login_to_comment",
+ signin: link_to(t("votes.signin"), new_user_session_path),
+ signup: link_to(t("votes.signup"), new_user_registration_path)).html_safe %>
<% end %>
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 06fa86bb0..9cc3c4027 100755
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -34,6 +34,9 @@ en:
close: Close
menu: Menu
comments:
+ comments_closed: Comments are closed
+ verified_only: To participate %{verify_account}
+ verify_account: verify your account
comment:
admin: Administrator
author: Author
diff --git a/config/locales/es.yml b/config/locales/es.yml
index 3b44c35a8..4c0484b97 100755
--- a/config/locales/es.yml
+++ b/config/locales/es.yml
@@ -34,6 +34,9 @@ es:
close: Cerrar
menu: Menú
comments:
+ comments_closed: Los comentarios están cerrados
+ verified_only: Para participar %{verify_account}
+ verify_account: verifica tu cuenta
comment:
admin: Administrador
author: Autor
diff --git a/config/locales/legislation.en.yml b/config/locales/legislation.en.yml
index 294d35104..56e34b800 100644
--- a/config/locales/legislation.en.yml
+++ b/config/locales/legislation.en.yml
@@ -84,6 +84,7 @@ en:
comments:
comment_button: Publish answer
comments_title: Open answers
+ comments_closed: Closed phase
form:
leave_comment: Leave your answer
question:
diff --git a/config/locales/legislation.es.yml b/config/locales/legislation.es.yml
index 6f0d5d5e7..fe20128ff 100644
--- a/config/locales/legislation.es.yml
+++ b/config/locales/legislation.es.yml
@@ -84,6 +84,7 @@ es:
comments:
comment_button: Publicar respuesta
comments_title: Respuestas abiertas
+ comments_closed: Fase cerrada
form:
leave_comment: Deja tu respuesta
question:
diff --git a/spec/controllers/comments_controller_spec.rb b/spec/controllers/comments_controller_spec.rb
new file mode 100644
index 000000000..abd2577c2
--- /dev/null
+++ b/spec/controllers/comments_controller_spec.rb
@@ -0,0 +1,38 @@
+require 'rails_helper'
+
+describe CommentsController do
+
+ describe 'POST create' do
+ before(:each) do
+ @process = create(:legislation_process, debate_start_date: Date.current - 3.day, debate_end_date: Date.current + 2.days)
+ @question = create(:legislation_question, process: @process, title: "Question 1")
+ @user = create(:user, :level_two)
+ @unverified_user = create(:user)
+ end
+
+ it 'should create an comment if the comments are open' do
+ sign_in @user
+
+ expect do
+ xhr :post, :create, comment: {commentable_id: @question.id, commentable_type: "Legislation::Question", body: "a comment"}
+ end.to change { @question.reload.comments_count }.by(1)
+ end
+
+ it 'should not create a comment if the comments are closed' do
+ sign_in @user
+ @process.update_attribute(:debate_end_date, Date.current - 1.day)
+
+ expect do
+ xhr :post, :create, comment: {commentable_id: @question.id, commentable_type: "Legislation::Question", body: "a comment"}
+ end.to_not change { @question.reload.comments_count }
+ end
+
+ it 'should not create a comment for unverified users when the commentable requires it' do
+ sign_in @unverified_user
+
+ expect do
+ xhr :post, :create, comment: {commentable_id: @question.id, commentable_type: "Legislation::Question", body: "a comment"}
+ end.to_not change { @question.reload.comments_count }
+ end
+ end
+end
diff --git a/spec/factories.rb b/spec/factories.rb
index c0137e175..5c08ae1d9 100644
--- a/spec/factories.rb
+++ b/spec/factories.rb
@@ -486,6 +486,17 @@ FactoryGirl.define do
allegations_end_date Date.current - 4.days
final_publication_date Date.current - 2.days
end
+
+ trait :in_debate_phase do
+ start_date Date.current - 5.days
+ end_date Date.current + 5.days
+ debate_start_date Date.current - 5.days
+ debate_end_date Date.current + 1.days
+ draft_publication_date Date.current + 1.day
+ allegations_start_date Date.current + 2.days
+ allegations_end_date Date.current + 3.days
+ final_publication_date Date.current + 5.days
+ end
end
factory :legislation_draft_version, class: 'Legislation::DraftVersion' do
diff --git a/spec/features/comments/legislation_questions_spec.rb b/spec/features/comments/legislation_questions_spec.rb
index 253c25b99..35521e89b 100644
--- a/spec/features/comments/legislation_questions_spec.rb
+++ b/spec/features/comments/legislation_questions_spec.rb
@@ -2,8 +2,9 @@ require 'rails_helper'
include ActionView::Helpers::DateHelper
feature 'Commenting legislation questions' do
- let(:user) { create :user }
- let(:legislation_question) { create :legislation_question }
+ let(:user) { create :user, :level_two }
+ let(:process) { create :legislation_process, :in_debate_phase }
+ let(:legislation_question) { create :legislation_question, process: process }
scenario 'Index' do
3.times { create(:comment, commentable: legislation_question) }
@@ -181,9 +182,27 @@ feature 'Commenting legislation questions' do
expect(page).to have_content "Can't be blank"
end
+ scenario "Unverified user can't create comments", :js do
+ unverified_user = create :user
+ login_as(unverified_user)
+
+ visit legislation_process_question_path(legislation_question.process, legislation_question)
+
+ expect(page).to have_content "To participate verify your account"
+ end
+
+ scenario "Can't create comments if debate phase is not open", :js do
+ process.update_attributes(debate_start_date: Date.current - 2.days, debate_end_date: Date.current - 1.days)
+ login_as(user)
+
+ visit legislation_process_question_path(legislation_question.process, legislation_question)
+
+ expect(page).to have_content "Closed phase"
+ end
+
scenario 'Reply', :js do
citizen = create(:user, username: 'Ana')
- manuela = create(:user, username: 'Manuela')
+ manuela = create(:user, :level_two, username: 'Manuela')
comment = create(:comment, commentable: legislation_question, user: citizen)
login_as(manuela)
@@ -264,7 +283,7 @@ feature 'Commenting legislation questions' do
end
scenario "Flagging turbolinks sanity check", :js do
- legislation_question = create(:legislation_question, title: "Should we change the world?")
+ legislation_question = create(:legislation_question, process: process, title: "Should we change the world?")
comment = create(:comment, commentable: legislation_question)
login_as(user)
@@ -278,7 +297,6 @@ feature 'Commenting legislation questions' do
end
scenario "Erasing a comment's author" do
- legislation_question = create(:legislation_question)
comment = create(:comment, commentable: legislation_question, body: 'this should be visible')
comment.user.erase
@@ -290,7 +308,6 @@ feature 'Commenting legislation questions' do
end
scenario 'Submit button is disabled after clicking', :js do
- legislation_question = create(:legislation_question)
login_as(user)
visit legislation_process_question_path(legislation_question.process, legislation_question)
diff --git a/spec/features/notifications_spec.rb b/spec/features/notifications_spec.rb
index 08ba7d3a7..b5a78c38c 100644
--- a/spec/features/notifications_spec.rb
+++ b/spec/features/notifications_spec.rb
@@ -10,7 +10,8 @@ feature "Notifications" do
let(:user) { create :user }
let(:debate) { create :debate, author: author }
let(:proposal) { create :proposal, author: author }
- let(:legislation_question) { create(:legislation_question, author: administrator) }
+ let(:process) { create :legislation_process, :in_debate_phase }
+ let(:legislation_question) { create(:legislation_question, process: process, author: administrator) }
let(:legislation_annotation) { create(:legislation_annotation, author: author) }
scenario "User commented on my debate", :js do
@@ -36,7 +37,8 @@ feature "Notifications" do
end
scenario "User commented on my legislation question", :js do
- login_as user
+ verified_user = create(:user, :level_two)
+ login_as verified_user
visit legislation_process_question_path legislation_question.process, legislation_question
fill_in "comment-body-legislation_question_#{legislation_question.id}", with: "I answered your question"