Skip to content

Commit

Permalink
added the profanity, hate, violance, sex filter using `language_filte…
Browse files Browse the repository at this point in the history
…r` gem

- these filters are applied on forum_thread's title, forum_posts's body
- error messages are now improves with proper altert message styling
  using bootstrap's alter class
  • Loading branch information
Waishnav committed Jul 7, 2024
1 parent 9affcdd commit ba26daa
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 13 deletions.
15 changes: 15 additions & 0 deletions app/models/forum_post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,27 @@ class ForumPost < ApplicationRecord
belongs_to :forum_thread, counter_cache: true, touch: true
belongs_to :user

validate :clean_body
validates :user_id, :body, presence: true

scope :sorted, -> { order(:created_at) }

after_update :solve_forum_thread, if: :solved?

def clean_body
filters = [:profanity, :sex, :violence, :hate]
detected_words = Set.new

filters.each do |matchlist|
filter = LanguageFilter::Filter.new(matchlist: matchlist)
detected_words.merge(filter.matched(body)) if filter.match?(body)
end

if detected_words.any?
errors.add(:body, "contains inappropriate language: #{detected_words.to_a.join(', ')}")
end
end

def solve_forum_thread
forum_thread.update(solved: true)
end
Expand Down
17 changes: 17 additions & 0 deletions app/models/forum_thread.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class ForumThread < ApplicationRecord
extend FriendlyId
friendly_id :title, use: :slugged
validate :clean_title

belongs_to :forum_category
belongs_to :user
Expand All @@ -22,6 +23,22 @@ class ForumThread < ApplicationRecord
scope :unpinned, -> { where.not(pinned: true) }
scope :unsolved, -> { where.not(solved: true) }

def clean_title
puts "title: #{title}"
filters = [:profanity, :sex, :violence, :hate]

detected_words = Set.new

filters.each do |matchlist|
filter = LanguageFilter::Filter.new(matchlist: matchlist)
detected_words.merge(filter.matched(title)) if filter.match?(title)
end

if detected_words.any?
errors.add(:title, "contains inappropriate language: #{detected_words.to_a.join(', ')}")
end
end

def subscribed_users
(users + optin_subscribers).uniq - optout_subscribers
end
Expand Down
12 changes: 4 additions & 8 deletions app/views/simple_discussion/forum_posts/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,10 @@
html: { data: { behavior: "comment-form" } } do |f| %>
<% if @forum_post.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@forum_post.errors.count, "error") %> <%= t("forum_thread_error_explanation") %></h2>

<ul>
<% @forum_post.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
<div class="alert alert-danger" role="alert">
<% @forum_post.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</div>
<% end %>

Expand Down
6 changes: 1 addition & 5 deletions app/views/simple_discussion/forum_threads/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,10 @@
html: { data: {behavior: "comment-form"} } do |f| %>
<% if @forum_thread.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@forum_thread.errors.count, "error") %> <%= t("forum_thread_error_explanation") %></h2>

<ul>
<div class="alert alert-danger" role="alert">
<% @forum_thread.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>

Expand Down
1 change: 1 addition & 0 deletions simple_discussion.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ Gem::Specification.new do |spec|
spec.add_dependency "friendly_id", ">= 5.2.0"
spec.add_dependency "rails", ">= 4.2"
spec.add_dependency "will_paginate", ">= 3.1.0"
spec.add_dependency "language_filter", ">= 0.3.01"
spec.metadata["rubygems_mfa_required"] = "true"
end

0 comments on commit ba26daa

Please sign in to comment.