Sha256: a818c87c2da98c787427b8681b53482349b5545426a685bd9b017a1968f4a8e4

Contents?: true

Size: 1.46 KB

Versions: 3

Compression:

Stored size: 1.46 KB

Contents

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

    # Associations
    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

    # Validations
    validates :label, presence: true, uniqueness: { scope: :version_id }, unless: :free_form?

    # Callbacks
    before_save :handle_sort_order

    # Scopes
    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.10 app/models/pulitzer/content_element.rb
pulitzer-0.1.9 app/models/pulitzer/content_element.rb
pulitzer-0.1.8 app/models/pulitzer/content_element.rb