diff --git a/app/controllers/polls_controller.rb b/app/controllers/polls_controller.rb
index 89999ac78..5401749af 100644
--- a/app/controllers/polls_controller.rb
+++ b/app/controllers/polls_controller.rb
@@ -1,6 +1,7 @@
class PollsController < ApplicationController
include PollsHelper
+ before_action :load_poll, except: [:index]
before_action :load_active_poll, only: :index
load_and_authorize_resource
@@ -40,6 +41,10 @@ class PollsController < ApplicationController
private
+ def load_poll
+ @poll = Poll.find_by(slug: params[:id]) || Poll.find_by(id: params[:id])
+ end
+
def load_active_poll
@active_poll = ActivePoll.first
end
diff --git a/app/views/polls/_poll_group.html.erb b/app/views/polls/_poll_group.html.erb
index 711271509..7221356a8 100644
--- a/app/views/polls/_poll_group.html.erb
+++ b/app/views/polls/_poll_group.html.erb
@@ -49,7 +49,7 @@
<% elsif poll.stats_enabled? %>
<%= link_to poll.name, stats_poll_path(poll) %>
<% else %>
- <%= link_to poll.name, poll %>
+ <%= link_to poll.name, poll_path(poll.slug) %>
<% end %>
<%= poll_dates(poll) %>
@@ -72,7 +72,7 @@
- <%= link_to poll, class: "button hollow expanded" do %>
+ <%= link_to poll_path(poll.slug), class: "button hollow expanded" do %>
<% if poll.expired? %>
<%= t("polls.index.participate_button_expired") %>
<% else %>
diff --git a/db/migrate/20171008154106_add_slug_to_polls.rb b/db/migrate/20171008154106_add_slug_to_polls.rb
new file mode 100644
index 000000000..f9f3d752f
--- /dev/null
+++ b/db/migrate/20171008154106_add_slug_to_polls.rb
@@ -0,0 +1,5 @@
+class AddSlugToPolls < ActiveRecord::Migration
+ def change
+ add_column :polls, :slug, :string
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index 4430a4980..857082f44 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -1127,6 +1127,7 @@ ActiveRecord::Schema.define(version: 20190411090023) do
t.integer "comments_count", default: 0
t.integer "author_id"
t.datetime "hidden_at"
+ t.string "slug"
t.boolean "results_enabled", default: false
t.boolean "stats_enabled", default: false
t.datetime "created_at"
diff --git a/spec/factories/polls.rb b/spec/factories/polls.rb
index 17119ad10..5a23e2630 100644
--- a/spec/factories/polls.rb
+++ b/spec/factories/polls.rb
@@ -2,6 +2,8 @@ FactoryBot.define do
factory :poll do
sequence(:name) { |n| "Poll #{SecureRandom.hex}" }
+ slug "this-is-a-slug"
+
starts_at { 1.month.ago }
ends_at { 1.month.from_now }
diff --git a/spec/features/polls/polls_spec.rb b/spec/features/polls/polls_spec.rb
index 5b970324d..77abe0100 100644
--- a/spec/features/polls/polls_spec.rb
+++ b/spec/features/polls/polls_spec.rb
@@ -130,6 +130,16 @@ feature "Polls" do
let(:geozone) { create(:geozone) }
let(:poll) { create(:poll, summary: "Summary", description: "Description") }
+ scenario "Visit path with id" do
+ visit poll_path(poll.id)
+ expect(page).to have_current_path(poll_path(poll.id))
+ end
+
+ scenario "Visit path with slug" do
+ visit poll_path(poll.slug)
+ expect(page).to have_current_path(poll_path(poll.slug))
+ end
+
scenario "Show answers with videos" do
question = create(:poll_question, poll: poll)
answer = create(:poll_question_answer, question: question, title: "Chewbacca")