Sha256: 24fdbe6c19493d844a096dd5cfb5e2365bd432385241d4bf177ed9ff9036dc68
Contents?: true
Size: 1.44 KB
Versions: 1
Compression:
Stored size: 1.44 KB
Contents
require "redcarpet" module BitCore # A collection of ordered Slides. class Slideshow < ActiveRecord::Base has_many :slides, -> { order "position" }, class_name: "BitCore::Slide", foreign_key: :bit_core_slideshow_id, dependent: :destroy, inverse_of: :slideshow has_one :content_provider, as: :source_content, dependent: :nullify validates :title, presence: true accepts_nested_attributes_for :slides def pretty_title return "" if title.nil? Redcarpet::Markdown.new( Redcarpet::Render::HTML.new( filter_html: true, safe_links_only: true ), space_after_headers: true ).render(title).html_safe end def add(slide_params) slide = slides.build(slide_params) slide.position = slides.count + 1 slide end def remove(slide) return false unless slide.destroy (slide.position + 1..slides.last.position).each do |pos| slides.find_by_position(pos).update(position: pos - 1) end true end def sort(ordered_ids) self.class.transaction do self.class.connection.execute "SET CONSTRAINTS " \ "bit_core_slide_position DEFERRED" ordered_ids.each_with_index do |id, idx| slides.find(id).update_attribute(:position, idx + 1) end end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
bit_core-1.2.2 | app/models/bit_core/slideshow.rb |