Replaces can? :flag/:unflag by a helper function

This commit is contained in:
kikito
2015-09-01 10:34:46 +02:00
parent 6902d2851b
commit 005fd4c09a
3 changed files with 27 additions and 4 deletions

View File

@@ -0,0 +1,23 @@
module FlagsHelper
def show_flag_action?(flaggable)
current_user && !own_flaggable?(flaggable) && !flagged?(flaggable)
end
def show_unflag_action?(flaggable)
current_user && !own_flaggable?(flaggable) && flagged?(flaggable)
end
private
def flagged?(flaggable)
Flag.flagged?(current_user, flaggable)
end
def own_flaggable?(flaggable)
if flaggable.is_a? Comment
flaggable.user_id == current_user.id
else
flaggable.author_id == current_user.id
end
end
end

View File

@@ -1,4 +1,4 @@
<% if can? :flag, comment %> <% if show_flag_action? comment %>
<span class="divider">&nbsp;|&nbsp;</span> <span class="divider">&nbsp;|&nbsp;</span>
<a id="flag-expand-comment-<%= comment.id %>" data-dropdown="flag-drop-comment-<%= comment.id %>" aria-controls="flag-drop-comment-<%= comment.id %>" aria-expanded="false" title="<%= t('shared.flag') %>"> <a id="flag-expand-comment-<%= comment.id %>" data-dropdown="flag-drop-comment-<%= comment.id %>" aria-controls="flag-drop-comment-<%= comment.id %>" aria-expanded="false" title="<%= t('shared.flag') %>">
&nbsp;<i class="icon-flag flag-disable"></i>&nbsp;&nbsp; &nbsp;<i class="icon-flag flag-disable"></i>&nbsp;&nbsp;
@@ -10,7 +10,7 @@
</ul> </ul>
<% end %> <% end %>
<% if can? :unflag, comment %> <% if show_unflag_action? comment %>
<span class="divider">&nbsp;|&nbsp;</span> <span class="divider">&nbsp;|&nbsp;</span>
<a id="unflag-expand-comment-<%= comment.id %>" data-dropdown="unflag-drop-comment-<%= comment.id %>" aria-controls="unflag-drop-comment-<%= comment.id %>" aria-expanded="false" title="<%= t('shared.unflag') %>"> <a id="unflag-expand-comment-<%= comment.id %>" data-dropdown="unflag-drop-comment-<%= comment.id %>" aria-controls="unflag-drop-comment-<%= comment.id %>" aria-expanded="false" title="<%= t('shared.unflag') %>">
&nbsp;<i class="icon-flag flag-active"></i>&nbsp;&nbsp; &nbsp;<i class="icon-flag flag-active"></i>&nbsp;&nbsp;

View File

@@ -1,4 +1,4 @@
<% if can? :flag, debate %> <% if show_flag_action? debate %>
<a id="flag-expand-debate-<%= debate.id %>" data-dropdown="flag-drop-debate-<%= debate.id %>" aria-controls="flag-drop-debate-<%= debate.id %>" aria-expanded="false" title="<%= t('shared.flag') %>"> <a id="flag-expand-debate-<%= debate.id %>" data-dropdown="flag-drop-debate-<%= debate.id %>" aria-controls="flag-drop-debate-<%= debate.id %>" aria-expanded="false" title="<%= t('shared.flag') %>">
&nbsp;<i class="icon-flag flag-disable"></i>&nbsp;&nbsp; &nbsp;<i class="icon-flag flag-disable"></i>&nbsp;&nbsp;
</a> </a>
@@ -9,7 +9,7 @@
</ul> </ul>
<% end %> <% end %>
<% if can? :unflag, debate %> <% if show_unflag_action? debate %>
<a id="unflag-expand-debate-<%= debate.id %>" data-dropdown="unflag-drop-debate-<%= debate.id %>" aria-controls="unflag-drop-debate-<%= debate.id %>" aria-expanded="false" title="<%= t('shared.unflag') %>"> <a id="unflag-expand-debate-<%= debate.id %>" data-dropdown="unflag-drop-debate-<%= debate.id %>" aria-controls="unflag-drop-debate-<%= debate.id %>" aria-expanded="false" title="<%= t('shared.unflag') %>">
&nbsp;<i class="icon-flag flag-active"></i>&nbsp;&nbsp; &nbsp;<i class="icon-flag flag-active"></i>&nbsp;&nbsp;
</a> </a>