Merge pull request #817 from microweb10/friendly_urls

Friendly urls
This commit is contained in:
Raimond Garcia
2016-03-02 17:51:00 +01:00
10 changed files with 107 additions and 0 deletions

View File

@@ -18,6 +18,11 @@ class DebatesController < ApplicationController
helper_method :resource_model, :resource_name helper_method :resource_model, :resource_name
respond_to :html, :js respond_to :html, :js
def show
super
redirect_to debate_path(@debate), status: :moved_permanently if request.path != debate_path(@debate)
end
def vote def vote
@debate.register_vote(current_user, params[:value]) @debate.register_vote(current_user, params[:value])
set_debate_votes(@debate) set_debate_votes(@debate)

View File

@@ -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{confidence_score hot_score created_at most_commented random}, only: [:index, :print]
has_orders %w{most_voted newest}, only: :show 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 def vote
@proposal.register_vote(current_user, 'yes') @proposal.register_vote(current_user, 'yes')
set_proposal_votes(@proposal) set_proposal_votes(@proposal)

View File

@@ -17,6 +17,11 @@ class ProposalsController < ApplicationController
helper_method :resource_model, :resource_name helper_method :resource_model, :resource_name
respond_to :html, :js 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 def index_customization
@featured_proposals = Proposal.all.sort_by_confidence_score.limit(3) if (!@advanced_search_terms && @search_terms.blank? && @tag_filter.blank?) @featured_proposals = Proposal.all.sort_by_confidence_score.limit(3) if (!@advanced_search_terms && @search_terms.blank? && @tag_filter.blank?)
if @featured_proposals.present? if @featured_proposals.present?

View File

@@ -55,6 +55,10 @@ class Debate < ActiveRecord::Base
self.pg_search(terms) self.pg_search(terms)
end end
def to_param
"#{id}-#{title}".parameterize
end
def description def description
super.try :html_safe super.try :html_safe
end end

View File

@@ -43,6 +43,10 @@ class Proposal < ActiveRecord::Base
scope :sort_by_flags, -> { order(flags_count: :desc, updated_at: :desc) } scope :sort_by_flags, -> { order(flags_count: :desc, updated_at: :desc) }
scope :last_week, -> { where("proposals.created_at >= ?", 7.days.ago)} scope :last_week, -> { where("proposals.created_at >= ?", 7.days.ago)}
def to_param
"#{id}-#{title}".parameterize
end
def searchable_values def searchable_values
{ title => 'A', { title => 'A',
question => 'B', question => 'B',

View File

@@ -67,6 +67,28 @@ feature 'Debates' do
expect(link_text).to include(debates_path order: :hot_score, page: 1) expect(link_text).to include(debates_path order: :hot_score, page: 1)
end 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 scenario 'Create' do
author = create(:user) author = create(:user)
login_as(author) login_as(author)

View File

@@ -56,6 +56,34 @@ feature 'Proposals' do
end end
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 scenario "Searching" do
proposal1 = create(:proposal, title: "Show me what you got") proposal1 = create(:proposal, title: "Show me what you got")
proposal2 = create(:proposal, title: "Get Schwifty") proposal2 = create(:proposal, title: "Get Schwifty")

View File

@@ -65,6 +65,28 @@ feature 'Proposals' do
end end
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 scenario 'Social Media Cards' do
proposal = create(:proposal) proposal = create(:proposal)

View File

@@ -692,4 +692,10 @@ describe Debate do
end end
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 end

View File

@@ -735,4 +735,10 @@ describe Proposal do
end end
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 end