Sha256: 9d6b9dabc8c2a312b3f3e070edb83b84f76516b8a197c2b4525eef4fbc68fca5

Contents?: true

Size: 1.3 KB

Versions: 3

Compression:

Stored size: 1.3 KB

Contents

module Pulitzer
  class ContentElement < ActiveRecord::Base
    mount_uploader :image, Pulitzer::ImageUploader
    enum kind: [ :template, :free_form ]

    belongs_to :version
    belongs_to :content_element_type
    belongs_to :post_type_content_element_type
    delegate :type, :text_type?, :image_type?, :video_type?, to: :content_element_type
    delegate :post, to: :version

    before_save :handle_sort_order

    default_scope { order(id: :asc) }
    scope :free_form, -> { where(kind: kinds[:free_form]).reorder(sort_order: :asc) }

    def video_link
      if video_type? && !body.nil?
        vimeo_video(body) || youtube_video(body)
      end
    end

    def html
      body.blank? ? "" : body.html_safe
    end

    def empty_body?
      image_type? ? !image? : body.blank?
    end

private
    def vimeo_video(element)
      if element.match(/vimeo/) && code = element.split("/").last
        "https://player.vimeo.com/video/#{code}"
      end
    end

    def youtube_video(element)
      if element.match(/youtube/) && code = element.split("=").last
        "https://www.youtube.com/embed/#{code}"
      end
    end

    def handle_sort_order
      if new_record? && sort_order.nil? && free_form?
        self.sort_order = version.free_form_content_elements.maximum(:sort_order).to_i + 1
      end
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
pulitzer-0.1.7 app/models/pulitzer/content_element.rb
pulitzer-0.1.6 app/models/pulitzer/content_element.rb
pulitzer-0.1.5 app/models/pulitzer/content_element.rb