Merge pull request #39 from medialab-prado/28-question-page-comments

Comments in legislation question page
This commit is contained in:
Fernando Blat
2016-12-23 18:20:27 +01:00
committed by GitHub
19 changed files with 583 additions and 16 deletions

View File

@@ -7,6 +7,7 @@ class Admin::Legislation::QuestionsController < Admin::Legislation::BaseControll
end
def create
@question.author = current_user
if @question.save
redirect_to admin_legislation_process_questions_path
else

View File

@@ -2,6 +2,11 @@ class Legislation::QuestionsController < Legislation::BaseController
load_and_authorize_resource :process
load_and_authorize_resource :question, through: :process
has_orders %w{most_voted newest oldest}, only: :show
def show
@commentable = @question
@comment_tree = CommentTree.new(@commentable, params[:page], @current_order)
set_comment_flags(@comment_tree.comments)
end
end

View File

@@ -2,4 +2,8 @@ module LegislationHelper
def format_date(date)
l(date, format: "%d %b %Y") if date
end
def legislation_question_path(question)
legislation_process_question_path(question.process, question)
end
end

View File

@@ -33,7 +33,7 @@ module Abilities
can :mark_featured, Debate
can :unmark_featured, Debate
can :comment_as_administrator, [Debate, Comment, Proposal]
can :comment_as_administrator, [Debate, Comment, Proposal, Legislation::Question]
can [:search, :create, :index, :destroy], ::Moderator
can [:search, :create, :index, :summary], ::Valuator
@@ -49,6 +49,7 @@ module Abilities
can [:manage], ::Legislation::Process
can [:manage], ::Legislation::DraftVersion
can [:manage], ::Legislation::Question
cannot :comment_as_moderator, ::Legislation::Question
end
end
end

View File

@@ -5,7 +5,7 @@ module Abilities
def initialize(user)
self.merge Abilities::Moderation.new(user)
can :comment_as_moderator, [Debate, Comment, Proposal]
can :comment_as_moderator, [Debate, Comment, Proposal, Legislation::Question]
end
end
end

View File

@@ -1,8 +1,11 @@
class Legislation::Question < ActiveRecord::Base
acts_as_paranoid column: :hidden_at
include ActsAsParanoidAliases
belongs_to :author, -> { with_hidden }, class_name: 'User', foreign_key: 'author_id'
belongs_to :process, class_name: 'Legislation::Process', foreign_key: 'legislation_process_id'
has_many :question_options, class_name: 'Legislation::QuestionOption', foreign_key: 'legislation_question_id', dependent: :destroy, inverse_of: :question
# has_many :answers
has_many :comments, as: :commentable
accepts_nested_attributes_for :question_options, :reject_if => proc { |attributes| attributes[:value].blank? }, allow_destroy: true

View File

@@ -1,7 +1,8 @@
class Legislation::QuestionOption < ActiveRecord::Base
belongs_to :question, class_name: 'Legislation::Question', foreign_key: 'legislation_question_id', inverse_of: :question_options
acts_as_paranoid column: :hidden_at
include ActsAsParanoidAliases
# has_many :answers
belongs_to :question, class_name: 'Legislation::Question', foreign_key: 'legislation_question_id', inverse_of: :question_options
validates :question, presence: true
validates :value, presence: true, uniqueness: { scope: :legislation_question_id }

View File

@@ -1,6 +1,6 @@
<% cache [locale_and_user_status(comment), comment, commentable_cache_key(comment.commentable), comment.author, (@comment_flags[comment.id] if @comment_flags)] do %>
<div class="row">
<ul id="<%= dom_id(comment) %>" class="comment no-bullet small-12 column">
<ul id="<%= dom_id(comment) %>" class="comment no-bullet small-12 column">
<% if comment.hidden? || comment.user.hidden? %>
<% if comment.children.size > 0 %>

View File

@@ -0,0 +1,29 @@
<% cache [locale_and_user_status, @current_order, commentable_cache_key(@question), @comment_tree.comments, @comment_tree.comment_authors, @question.comments_count, @comment_flags] do %>
<div class="row comments">
<div id="comments" class="small-12 column">
<h3>
<%= t("legislation.questions.show.comments") %>
<span class="js-comments-count">(<%= @question.comments_count %>)</span>
</h3>
<%= render 'shared/wide_order_selector', i18n_namespace: "comments" %>
<% if user_signed_in? %>
<%= render 'comments/form', {commentable: @question, parent_id: nil, toggeable: false} %>
<% else %>
<br>
<div data-alert class="callout primary">
<%= 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 %>
</div>
<% end %>
<% @comment_tree.root_comments.each do |comment| %>
<%= render 'comments/comment', comment: comment %>
<% end %>
<%= paginate @comment_tree.root_comments %>
</div>
</div>
<% end %>

View File

@@ -49,10 +49,4 @@
</div>
</section>
<hr />
<div class="row">
<div class="small-12 column">
<%= t('.comments') %>
</div>
</div>
<%= render 'comments' %>

View File

@@ -269,7 +269,7 @@ en:
other: "%{count} comments"
debate: Debate
show:
comments: COMMENTS
comments: Comments
next_question: Next question
share: Share
share_twitter: Share on Twitter

View File

@@ -269,7 +269,7 @@ es:
other: "%{count} comentarios"
debate: Debate
show:
comments: COMENTARIOS
comments: Comentarios
next_question: Siguiente pregunta
share: Compartir
share_twitter: Compartir en Twitter

View File

@@ -0,0 +1,5 @@
class AddCommentsCountToLegislationQuestions < ActiveRecord::Migration
def change
add_column :legislation_questions, :comments_count, :integer, default: 0
end
end

View File

@@ -0,0 +1,5 @@
class AddAuthorToLegislationQuestions < ActiveRecord::Migration
def change
add_column :legislation_questions, :author_id, :integer
end
end

View File

@@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20161220120037) do
ActiveRecord::Schema.define(version: 20161222180927) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@@ -293,6 +293,8 @@ ActiveRecord::Schema.define(version: 20161220120037) do
t.datetime "hidden_at"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "comments_count", default: 0
t.integer "author_id"
end
add_index "legislation_questions", ["hidden_at"], name: "index_legislation_questions_on_hidden_at", using: :btree

View File

@@ -393,6 +393,7 @@ FactoryGirl.define do
factory :legislation_question, class: 'Legislation::Question' do
process factory: :legislation_process
title "Question text"
author factory: :user
end
factory :legislation_question_option, class: 'Legislation::QuestionOption' do

View File

@@ -0,0 +1,509 @@
require 'rails_helper'
include ActionView::Helpers::DateHelper
feature 'Commenting legislation questions' do
let(:user) { create :user }
let(:legislation_question) { create :legislation_question }
scenario 'Index' do
3.times { create(:comment, commentable: legislation_question) }
visit legislation_process_question_path(legislation_question.process, legislation_question)
expect(page).to have_css('.comment', count: 3)
comment = Comment.last
within first('.comment') do
expect(page).to have_content comment.user.name
expect(page).to have_content I18n.l(comment.created_at, format: :datetime)
expect(page).to have_content comment.body
end
end
scenario 'Show' do
parent_comment = create(:comment, commentable: legislation_question)
first_child = create(:comment, commentable: legislation_question, parent: parent_comment)
second_child = create(:comment, commentable: legislation_question, parent: parent_comment)
visit comment_path(parent_comment)
expect(page).to have_css(".comment", count: 3)
expect(page).to have_content parent_comment.body
expect(page).to have_content first_child.body
expect(page).to have_content second_child.body
expect(page).to have_link "Go back to #{legislation_question.title}", href: legislation_process_question_path(legislation_question.process, legislation_question)
end
scenario 'Collapsable comments', :js do
parent_comment = create(:comment, body: "Main comment", commentable: legislation_question)
child_comment = create(:comment, body: "First subcomment", commentable: legislation_question, parent: parent_comment)
grandchild_comment = create(:comment, body: "Last subcomment", commentable: legislation_question, parent: child_comment)
visit legislation_process_question_path(legislation_question.process, legislation_question)
expect(page).to have_css('.comment', count: 3)
find("#comment_#{child_comment.id}_children_arrow").trigger('click')
expect(page).to have_css('.comment', count: 2)
expect(page).to_not have_content grandchild_comment.body
find("#comment_#{child_comment.id}_children_arrow").trigger('click')
expect(page).to have_css('.comment', count: 3)
expect(page).to have_content grandchild_comment.body
find("#comment_#{parent_comment.id}_children_arrow").trigger('click')
expect(page).to have_css('.comment', count: 1)
expect(page).to_not have_content child_comment.body
expect(page).to_not have_content grandchild_comment.body
end
scenario 'Comment order' do
c1 = create(:comment, :with_confidence_score, commentable: legislation_question, cached_votes_up: 100, cached_votes_total: 120, created_at: Time.current - 2)
c2 = create(:comment, :with_confidence_score, commentable: legislation_question, cached_votes_up: 10, cached_votes_total: 12, created_at: Time.current - 1)
c3 = create(:comment, :with_confidence_score, commentable: legislation_question, cached_votes_up: 1, cached_votes_total: 2, created_at: Time.current)
visit legislation_process_question_path(legislation_question.process, legislation_question, order: :most_voted)
expect(c1.body).to appear_before(c2.body)
expect(c2.body).to appear_before(c3.body)
visit legislation_process_question_path(legislation_question.process, legislation_question, order: :newest)
expect(c3.body).to appear_before(c2.body)
expect(c2.body).to appear_before(c1.body)
visit legislation_process_question_path(legislation_question.process, legislation_question, order: :oldest)
expect(c1.body).to appear_before(c2.body)
expect(c2.body).to appear_before(c3.body)
end
scenario 'Creation date works differently in roots and in child comments, even when sorting by confidence_score' do
old_root = create(:comment, commentable: legislation_question, created_at: Time.current - 10)
new_root = create(:comment, commentable: legislation_question, created_at: Time.current)
old_child = create(:comment, commentable: legislation_question, parent_id: new_root.id, created_at: Time.current - 10)
new_child = create(:comment, commentable: legislation_question, parent_id: new_root.id, created_at: Time.current)
visit legislation_process_question_path(legislation_question.process, legislation_question, order: :most_voted)
expect(new_root.body).to appear_before(old_root.body)
expect(old_child.body).to appear_before(new_child.body)
visit legislation_process_question_path(legislation_question.process, legislation_question, order: :newest)
expect(new_root.body).to appear_before(old_root.body)
expect(new_child.body).to appear_before(old_child.body)
visit legislation_process_question_path(legislation_question.process, legislation_question, order: :oldest)
expect(old_root.body).to appear_before(new_root.body)
expect(old_child.body).to appear_before(new_child.body)
end
scenario 'Turns links into html links' do
create :comment, commentable: legislation_question, body: 'Built with http://rubyonrails.org/'
visit legislation_process_question_path(legislation_question.process, legislation_question)
within first('.comment') do
expect(page).to have_content 'Built with http://rubyonrails.org/'
expect(page).to have_link('http://rubyonrails.org/', href: 'http://rubyonrails.org/')
expect(find_link('http://rubyonrails.org/')[:rel]).to eq('nofollow')
expect(find_link('http://rubyonrails.org/')[:target]).to eq('_blank')
end
end
scenario 'Sanitizes comment body for security' do
create :comment, commentable: legislation_question, body: "<script>alert('hola')</script> <a href=\"javascript:alert('sorpresa!')\">click me<a/> http://www.url.com"
visit legislation_process_question_path(legislation_question.process, legislation_question)
within first('.comment') do
expect(page).to have_content "click me http://www.url.com"
expect(page).to have_link('http://www.url.com', href: 'http://www.url.com')
expect(page).not_to have_link('click me')
end
end
scenario 'Paginated comments' do
per_page = 10
(per_page + 2).times { create(:comment, commentable: legislation_question)}
visit legislation_process_question_path(legislation_question.process, legislation_question)
expect(page).to have_css('.comment', count: per_page)
within("ul.pagination") do
expect(page).to have_content("1")
expect(page).to have_content("2")
expect(page).to_not have_content("3")
click_link "Next", exact: false
end
expect(page).to have_css('.comment', count: 2)
end
feature 'Not logged user' do
scenario 'can not see comments forms' do
create(:comment, commentable: legislation_question)
visit legislation_process_question_path(legislation_question.process, legislation_question)
expect(page).to have_content 'You must Sign in or Sign up to leave a comment'
within('#comments') do
expect(page).to_not have_content 'Write a comment'
expect(page).to_not have_content 'Reply'
end
end
end
scenario 'Create', :js do
login_as(user)
visit legislation_process_question_path(legislation_question.process, legislation_question)
fill_in "comment-body-legislation_question_#{legislation_question.id}", with: 'Have you thought about...?'
click_button 'Publish comment'
within "#comments" do
expect(page).to have_content 'Have you thought about...?'
expect(page).to have_content '(1)'
end
end
scenario 'Errors on create', :js do
login_as(user)
visit legislation_process_question_path(legislation_question.process, legislation_question)
click_button 'Publish comment'
expect(page).to have_content "Can't be blank"
end
scenario 'Reply', :js do
citizen = create(:user, username: 'Ana')
manuela = create(:user, username: 'Manuela')
comment = create(:comment, commentable: legislation_question, user: citizen)
login_as(manuela)
visit legislation_process_question_path(legislation_question.process, legislation_question)
click_link "Reply"
within "#js-comment-form-comment_#{comment.id}" do
fill_in "comment-body-comment_#{comment.id}", with: 'It will be done next week.'
click_button 'Publish reply'
end
within "#comment_#{comment.id}" do
expect(page).to have_content 'It will be done next week.'
end
expect(page).to_not have_selector("#js-comment-form-comment_#{comment.id}", visible: true)
end
scenario 'Errors on reply', :js do
comment = create(:comment, commentable: legislation_question, user: user)
login_as(user)
visit legislation_process_question_path(legislation_question.process, legislation_question)
click_link "Reply"
within "#js-comment-form-comment_#{comment.id}" do
click_button 'Publish reply'
expect(page).to have_content "Can't be blank"
end
end
scenario "N replies", :js do
parent = create(:comment, commentable: legislation_question)
7.times do
create(:comment, commentable: legislation_question, parent: parent)
parent = parent.children.first
end
visit legislation_process_question_path(legislation_question.process, legislation_question)
expect(page).to have_css(".comment.comment.comment.comment.comment.comment.comment.comment")
end
scenario "Flagging as inappropriate", :js do
comment = create(:comment, commentable: legislation_question)
login_as(user)
visit legislation_process_question_path(legislation_question.process, legislation_question)
within "#comment_#{comment.id}" do
page.find("#flag-expand-comment-#{comment.id}").click
page.find("#flag-comment-#{comment.id}").click
expect(page).to have_css("#unflag-expand-comment-#{comment.id}")
end
expect(Flag.flagged?(user, comment)).to be
end
scenario "Undoing flagging as inappropriate", :js do
comment = create(:comment, commentable: legislation_question)
Flag.flag(user, comment)
login_as(user)
visit legislation_process_question_path(legislation_question.process, legislation_question)
within "#comment_#{comment.id}" do
page.find("#unflag-expand-comment-#{comment.id}").click
page.find("#unflag-comment-#{comment.id}").click
expect(page).to have_css("#flag-expand-comment-#{comment.id}")
end
expect(Flag.flagged?(user, comment)).to_not be
end
scenario "Flagging turbolinks sanity check", :js do
legislation_question = create(:legislation_question, title: "Should we change the world?")
comment = create(:comment, commentable: legislation_question)
login_as(user)
visit legislation_process_path(legislation_question.process)
click_link "Should we change the world?"
within "#comment_#{comment.id}" do
page.find("#flag-expand-comment-#{comment.id}").click
expect(page).to have_selector("#flag-comment-#{comment.id}")
end
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
visit legislation_process_question_path(legislation_question.process, legislation_question)
within "#comment_#{comment.id}" do
expect(page).to have_content('User deleted')
expect(page).to have_content('this should be visible')
end
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)
fill_in "comment-body-legislation_question_#{legislation_question.id}", with: 'Testing submit button!'
click_button 'Publish comment'
# The button's text should now be "..."
# This should be checked before the Ajax request is finished
expect(page).to_not have_button 'Publish comment'
expect(page).to have_content('Testing submit button!')
end
feature "Moderators" do
scenario "can create comment as a moderator", :js do
moderator = create(:moderator)
login_as(moderator.user)
visit legislation_process_question_path(legislation_question.process, legislation_question)
fill_in "comment-body-legislation_question_#{legislation_question.id}", with: "I am moderating!"
check "comment-as-moderator-legislation_question_#{legislation_question.id}"
click_button "Publish comment"
within "#comments" do
expect(page).to have_content "I am moderating!"
expect(page).to have_content "Moderator ##{moderator.id}"
expect(page).to have_css "div.is-moderator"
expect(page).to have_css "img.moderator-avatar"
end
end
scenario "can create reply as a moderator", :js do
citizen = create(:user, username: "Ana")
manuela = create(:user, username: "Manuela")
moderator = create(:moderator, user: manuela)
comment = create(:comment, commentable: legislation_question, user: citizen)
login_as(manuela)
visit legislation_process_question_path(legislation_question.process, legislation_question)
click_link "Reply"
within "#js-comment-form-comment_#{comment.id}" do
fill_in "comment-body-comment_#{comment.id}", with: "I am moderating!"
check "comment-as-moderator-comment_#{comment.id}"
click_button 'Publish reply'
end
within "#comment_#{comment.id}" do
expect(page).to have_content "I am moderating!"
expect(page).to have_content "Moderator ##{moderator.id}"
expect(page).to have_css "div.is-moderator"
expect(page).to have_css "img.moderator-avatar"
end
expect(page).to_not have_selector("#js-comment-form-comment_#{comment.id}", visible: true)
end
scenario "can not comment as an administrator" do
moderator = create(:moderator)
login_as(moderator.user)
visit legislation_process_question_path(legislation_question.process, legislation_question)
expect(page).to_not have_content "Comment as administrator"
end
end
feature "Administrators" do
scenario "can create comment as an administrator", :js do
admin = create(:administrator)
login_as(admin.user)
visit legislation_process_question_path(legislation_question.process, legislation_question)
fill_in "comment-body-legislation_question_#{legislation_question.id}", with: "I am your Admin!"
check "comment-as-administrator-legislation_question_#{legislation_question.id}"
click_button "Publish comment"
within "#comments" do
expect(page).to have_content "I am your Admin!"
expect(page).to have_content "Administrator ##{admin.id}"
expect(page).to have_css "div.is-admin"
expect(page).to have_css "img.admin-avatar"
end
end
scenario "can create reply as an administrator", :js do
citizen = create(:user, username: "Ana")
manuela = create(:user, username: "Manuela")
admin = create(:administrator, user: manuela)
comment = create(:comment, commentable: legislation_question, user: citizen)
login_as(manuela)
visit legislation_process_question_path(legislation_question.process, legislation_question)
click_link "Reply"
within "#js-comment-form-comment_#{comment.id}" do
fill_in "comment-body-comment_#{comment.id}", with: "Top of the world!"
check "comment-as-administrator-comment_#{comment.id}"
click_button 'Publish reply'
end
within "#comment_#{comment.id}" do
expect(page).to have_content "Top of the world!"
expect(page).to have_content "Administrator ##{admin.id}"
expect(page).to have_css "div.is-admin"
expect(page).to have_css "img.admin-avatar"
end
expect(page).to_not have_selector("#js-comment-form-comment_#{comment.id}", visible: true)
end
scenario "can not comment as a moderator" do
admin = create(:administrator)
login_as(admin.user)
visit legislation_process_question_path(legislation_question.process, legislation_question)
expect(page).to_not have_content "Comment as moderator"
end
end
feature 'Voting comments' do
background do
@manuela = create(:user, verified_at: Time.current)
@pablo = create(:user)
@legislation_question = create(:legislation_question)
@comment = create(:comment, commentable: @legislation_question)
login_as(@manuela)
end
scenario 'Show' do
create(:vote, voter: @manuela, votable: @comment, vote_flag: true)
create(:vote, voter: @pablo, votable: @comment, vote_flag: false)
visit legislation_process_question_path(@legislation_question.process, @legislation_question)
within("#comment_#{@comment.id}_votes") do
within(".in_favor") do
expect(page).to have_content "1"
end
within(".against") do
expect(page).to have_content "1"
end
expect(page).to have_content "2 votes"
end
end
scenario 'Create', :js do
visit legislation_process_question_path(@legislation_question.process, @legislation_question)
within("#comment_#{@comment.id}_votes") do
find(".in_favor a").click
within(".in_favor") do
expect(page).to have_content "1"
end
within(".against") do
expect(page).to have_content "0"
end
expect(page).to have_content "1 vote"
end
end
scenario 'Update', :js do
visit legislation_process_question_path(@legislation_question.process, @legislation_question)
within("#comment_#{@comment.id}_votes") do
find('.in_favor a').click
find('.against a').click
within('.in_favor') do
expect(page).to have_content "0"
end
within('.against') do
expect(page).to have_content "1"
end
expect(page).to have_content "1 vote"
end
end
xscenario 'Trying to vote multiple times', :js do
visit legislation_process_question_path(@legislation_question.process, @legislation_question)
within("#comment_#{@comment.id}_votes") do
find('.in_favor a').click
within('.in_favor') do
expect(page).to have_content "1"
end
find('.in_favor a').click
within('.in_favor') do
expect(page).to_not have_content "2"
expect(page).to have_content "1"
end
within('.against') do
expect(page).to have_content "0"
end
expect(page).to have_content "1 vote"
end
end
end
end

View File

@@ -12,6 +12,7 @@ describe "Abilities::Administrator" do
let(:debate) { create(:debate) }
let(:comment) { create(:comment) }
let(:proposal) { create(:proposal) }
let(:legislation_question) { create(:legislation_question) }
let(:hidden_debate) { create(:debate, :hidden) }
let(:hidden_comment) { create(:comment, :hidden) }
@@ -50,6 +51,9 @@ describe "Abilities::Administrator" do
it { should be_able_to(:comment_as_administrator, proposal) }
it { should_not be_able_to(:comment_as_moderator, proposal) }
it { should be_able_to(:comment_as_administrator, legislation_question) }
it { should_not be_able_to(:comment_as_moderator, legislation_question) }
it { should be_able_to(:manage, Annotation) }
it { should be_able_to(:read, SpendingProposal) }

View File

@@ -11,6 +11,7 @@ describe "Abilities::Moderator" do
let(:debate) { create(:debate) }
let(:comment) { create(:comment) }
let(:proposal) { create(:proposal) }
let(:legislation_question) { create(:legislation_question) }
let(:own_debate) { create(:debate, author: user) }
let(:own_comment) { create(:comment, author: user) }
@@ -101,7 +102,9 @@ describe "Abilities::Moderator" do
it { should be_able_to(:comment_as_moderator, debate) }
it { should be_able_to(:comment_as_moderator, proposal) }
it { should be_able_to(:comment_as_moderator, legislation_question) }
it { should_not be_able_to(:comment_as_administrator, debate) }
it { should_not be_able_to(:comment_as_administrator, proposal) }
it { should_not be_able_to(:comment_as_administrator, legislation_question) }
end
end