DEV Community

Cover image for 🤬 Make users' input polite with PoliteText
Olivier Dumas
Olivier Dumas

Posted on

🤬 Make users' input polite with PoliteText

Hey devs ✌️,

At stanza.dev we help developers to share coding concepts and learn from each others. 👨‍💻 👩‍💻

We can't monitor each post by hand but as Stanza must be a place full of love 🌞, we have developed a gem that removes the bad words from the website : polite_text

Remove swear words 🤬

include PoliteText

str = "This gem is a fucking big shit but let's try it"

PoliteText.be_polite!(str)
=> "This gem is a *** big *** but let's try it"
Enter fullscreen mode Exit fullscreen mode

Check if string is polite 🙅‍♂️ 🙅‍♀️

include PoliteText

str = "This gem is a fucking big shit but let's try it"

PoliteText.is_polite?(str)
=> false
Enter fullscreen mode Exit fullscreen mode

Example for an Article model

# == Schema Information
#
# Table name: articles
#
#  id         :bigint           not null, primary key
#  text      :string           default(""), not null
#  created_at :datetime         not null
#  updated_at :datetime         not null
#

class Article < ApplicationRecord
  include PoliteText

  # Callbacks
  before_save :make_text_polite

  # Methods
  def make_text_polite
    PoliteText.be_polite!(text)
  end
end
Enter fullscreen mode Exit fullscreen mode

Custom swear words list 🌞

You can even pass your custom swear words list that is relevant for your app.

We have opensourced it and we hope that you will like it and contribute to improve it if you want 🙏

👉 stanza.dev - Learn coding concepts faster

Access the gem repo here

Top comments (2)

Collapse
 
bizzibody profile image
Ian bradbury

Definitely book marking this.

Collapse
 
olivdums profile image
Olivier Dumas

Thank you very much ! :)
I've just made the config for custom swear words list even simpler now, feel free to contribute or to send me your feedbacks if I can improve some stuffs !