Sha256: 83779405ae6c83380e2e5bd002d9f66eeeb5a1e55d2276008138f47d8e629212

Contents?: true

Size: 1.3 KB

Versions: 1

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, -> { unscoped.where(kind: kinds[:free_form]).order(sort_order: :asc) }

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

    def html
      body.html_safe if body
    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

1 entries across 1 versions & 1 rubygems

Version Path
pulitzer-0.1.3 app/models/pulitzer/content_element.rb