Remove duplication in poll permissions

We were checking for `expired?` and `results_enabled?` in views and
helpers, when we've already defined a rule for accessing stats and
results for a poll.

This way we also fix a bug when stats were enabled but the poll wasn't
finished. In this scenario, the link pointed to the stats page, but when
clicking it we'd get a "you don't have permission" message.

Now the link doesn't point to the stats page anymore.
This commit is contained in:
Javi Martín
2019-11-08 21:00:09 +01:00
parent 2029d7baa5
commit 864f750d92
3 changed files with 9 additions and 13 deletions

View File

@@ -49,19 +49,15 @@ module PollsHelper
end
def link_to_poll(text, poll)
if poll.results_enabled?
if can?(:results, poll)
link_to text, results_poll_path(id: poll.slug || poll.id)
elsif poll.stats_enabled?
elsif can?(:stats, poll)
link_to text, stats_poll_path(id: poll.slug || poll.id)
else
link_to text, poll_path(id: poll.slug || poll.id)
end
end
def show_stats_or_results?
@poll.expired? && (@poll.results_enabled? || @poll.stats_enabled?)
end
def results_menu?
controller_name == "polls" && action_name == "results"
end

View File

@@ -1,8 +1,8 @@
<% if show_stats_or_results? %>
<% if can?(:stats, @poll) || can?(:results, @poll) %>
<div class="row margin-top">
<div class="small-12 column">
<ul class="menu simple clear">
<% if @poll.results_enabled? %>
<% if can?(:results, @poll) %>
<% if results_menu? %>
<li class="is-active">
<h2><%= t("polls.show.results_menu") %></h2>
@@ -12,7 +12,7 @@
<% end %>
<% end %>
<% if @poll.stats_enabled? %>
<% if can?(:stats, @poll) %>
<% if stats_menu? %>
<li class="is-active">
<h2><%= t("polls.show.stats_menu") %></h2>

View File

@@ -110,17 +110,17 @@ describe "Polls" do
end
scenario "Poll title link to stats if enabled" do
poll = create(:poll, name: "Poll with stats", stats_enabled: true)
poll = create(:poll, :expired, name: "Poll with stats", stats_enabled: true)
visit polls_path
visit polls_path(filter: "expired")
expect(page).to have_link("Poll with stats", href: stats_poll_path(poll.slug))
end
scenario "Poll title link to results if enabled" do
poll = create(:poll, name: "Poll with results", stats_enabled: true, results_enabled: true)
poll = create(:poll, :expired, name: "Poll with results", stats_enabled: true, results_enabled: true)
visit polls_path
visit polls_path(filter: "expired")
expect(page).to have_link("Poll with results", href: results_poll_path(poll.slug))
end