Sha256: 80138abdc40dd1472ccb48b37aec30db6f2def1a879f7bec09e7aa6b497a75c1

Contents?: true

Size: 917 Bytes

Versions: 1

Compression:

Stored size: 917 Bytes

Contents

require "redcarpet"

module BitPlayer
  class Slide < ActiveRecord::Base
    belongs_to :slideshow,
      class_name: "BitPlayer::Slideshow",
      foreign_key: :bit_player_slideshow_id,
      inverse_of: :slides

    validates :title, :body, :position, presence: true
    validates_numericality_of :position, greater_than_or_equal_to: 1
    validates_uniqueness_of :position, scope: :bit_player_slideshow_id

    def render_body
      rendered = ""

      if !body.nil?
        markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML, space_after_headers: true)
        rendered += markdown.render(body).html_safe
      end

      rendered
    end

    def self.update_positions(ids)
      transaction do
        connection.execute "SET CONSTRAINTS slide_position DEFERRED"
        ids.each_with_index do |id, index|
          where(id: id).update_all(position: index + 1)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bit_player-0.1.2 app/models/bit_player/slide.rb