Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,27 @@ module Editor
class LinkableEntriesController < ActionController::Base
include Pageflow::EditorController

helper PageflowScrolled::LinkedEntriesHelper
helper Pageflow::FilesHelper
helper Pageflow::SocialShareHelper

def index
@entries = Pageflow::Entry
.accessible_by(current_ability, :read)
.where(account: @entry.account, fragment_library: nil)
.where.not(id: @entry.id)
.preload(:site, :account, :published_revision, :draft, permalink: :directory)
.order(:title)
@linkable_entries = LinkableEntry.all(
entries
.preload(:site, :account, :published_revision, :draft, permalink: :directory)
.order(:title),
except: @entry
)
end

def show
@linkable_entry = LinkableEntry.find(entries, params[:id], except: @entry)
head :not_found unless @linkable_entry
end

private

def entries
@entry.site.entries.accessible_by(current_ability, :read)
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,80 +26,23 @@ def scrolled_entry_json_seed_script_tag(scrolled_entry, options = {})
end

def scrolled_entry_json_seed(json, scrolled_entry, options = {})
revision = scrolled_entry.revision
sections = scrolled_entry_json_seed_sections(scrolled_entry, options)
content_elements = scrolled_entry_json_seed_content_elements(revision, sections)
content = RevisionContent.new(scrolled_entry.revision,
include_hidden: options[:include_hidden_sections],
cutoff: scrolled_entry.cutoff_mode_enabled_for?(request))
widgets = scrolled_entry.resolve_widgets(insert_point: :react)
entry_config = Pageflow.config_for(scrolled_entry)

json.partial!('pageflow_scrolled/entry_json_seed/entry',
entry: scrolled_entry,
entry_config:,
storylines: scrolled_entry_json_seed_storylines(revision),
chapters: scrolled_entry_json_seed_chapters(revision, options),
sections:,
content_elements:,
content:,
file_reference_locations: EntryFileReferenceLocations.new(
entry_config,
content_elements,
content.content_elements,
include_unused: options[:include_unused_file_reference_locations]
),
linked_entries: scrolled_linked_entries(scrolled_entry,
content_elements + widgets),
widgets:,
options:)
end

private

def scrolled_entry_json_seed_storylines(revision)
Storyline.all_for_revision(revision)
end

def scrolled_entry_json_seed_sections(scrolled_entry, options)
main_storyline = Storyline.all_for_revision(scrolled_entry.revision).first || Storyline.new

sections =
if scrolled_entry.cutoff_mode_enabled_for?(request)
main_storyline.sections_before_cutoff_section
else
Section.all_for_revision(scrolled_entry.revision)
end

return sections if options[:include_hidden_sections]

sections.reject { |section| section.configuration['hidden'] }
end

def scrolled_entry_json_seed_chapters(revision, options)
chapters = Chapter.all_for_revision(revision)
return chapters if options[:include_hidden_sections]

has_visible_sections, has_hidden_sections =
scrolled_entry_json_seed_chapter_section_visibilites(revision)

chapters.reject do |chapter|
has_hidden_sections[chapter.id] && !has_visible_sections[chapter.id]
end
end

def scrolled_entry_json_seed_chapter_section_visibilites(revision)
has_visible_sections = {}
has_hidden_sections = {}

Section.all_for_revision(revision).each do |section|
if section.configuration['hidden']
has_hidden_sections[section.chapter_id] = true
else
has_visible_sections[section.chapter_id] = true
end
end

[has_visible_sections, has_hidden_sections]
end

def scrolled_entry_json_seed_content_elements(revision, sections)
ContentElement.all_for_revision(revision).where(section: sections)
end
end
end
Original file line number Diff line number Diff line change
@@ -1,32 +1,31 @@
module PageflowScrolled
# Determine urls of entries that links point to.
# Render entries and chapters that links point to.
#
# @api private
module LinkedEntriesHelper
include Pageflow::SocialShareHelper

def scrolled_linked_entries(scrolled_entry, configurables)
entry_ids = EntryLinkReferences.new(configurables.map(&:configuration)).entry_ids
return [] if entry_ids.empty?

Pageflow::Entry
.where(id: entry_ids, account_id: scrolled_entry.account.id)
.preload(:site, :account, :published_revision, :draft, permalink: :directory)
.order(:id)
.map do |entry|
scrolled_linked_entry(entry, include_preview: !scrolled_entry.published_revision?)
end
def scrolled_linked_entries_json_seed(json, scrolled_entry, configurables)
json.linked_entries(scrolled_linked_entries(scrolled_entry, configurables)) do |linked_entry|
json.partial!(scrolled_linked_entry_partial(scrolled_entry), linked_entry:)
end
end

def scrolled_linked_entry(entry, include_preview: true)
linked_entry = Pageflow::PublishedEntry.new(entry, entry.published_revision || entry.draft)
result = {entry: linked_entry, url: social_share_entry_url(linked_entry)}
private

return result unless include_preview
def scrolled_linked_entries(scrolled_entry, configurables)
LinkedEntry.all(
scrolled_entry.account.entries.preload(:site, :account, permalink: :directory),
EntryLinkReferences.new(configurables.map(&:configuration)).chapter_perma_ids_by_entry_id
)
end

result.merge(preview_url: main_app.preview_admin_entry_path(entry),
publication_state: entry.publication_state,
signature: EntryLinkSignature.generate(entry.id))
def scrolled_linked_entry_partial(scrolled_entry)
if scrolled_entry.published_revision?
'pageflow_scrolled/linked_entries/linked_entry'
else
'pageflow_scrolled/linked_entries/preview_linked_entry'
end
end
end
end
30 changes: 30 additions & 0 deletions entry_types/scrolled/app/models/pageflow_scrolled/chapter_slugs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module PageflowScrolled
# Compute chapter slugs the same way as the frontend.
#
# @api private
class ChapterSlugs
def initialize(chapters)
@chapters = chapters
end

def to_h
used_slugs = Set.new

@chapters.to_h do |chapter|
[chapter.perma_id, slug(chapter, used_slugs)]
end
end

private

def slug(chapter, used_slugs)
slug = Slugify.call(chapter.configuration['title'] || '')
return "chapter-#{chapter.perma_id}" if slug.empty?

slug = "#{slug}-#{chapter.perma_id}" if used_slugs.include?(slug)
used_slugs << slug

slug
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
module PageflowScrolled
# Find signed links to other entries and their chapters and
# sections in configurations.
#
# @api private
class EntryLinkReferences
def initialize(configurations)
@configurations = configurations
end

def chapter_perma_ids_by_entry_id
valid_links
.group_by(&:entry_id)
.transform_values { |links| links.filter_map(&:chapter).uniq }
end

private

Link = Struct.new(:entry_id, :chapter, :section, :signature)

def valid_links
@valid_links ||= links.uniq.select do |link|
EntryLinkSignature.valid?(link.entry_id,
link.signature,
chapter: link.chapter,
section: link.section)
end
end

def links
@configurations.each_with_object([]) do |configuration, result|
each_href(configuration) do |href|
entry_id = to_id(href['entry'])
next unless entry_id

result << Link.new(entry_id,
to_id(href['chapter']),
to_id(href['section']),
href['signature'])
end
end
end

def each_href(value, &block)
case value
when Hash
yield value['href'] if value['href'].is_a?(Hash)
value.each_value { |item| each_href(item, &block) }
when Array
value.each { |item| each_href(item, &block) }
end
end

def to_id(value)
id = Integer(value, exception: false)
id if id&.positive?
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,14 @@ def title
entry.entry_title
end

def storylines
Storyline.all_for_revision(entry.draft)
end

def chapters
Chapter.all_for_revision(entry.draft)
end

def sections
Section.all_for_revision(entry.draft)
end

def content_elements
ContentElement.all_for_revision(entry.draft)
def content
@content ||= RevisionContent.new(entry.draft, include_hidden: true)
end

def copy_fragment_to(fragment_perma_id:, entry:, chapter:)
SectionsCopy
.new(source_entry: self.entry, destination_entry: entry)
.perform(chapters.find_by!(perma_id: fragment_perma_id).sections, chapter:)
.perform(fragment_chapter(fragment_perma_id).sections, chapter:)
end

def extract_fragment_from(entry:, chapter:, title: nil)
Expand All @@ -69,6 +57,11 @@ def extract_fragment_from(entry:, chapter:, title: nil)

private

def fragment_chapter(perma_id)
content.chapters.find { |chapter| chapter.perma_id == perma_id.to_i } ||
raise(ActiveRecord::RecordNotFound)
end

def create_entry(locale:)
Pageflow::DraftEntry.new(
Pageflow::Entry.create!(
Expand All @@ -84,7 +77,7 @@ def create_entry(locale:)
end

def create_fragment_chapter(chapter, title)
storyline = storylines.detect { |candidate| candidate.configuration['main'] }
storyline = content.main_storyline

storyline.chapters.create!(position: (storyline.chapters.maximum(:position) || -1) + 1,
configuration: fragment_configuration(chapter, title))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
module PageflowScrolled
# Entry that editors can link to.
#
# @api private
class LinkableEntry
attr_reader :entry

delegate :id, :title, :type_name, :publication_state, :draft, to: :entry

class << self
def all(scope, except:)
linkable(scope, except).map { |entry| new(entry) }
end

def find(scope, id, except:)
entry = linkable(scope, except).find_by(id:, type_name: 'scrolled')
entry && new(entry)
end

private

def linkable(scope, except)
scope.where(fragment_library: nil).where.not(id: except.id)
end
end

def initialize(entry)
@entry = entry
end

def published_entry
Pageflow::PublishedEntry.new(entry, entry.published_revision || entry.draft)
end

def content
@content ||= RevisionContent.new(draft, include_hidden: true)
end
end
end
37 changes: 37 additions & 0 deletions entry_types/scrolled/app/models/pageflow_scrolled/linked_entry.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
module PageflowScrolled
# Entry that links point to along with the chapters links point to.
#
# @api private
class LinkedEntry
attr_reader :entry, :chapters

delegate :id, :title, :publication_state, to: :entry

class << self
def all(scope, chapter_perma_ids_by_entry_id)
return [] if chapter_perma_ids_by_entry_id.empty?

entries = scope
.where(id: chapter_perma_ids_by_entry_id.keys)
.preload(:published_revision, :draft)
.order(:id)
.to_a
chapter_loader = ChapterLoader.new(entries)

entries.map do |entry|
new(entry,
chapters: chapter_loader.for(entry, chapter_perma_ids_by_entry_id[entry.id]))
end
end
end

def initialize(entry, chapters: [])
@entry = entry
@chapters = chapters
end

def published_entry
Pageflow::PublishedEntry.new(entry, entry.published_revision || entry.draft)
end
end
end
Loading
Loading