diff --git a/app/controllers/communities_controller.rb b/app/controllers/communities_controller.rb index 3462c8425..ff74f208b 100644 --- a/app/controllers/communities_controller.rb +++ b/app/controllers/communities_controller.rb @@ -1,8 +1,8 @@ class CommunitiesController < ApplicationController + TOPIC_ORDERS = %w{newest most_commented oldest}.freeze + before_action :set_order, :set_community, :load_topics, :load_participants - before_action :set_order, :set_community, :load_topics, :load_participants, only: :show - - has_orders %w{newest most_commented oldest}, only: :show + has_orders TOPIC_ORDERS skip_authorization_check @@ -13,7 +13,7 @@ class CommunitiesController < ApplicationController private def set_order - @order = params[:order].present? ? params[:order] : "newest" + @order = valid_order? ? params[:order] : "newest" end def set_community @@ -27,4 +27,8 @@ class CommunitiesController < ApplicationController def load_participants @participants = @community.participants end + + def valid_order? + params[:order].present? && TOPIC_ORDERS.include?(params[:order]) + end end diff --git a/spec/features/communities_spec.rb b/spec/features/communities_spec.rb index b9d14c790..4772df24b 100644 --- a/spec/features/communities_spec.rb +++ b/spec/features/communities_spec.rb @@ -63,6 +63,43 @@ feature 'Communities' do end end + scenario "Topic order" do + proposal = create(:proposal) + community = proposal.community + topic1 = create(:topic, community: community) + topic2 = create(:topic, community: community) + topic2_comment = create(:comment, :with_confidence_score, commentable: topic2) + topic3 = create(:topic, community: community) + topic3_comment = create(:comment, :with_confidence_score, commentable: topic3) + topic3_comment = create(:comment, :with_confidence_score, commentable: topic3) + + visit community_path(community, order: :most_commented) + + expect(topic3.title).to appear_before(topic2.title) + expect(topic2.title).to appear_before(topic1.title) + + visit community_path(community, order: :oldest) + + expect(topic1.title).to appear_before(topic2.title) + expect(topic2.title).to appear_before(topic3.title) + + visit community_path(community, order: :newest) + + expect(topic3.title).to appear_before(topic2.title) + expect(topic2.title).to appear_before(topic1.title) + end + + scenario "Should order by newest when order param is invalid" do + proposal = create(:proposal) + community = proposal.community + topic1 = create(:topic, community: community) + topic2 = create(:topic, community: community) + + visit community_path(community, order: "invalid_param") + + expect(topic2.title).to appear_before(topic1.title) + end + scenario 'Should display topic edit button when author is logged' do proposal = create(:proposal) community = proposal.community