Skip to content

Commit c69151f

Browse files
Flagging (#217)
* fix errors * test * add flagging * upgrade rails * title
1 parent a79fd99 commit c69151f

File tree

4 files changed

+34
-0
lines changed

4 files changed

+34
-0
lines changed

app/controllers/documents_controller.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ def edit
1515

1616
# GET /documents/1 or /documents/1.json
1717
def show
18+
# Handle flagging functionality (requires user to be logged in)
19+
if params[:flag] && current_user
20+
@document.disliked_by current_user, vote_scope: 'flag'
21+
elsif params[:unflag] && current_user
22+
@document.undisliked_by current_user, vote_scope: 'flag'
23+
end
24+
25+
redirect_to action: :show if params[:flag] || params[:unflag]
26+
1827
@related_docs = related_documents(@document).first(5)
1928
end
2029
end

app/models/document.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
class Document < ApplicationRecord
22
include PgSearch::Model
33

4+
# Enable flagging functionality
5+
acts_as_votable
6+
47
has_many :documents_questions
58
has_many :questions, through: :documents_questions
69

app/policies/document_policy.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ def update?
1616
user.admin? || document.library.user_id == user.id || user_is_editor?
1717
end
1818

19+
def flag?
20+
!user.nil? # Any logged-in user can flag documents
21+
end
22+
23+
def unflag?
24+
!user.nil? # Any logged-in user can unflag their flags
25+
end
26+
1927
private
2028

2129
def user_is_editor?

app/views/documents/_document.html.erb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,20 @@
3535
</div>
3636
<% end %>
3737
</div>
38+
39+
<!-- Flag Button on the right side -->
40+
<div class="flex items-center mx-4">
41+
<% flag_count = @document.votes_for.where(vote_scope: 'flag', vote_flag: false).count %>
42+
<% if current_user.voted_down_on?(@document, vote_scope: 'flag') %>
43+
<a href="<%= document_path(@document, unflag: 1) %>" title="Document will be disabled if 2 or more people flag it" class="flex items-center px-8 py-1 text-sm bg-red-100 text-red-800 border border-red-300 rounded hover:bg-red-200 transition-colors min-w-36 whitespace-nowrap">
44+
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="size-5 mr-1"><path stroke-linecap="round" stroke-linejoin="round" d="M3 3v1.5M3 21v-6m0 0 2.77-.693a9 9 0 0 1 6.208.682l.108.054a9 9 0 0 0 6.086.71l3.114-.732a48.524 48.524 0 0 1-.005-10.499l-3.11.732a9 9 0 0 1-6.085-.711l-.108-.054a9 9 0 0 0-6.208-.682L3 4.5M3 15V4.5" /></svg><%="Flagged (#{flag_count})" %>
45+
</a>
46+
<% else %>
47+
<a href="<%= document_path(@document, flag: 1) %>" title="Document will be disabled if 2 or more people flag it" class="flex items-center px-8 py-1 text-sm bg-gray-100 text-gray-700 border border-gray-300 rounded hover:bg-gray-200 transition-colors min-w-36 whitespace-nowrap">
48+
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="size-5 mr-1"><path stroke-linecap="round" stroke-linejoin="round" d="M3 3v1.5M3 21v-6m0 0 2.77-.693a9 9 0 0 1 6.208.682l.108.054a9 9 0 0 0 6.086.71l3.114-.732a48.524 48.524 0 0 1-.005-10.499l-3.11.732a9 9 0 0 1-6.085-.711l-.108-.054a9 9 0 0 0-6.208-.682L3 4.5M3 15V4.5" /></svg><%="Flag (#{flag_count})" %>
49+
</a>
50+
<% end %>
51+
</div>
3852
</div>
3953
</div>
4054
<div class="px-4 py-6 text-gray-800 bg-white font-normal">

0 commit comments

Comments
 (0)