Adds 3 orders for comments: most_voted, newest, oldest

This commit is contained in:
kikito
2015-11-02 14:01:29 +01:00
parent 8dd60b47f6
commit 73c306a673
7 changed files with 46 additions and 19 deletions

View File

@@ -7,7 +7,7 @@ class DebatesController < ApplicationController
before_action :authenticate_user!, except: [:index, :show] before_action :authenticate_user!, except: [:index, :show]
has_orders %w{hot_score confidence_score created_at most_commented random}, only: :index has_orders %w{hot_score confidence_score created_at most_commented random}, only: :index
has_orders %w{confidence_score created_at}, only: :show has_orders %w{most_voted newest oldest}, only: :show
load_and_authorize_resource load_and_authorize_resource
respond_to :html, :js respond_to :html, :js

View File

@@ -7,7 +7,7 @@ class ProposalsController < ApplicationController
before_action :authenticate_user!, except: [:index, :show] before_action :authenticate_user!, except: [:index, :show]
has_orders %w{hot_score confidence_score created_at most_commented random}, only: :index has_orders %w{hot_score confidence_score created_at most_commented random}, only: :index
has_orders %w{confidence_score created_at}, only: :show has_orders %w{most_voted newest oldest}, only: :show
load_and_authorize_resource load_and_authorize_resource
respond_to :html, :js respond_to :html, :js

View File

@@ -22,10 +22,15 @@ class Comment < ActiveRecord::Base
scope :for_render, -> { with_hidden.includes(user: :organization) } scope :for_render, -> { with_hidden.includes(user: :organization) }
scope :with_visible_author, -> { joins(:user).where("users.hidden_at IS NULL") } scope :with_visible_author, -> { joins(:user).where("users.hidden_at IS NULL") }
scope :sort_by_flags, -> { order(flags_count: :desc, updated_at: :desc) } scope :sort_by_flags, -> { order(flags_count: :desc, updated_at: :desc) }
scope :sort_by_confidence_score , -> { order(confidence_score: :desc, created_at: :desc) }
scope :sort_descendants_by_confidence_score , -> { order(confidence_score: :desc, created_at: :asc) } scope :sort_by_most_voted , -> { order(confidence_score: :desc, created_at: :desc) }
scope :sort_by_created_at, -> { order(created_at: :desc) } scope :sort_descendants_by_most_voted , -> { order(confidence_score: :desc, created_at: :asc) }
scope :sort_descendants_by_created_at, -> { order(created_at: :asc) }
scope :sort_by_newest, -> { order(created_at: :desc) }
scope :sort_descendants_by_newest, -> { order(created_at: :desc) }
scope :sort_by_oldest, -> { order(created_at: :asc) }
scope :sort_descendants_by_oldest, -> { order(created_at: :asc) }
after_create :call_after_commented after_create :call_after_commented

View File

@@ -241,8 +241,9 @@ en:
comments: comments:
select_order: "Sort by" select_order: "Sort by"
orders: orders:
confidence_score: "Most voted" most_voted: "Most voted"
created_at: "Newest" newest: "Newest first"
oldest: "Oldest first"
form: form:
leave_comment: "Leave your comment" leave_comment: "Leave your comment"
comment_as_moderator: "Comment as moderator" comment_as_moderator: "Comment as moderator"

View File

@@ -241,8 +241,9 @@ es:
comments: comments:
select_order: "Ordenar por" select_order: "Ordenar por"
orders: orders:
confidence_score: "Más votados" most_voted: "Más votados"
created_at: "Más nuevos" newest: "Más nuevos primero"
oldest: "Más antiguos primero"
form: form:
leave_comment: "Deja tu comentario" leave_comment: "Deja tu comentario"
comment_as_moderator: "Comentar como moderador" comment_as_moderator: "Comentar como moderador"

View File

@@ -25,15 +25,20 @@ feature 'Commenting debates' do
c2 = create(:comment, :with_confidence_score, commentable: debate, cached_votes_up: 10, cached_votes_total: 12, created_at: Time.now - 1) c2 = create(:comment, :with_confidence_score, commentable: debate, cached_votes_up: 10, cached_votes_total: 12, created_at: Time.now - 1)
c3 = create(:comment, :with_confidence_score, commentable: debate, cached_votes_up: 1, cached_votes_total: 2, created_at: Time.now) c3 = create(:comment, :with_confidence_score, commentable: debate, cached_votes_up: 1, cached_votes_total: 2, created_at: Time.now)
visit debate_path(debate) visit debate_path(debate, order: :most_voted)
expect(c1.body).to appear_before(c2.body) expect(c1.body).to appear_before(c2.body)
expect(c2.body).to appear_before(c3.body) expect(c2.body).to appear_before(c3.body)
visit debate_path(debate, order: :created_at) visit debate_path(debate, order: :newest)
expect(c3.body).to appear_before(c2.body) expect(c3.body).to appear_before(c2.body)
expect(c2.body).to appear_before(c1.body) expect(c2.body).to appear_before(c1.body)
visit debate_path(debate, order: :oldest)
expect(c1.body).to appear_before(c2.body)
expect(c2.body).to appear_before(c3.body)
end end
scenario 'Creation date works differently in roots and in child comments, even when sorting by confidence_score' do scenario 'Creation date works differently in roots and in child comments, even when sorting by confidence_score' do
@@ -42,14 +47,19 @@ feature 'Commenting debates' do
old_child = create(:comment, commentable: debate, parent_id: new_root.id, created_at: Time.now - 10) old_child = create(:comment, commentable: debate, parent_id: new_root.id, created_at: Time.now - 10)
new_child = create(:comment, commentable: debate, parent_id: new_root.id, created_at: Time.now) new_child = create(:comment, commentable: debate, parent_id: new_root.id, created_at: Time.now)
visit debate_path(debate) visit debate_path(debate, order: :most_voted)
expect(new_root.body).to appear_before(old_root.body) expect(new_root.body).to appear_before(old_root.body)
expect(old_child.body).to appear_before(new_child.body) expect(old_child.body).to appear_before(new_child.body)
visit debate_path(debate, order: :created_at) visit debate_path(debate, order: :newest)
expect(new_root.body).to appear_before(old_root.body) expect(new_root.body).to appear_before(old_root.body)
expect(new_child.body).to appear_before(old_child.body)
visit debate_path(debate, order: :oldest)
expect(old_root.body).to appear_before(new_root.body)
expect(old_child.body).to appear_before(new_child.body) expect(old_child.body).to appear_before(new_child.body)
end end

View File

@@ -25,31 +25,41 @@ feature 'Commenting proposals' do
c2 = create(:comment, :with_confidence_score, commentable: proposal, cached_votes_up: 10, cached_votes_total: 12, created_at: Time.now - 1) c2 = create(:comment, :with_confidence_score, commentable: proposal, cached_votes_up: 10, cached_votes_total: 12, created_at: Time.now - 1)
c3 = create(:comment, :with_confidence_score, commentable: proposal, cached_votes_up: 1, cached_votes_total: 2, created_at: Time.now) c3 = create(:comment, :with_confidence_score, commentable: proposal, cached_votes_up: 1, cached_votes_total: 2, created_at: Time.now)
visit proposal_path(proposal) visit proposal_path(proposal, order: :most_voted)
expect(c1.body).to appear_before(c2.body) expect(c1.body).to appear_before(c2.body)
expect(c2.body).to appear_before(c3.body) expect(c2.body).to appear_before(c3.body)
visit proposal_path(proposal, order: :created_at) visit proposal_path(proposal, order: :newest)
expect(c3.body).to appear_before(c2.body) expect(c3.body).to appear_before(c2.body)
expect(c2.body).to appear_before(c1.body) expect(c2.body).to appear_before(c1.body)
visit proposal_path(proposal, order: :oldest)
expect(c1.body).to appear_before(c2.body)
expect(c2.body).to appear_before(c3.body)
end end
scenario 'Creation date works differently in roots and in child comments, even when sorting by confidence_score' do scenario 'Creation date works differently in roots and in child comments, when sorting by confidence_score' do
old_root = create(:comment, commentable: proposal, created_at: Time.now - 10) old_root = create(:comment, commentable: proposal, created_at: Time.now - 10)
new_root = create(:comment, commentable: proposal, created_at: Time.now) new_root = create(:comment, commentable: proposal, created_at: Time.now)
old_child = create(:comment, commentable: proposal, parent_id: new_root.id, created_at: Time.now - 10) old_child = create(:comment, commentable: proposal, parent_id: new_root.id, created_at: Time.now - 10)
new_child = create(:comment, commentable: proposal, parent_id: new_root.id, created_at: Time.now) new_child = create(:comment, commentable: proposal, parent_id: new_root.id, created_at: Time.now)
visit proposal_path(proposal) visit proposal_path(proposal, order: :most_voted)
expect(new_root.body).to appear_before(old_root.body) expect(new_root.body).to appear_before(old_root.body)
expect(old_child.body).to appear_before(new_child.body) expect(old_child.body).to appear_before(new_child.body)
visit proposal_path(proposal, order: :created_at) visit proposal_path(proposal, order: :newest)
expect(new_root.body).to appear_before(old_root.body) expect(new_root.body).to appear_before(old_root.body)
expect(new_child.body).to appear_before(old_child.body)
visit proposal_path(proposal, order: :oldest)
expect(old_root.body).to appear_before(new_root.body)
expect(old_child.body).to appear_before(new_child.body) expect(old_child.body).to appear_before(new_child.body)
end end