Refactor communities controller. Add missing specs on topics order.
This commit is contained in:
@@ -1,8 +1,8 @@
|
|||||||
class CommunitiesController < ApplicationController
|
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 TOPIC_ORDERS
|
||||||
|
|
||||||
has_orders %w{newest most_commented oldest}, only: :show
|
|
||||||
|
|
||||||
skip_authorization_check
|
skip_authorization_check
|
||||||
|
|
||||||
@@ -13,7 +13,7 @@ class CommunitiesController < ApplicationController
|
|||||||
private
|
private
|
||||||
|
|
||||||
def set_order
|
def set_order
|
||||||
@order = params[:order].present? ? params[:order] : "newest"
|
@order = valid_order? ? params[:order] : "newest"
|
||||||
end
|
end
|
||||||
|
|
||||||
def set_community
|
def set_community
|
||||||
@@ -27,4 +27,8 @@ class CommunitiesController < ApplicationController
|
|||||||
def load_participants
|
def load_participants
|
||||||
@participants = @community.participants
|
@participants = @community.participants
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def valid_order?
|
||||||
|
params[:order].present? && TOPIC_ORDERS.include?(params[:order])
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -63,6 +63,43 @@ feature 'Communities' do
|
|||||||
end
|
end
|
||||||
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
|
scenario 'Should display topic edit button when author is logged' do
|
||||||
proposal = create(:proposal)
|
proposal = create(:proposal)
|
||||||
community = proposal.community
|
community = proposal.community
|
||||||
|
|||||||
Reference in New Issue
Block a user