diff --git a/app/controllers/debates_controller.rb b/app/controllers/debates_controller.rb index e1173f749..b5a0ae25a 100644 --- a/app/controllers/debates_controller.rb +++ b/app/controllers/debates_controller.rb @@ -18,6 +18,11 @@ class DebatesController < ApplicationController helper_method :resource_model, :resource_name respond_to :html, :js + def show + super + redirect_to debate_path(@debate), status: :moved_permanently if request.path != debate_path(@debate) + end + def vote @debate.register_vote(current_user, params[:value]) set_debate_votes(@debate) diff --git a/app/controllers/management/proposals_controller.rb b/app/controllers/management/proposals_controller.rb index b80864bba..6e01a8c77 100644 --- a/app/controllers/management/proposals_controller.rb +++ b/app/controllers/management/proposals_controller.rb @@ -11,6 +11,11 @@ class Management::ProposalsController < Management::BaseController has_orders %w{confidence_score hot_score created_at most_commented random}, only: [:index, :print] has_orders %w{most_voted newest}, only: :show + def show + super + redirect_to management_proposal_path(@proposal), status: :moved_permanently if request.path != management_proposal_path(@proposal) + end + def vote @proposal.register_vote(current_user, 'yes') set_proposal_votes(@proposal) diff --git a/app/controllers/proposals_controller.rb b/app/controllers/proposals_controller.rb index 8da777581..add59a7cb 100644 --- a/app/controllers/proposals_controller.rb +++ b/app/controllers/proposals_controller.rb @@ -17,6 +17,11 @@ class ProposalsController < ApplicationController helper_method :resource_model, :resource_name respond_to :html, :js + def show + super + redirect_to proposal_path(@proposal), status: :moved_permanently if request.path != proposal_path(@proposal) + end + def index_customization @featured_proposals = Proposal.all.sort_by_confidence_score.limit(3) if (!@advanced_search_terms && @search_terms.blank? && @tag_filter.blank?) if @featured_proposals.present? diff --git a/app/models/debate.rb b/app/models/debate.rb index b376b6dea..ceec9946b 100644 --- a/app/models/debate.rb +++ b/app/models/debate.rb @@ -55,6 +55,10 @@ class Debate < ActiveRecord::Base self.pg_search(terms) end + def to_param + "#{id}-#{title}".parameterize + end + def description super.try :html_safe end diff --git a/app/models/proposal.rb b/app/models/proposal.rb index c507f059c..f437fa134 100644 --- a/app/models/proposal.rb +++ b/app/models/proposal.rb @@ -43,6 +43,10 @@ class Proposal < ActiveRecord::Base scope :sort_by_flags, -> { order(flags_count: :desc, updated_at: :desc) } scope :last_week, -> { where("proposals.created_at >= ?", 7.days.ago)} + def to_param + "#{id}-#{title}".parameterize + end + def searchable_values { title => 'A', question => 'B', diff --git a/spec/features/debates_spec.rb b/spec/features/debates_spec.rb index 080a55640..63c076f4d 100644 --- a/spec/features/debates_spec.rb +++ b/spec/features/debates_spec.rb @@ -67,6 +67,28 @@ feature 'Debates' do expect(link_text).to include(debates_path order: :hot_score, page: 1) end + context "Show" do + scenario 'When path matches the friendly url' do + debate = create(:debate) + + right_path = debate_path(debate) + visit right_path + + expect(current_path).to eq(right_path) + end + + scenario 'When path does not match the friendly url' do + debate = create(:debate) + + right_path = debate_path(debate) + old_path = "#{debates_path}/#{debate.id}-something-else" + visit old_path + + expect(current_path).to_not eq(old_path) + expect(current_path).to eq(right_path) + end + end + scenario 'Create' do author = create(:user) login_as(author) diff --git a/spec/features/management/proposals_spec.rb b/spec/features/management/proposals_spec.rb index ffab09f55..f83c37a0c 100644 --- a/spec/features/management/proposals_spec.rb +++ b/spec/features/management/proposals_spec.rb @@ -56,6 +56,34 @@ feature 'Proposals' do end end + context "Show" do + scenario 'When path matches the friendly url' do + proposal = create(:proposal) + + user = create(:user, :level_two) + login_managed_user(user) + + right_path = management_proposal_path(proposal) + visit right_path + + expect(current_path).to eq(right_path) + end + + scenario 'When path does not match the friendly url' do + proposal = create(:proposal) + + user = create(:user, :level_two) + login_managed_user(user) + + right_path = management_proposal_path(proposal) + old_path = "#{management_proposals_path}/#{proposal.id}-something-else" + visit old_path + + expect(current_path).to_not eq(old_path) + expect(current_path).to eq(right_path) + end + end + scenario "Searching" do proposal1 = create(:proposal, title: "Show me what you got") proposal2 = create(:proposal, title: "Get Schwifty") diff --git a/spec/features/proposals_spec.rb b/spec/features/proposals_spec.rb index 24ced0bc3..8942b860c 100644 --- a/spec/features/proposals_spec.rb +++ b/spec/features/proposals_spec.rb @@ -65,6 +65,28 @@ feature 'Proposals' do end end + context "Show" do + scenario 'When path matches the friendly url' do + proposal = create(:proposal) + + right_path = proposal_path(proposal) + visit right_path + + expect(current_path).to eq(right_path) + end + + scenario 'When path does not match the friendly url' do + proposal = create(:proposal) + + right_path = proposal_path(proposal) + old_path = "#{proposals_path}/#{proposal.id}-something-else" + visit old_path + + expect(current_path).to_not eq(old_path) + expect(current_path).to eq(right_path) + end + end + scenario 'Social Media Cards' do proposal = create(:proposal) diff --git a/spec/models/debate_spec.rb b/spec/models/debate_spec.rb index 04132224a..7e6d851fe 100644 --- a/spec/models/debate_spec.rb +++ b/spec/models/debate_spec.rb @@ -692,4 +692,10 @@ describe Debate do end end + describe "#to_param" do + it "should return a friendly url" do + expect(debate.to_param).to eq "#{debate.id} #{debate.title}".parameterize + end + end + end diff --git a/spec/models/proposal_spec.rb b/spec/models/proposal_spec.rb index 8138580a9..eae4d4fbd 100644 --- a/spec/models/proposal_spec.rb +++ b/spec/models/proposal_spec.rb @@ -735,4 +735,10 @@ describe Proposal do end end + describe "#to_param" do + it "should return a friendly url" do + expect(proposal.to_param).to eq "#{proposal.id} #{proposal.title}".parameterize + end + end + end