Skip to content

Instantly share code, notes, and snippets.

@JohnBDonner
Last active October 27, 2017 09:25
Show Gist options
  • Save JohnBDonner/c13c158191d19bdb47477e64670b38bf to your computer and use it in GitHub Desktop.
Save JohnBDonner/c13c158191d19bdb47477e64670b38bf to your computer and use it in GitHub Desktop.
Coding Challenge

Description

Imagine we have a book model and chapter model. Both models have attributes that needs to have sanitized html. We use a gem called sanitizer to sanitize the text. Now how would you refactor this code to be more DRY and scalable? Bonus: How would you test this?

Directions:

  • Clone the gist
  • Modify the code (change these files or add your own)
  • Save your modifications as a forked gist
  • Respond to the Breezy email with a link to your gist
require "sanitize"
class Book < ApplicationRecord
SANITIZE_CONFIG = Sanitize::Config::BASIC.merge output_encoding: "UTF-8"
attribute :about_the_book, :string
before_validation :sanitize_html
private
def sanitize_html
self.about_the_book = Sanitize.fragment about_the_book, SANITIZE_CONFIG
end
end
require "sanitize"
class Chapter < ApplicationRecord
SANITIZE_CONFIG = Sanitize::Config::BASIC.merge output_encoding: "UTF-8"
attribute :content, :string
before_validation :sanitize_html
private
def sanitize_html
self.content = Sanitize.fragment content, SANITIZE_CONFIG
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment