app/models/blocky/content_block.rb in blocky-0.0.11 vs app/models/blocky/content_block.rb in blocky-1.0.0

- old
+ new

@@ -1,29 +1,25 @@ module Blocky + # A block of HTML content that can be rendered on one or multiple pages. class ContentBlock < ActiveRecord::Base - before_save :tidy_content + # Validations + validates :content_key, presence: true + validates :description, presence: true, unless: :new_record? - scope :global, -> { where("page_path IS NULL").order(:name) } - scope :per_page, -> { where("page_path IS NOT NULL").order(:page_path) } + # Callbacks + before_save :encode_content - def global? - self.page_path.nil? + # Returns the description of the content block if it exists. + # The content key is used as a fallback if there is no description. + def display_name + description.blank? ? content_key : description end - def tidy_content - tidy = Tidy.new({ - char_encoding: "raw", - doctype: "omit", - indent: "auto", - indent_spaces: 2, - markup: true, - output_xhtml: true, - show_body_only: "yes", - tab_size: 2, - tidy_mark: false - }) - html = tidy.clean(self.content.to_s.strip).force_encoding("utf-8") + private + + # Force encoding of the content to UTF-8 before saving. + def encode_content + html = content.to_s.strip.force_encoding("utf-8") self.content = "\n" + (html.blank? ? "<p><br/></p>" : html) + "\n" end - end end