Sha256: b497c248c79bef2fe144770570e266bf29a78b564c843f981a978f9e68fc8dff

Contents?: true

Size: 1.06 KB

Versions: 7

Compression:

Stored size: 1.06 KB

Contents

module PageflowScrolled
  class Chapter < Pageflow::ApplicationRecord
    include Pageflow::SerializedConfiguration
    include Pageflow::AutoGeneratedPermaId
    include Pageflow::NestedRevisionComponent

    belongs_to :storyline, touch: true
    has_many :sections,
             -> { order('pageflow_scrolled_sections.position ASC') },
             dependent: :destroy,
             inverse_of: :chapter
    has_many :content_elements, through: :sections

    nested_revision_components :sections

    attr_accessor :revision # used on :create to lazily create storyline
    before_validation :ensure_storyline, on: :create

    def self.all_for_revision(revision)
      joins(storyline: :revision)
        .where(pageflow_scrolled_storylines: {revision_id: revision})
    end

    private

    def ensure_storyline
      return if storyline.present?
      unless Storyline.all_for_revision(revision).exists?
        Storyline.create!(revision: revision, configuration: {main: true})
      end
      self.storyline = Storyline.all_for_revision(revision).first
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
pageflow-16.0.0 entry_types/scrolled/app/models/pageflow_scrolled/chapter.rb
pageflow-15.8.0 entry_types/scrolled/app/models/pageflow_scrolled/chapter.rb
pageflow-15.7.1 entry_types/scrolled/app/models/pageflow_scrolled/chapter.rb
pageflow-15.7.0 entry_types/scrolled/app/models/pageflow_scrolled/chapter.rb
pageflow-15.6.1 entry_types/scrolled/app/models/pageflow_scrolled/chapter.rb
pageflow-15.6.0 entry_types/scrolled/app/models/pageflow_scrolled/chapter.rb
pageflow-15.5.0 entry_types/scrolled/app/models/pageflow_scrolled/chapter.rb