diff --git a/app/controllers/concerns/commentable_actions.rb b/app/controllers/concerns/commentable_actions.rb index a6ddf7b57..525586f18 100644 --- a/app/controllers/concerns/commentable_actions.rb +++ b/app/controllers/concerns/commentable_actions.rb @@ -16,7 +16,7 @@ module CommentableActions def show set_resource_votes(resource) @commentable = resource - @root_comments = resource.comments.roots.recent.page(params[:page]).per(10).for_render + @root_comments = resource.comments.roots.send("sort_by_#{@current_order}").page(params[:page]).per(10).for_render @comments = @root_comments.inject([]){|all, root| all + Comment.descendants_of(root).for_render} @all_visible_comments = @root_comments + @comments @@ -91,4 +91,4 @@ module CommentableActions def index_customization nil end -end \ No newline at end of file +end diff --git a/spec/factories.rb b/spec/factories.rb index ddbb1d072..878627e0d 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -197,7 +197,7 @@ FactoryGirl.define do factory :comment do association :commentable, factory: :debate user - body 'Comment body' + sequence(:body) { |n| "Comment body #{n}" } trait :hidden do hidden_at Time.now diff --git a/spec/features/comments/debates_spec.rb b/spec/features/comments/debates_spec.rb index 8fc4d1d0b..db4b68060 100644 --- a/spec/features/comments/debates_spec.rb +++ b/spec/features/comments/debates_spec.rb @@ -20,6 +20,22 @@ feature 'Commenting debates' do end end + scenario 'Comment order' do + c1 = create(:comment, :with_confidence_score, commentable: debate, cached_votes_up: 100, cached_votes_total: 120, created_at: Time.now - 2) + 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) + + visit debate_path(debate) + + expect(c1.body).to appear_before(c2.body) + expect(c2.body).to appear_before(c3.body) + + visit debate_path(debate, order: :created_at) + + expect(c3.body).to appear_before(c2.body) + expect(c2.body).to appear_before(c1.body) + end + scenario 'Turns links into html links' do create :comment, commentable: debate, body: 'Built with http://rubyonrails.org/' diff --git a/spec/features/comments/proposals_spec.rb b/spec/features/comments/proposals_spec.rb index 9ba74ea81..61ae53d38 100644 --- a/spec/features/comments/proposals_spec.rb +++ b/spec/features/comments/proposals_spec.rb @@ -20,6 +20,22 @@ feature 'Commenting proposals' do end end + scenario 'Comment order' do + c1 = create(:comment, :with_confidence_score, commentable: proposal, cached_votes_up: 100, cached_votes_total: 120, created_at: Time.now - 2) + 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) + + visit proposal_path(proposal) + + expect(c1.body).to appear_before(c2.body) + expect(c2.body).to appear_before(c3.body) + + visit proposal_path(proposal, order: :created_at) + + expect(c3.body).to appear_before(c2.body) + expect(c2.body).to appear_before(c1.body) + end + scenario 'Turns links into html links' do create :comment, commentable: proposal, body: 'Built with http://rubyonrails.org/'