Sha256: faac7990d88e0c06be6fe9f76066b07608348ebc5fe831708d5142bdcf609fd3

Contents?: true

Size: 1.76 KB

Versions: 6

Compression:

Stored size: 1.76 KB

Contents

module Pageflow
  class Page < ActiveRecord::Base
    belongs_to :chapter, :touch => true

    attr_accessor :is_first

    validates_inclusion_of :template, :in => ->(_) { Pageflow.config.page_type_names }

    serialize :configuration, JSON

    scope :displayed_in_navigation, -> { where(:display_in_navigation => true) }

    before_save :ensure_perma_id

    def title
      configuration['title'].presence || configuration['additional_title']
    end

    def thumbnail
      model_name, attachment, property = thumbnail_definition

      begin
        model_name.to_s.camelcase.constantize.find(configuration[property]).send(attachment)
      rescue ActiveRecord::RecordNotFound
        ImageFile.new.processed_attachment
      end
    end

    def thumbnail_definition
      # TODO: this has to be refactored to be page type agnostic
      if template == 'video' || template == 'background_video'
        if configuration['poster_image_id'].present?
          ['pageflow/image_file', :attachment, 'poster_image_id']
        else
          ['pageflow/video_file', :poster, 'video_file_id']
        end
      else
        if configuration['thumbnail_image_id'].present?
          ['pageflow/image_file', :attachment, 'thumbnail_image_id']
        elsif configuration['after_image_id'].present?
          ['pageflow/image_file', :attachment, 'after_image_id']
        else
          ['pageflow/image_file', :attachment, 'background_image_id']
        end
      end
    end

    def configuration
      super || {}
    end

    def configuration=(value)
      self.display_in_navigation = value['display_in_navigation']
      super
    end

    def copy_to(chapter)
      chapter.pages << dup
    end

    def ensure_perma_id
      self.perma_id ||= (Page.maximum(:perma_id) || 0) + 1
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
pageflow-0.1.0 app/models/pageflow/page.rb
pageflow-0.0.5 app/models/pageflow/page.rb
pageflow-0.0.4 app/models/pageflow/page.rb
pageflow-0.0.3 app/models/pageflow/page.rb
pageflow-0.0.2 app/models/pageflow/page.rb
pageflow-0.0.1 app/models/pageflow/page.rb