Sha256: c7873925e927c3967adfd2760a8bb0e34e921e49a9550924dca97f97f652037d

Contents?: true

Size: 1.08 KB

Versions: 2

Compression:

Stored size: 1.08 KB

Contents

# frozen_string_literal: true
require "redcarpet"

module BitCore
  # A page of "static" or presentational content (as opposed to data capture).
  class Slide < ActiveRecord::Base
    belongs_to :slideshow,
               class_name: "BitCore::Slideshow",
               foreign_key: :bit_core_slideshow_id,
               inverse_of: :slides

    validates :title, :position, presence: true
    validates :position, numericality: { greater_than_or_equal_to: 1 }
    validates :position, uniqueness: { scope: :bit_core_slideshow_id }

    before_validation :normalize_options
    after_destroy :update_slide_positions

    serialize :options

    def render_body
      return "" if body.nil?

      Redcarpet::Markdown.new(
        Redcarpet::Render::HTML.new(
          filter_html: true,
          safe_links_only: true
        ),
        space_after_headers: true
      ).render(body).html_safe
    end

    private

    def normalize_options
      self.options = options.try(:to_h)
    end

    def update_slide_positions
      slideshow.reload
               .sort(slideshow.slide_ids)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
bit_core-1.4.7 app/models/bit_core/slide.rb
bit_core-2.0.0.beta2 app/models/bit_core/slide.rb