|
| 1 | +module PageflowScrolled |
| 2 | + # @api private |
| 3 | + class FragmentLibrary |
| 4 | + attr_reader :entry, :account |
| 5 | + |
| 6 | + delegate :id, to: :entry |
| 7 | + |
| 8 | + def self.all |
| 9 | + Query.new(Pageflow::Entry.where(fragment_library: 'shared')) |
| 10 | + end |
| 11 | + |
| 12 | + def self.where(...) |
| 13 | + all.where(...) |
| 14 | + end |
| 15 | + |
| 16 | + def self.shared_for_account(account) |
| 17 | + entry = Pageflow::Entry |
| 18 | + .where(account:, type_name: 'scrolled', fragment_library: 'shared') |
| 19 | + .order(:id) |
| 20 | + .first |
| 21 | + |
| 22 | + new(entry && Pageflow::DraftEntry.new(entry), account:) |
| 23 | + end |
| 24 | + |
| 25 | + def initialize(entry, account: entry.account) |
| 26 | + @entry = entry |
| 27 | + @account = account |
| 28 | + end |
| 29 | + |
| 30 | + def persisted? |
| 31 | + entry.present? |
| 32 | + end |
| 33 | + |
| 34 | + def title |
| 35 | + entry.entry_title |
| 36 | + end |
| 37 | + |
| 38 | + def storylines |
| 39 | + Storyline.all_for_revision(entry.draft) |
| 40 | + end |
| 41 | + |
| 42 | + def chapters |
| 43 | + Chapter.all_for_revision(entry.draft) |
| 44 | + end |
| 45 | + |
| 46 | + def sections |
| 47 | + Section.all_for_revision(entry.draft) |
| 48 | + end |
| 49 | + |
| 50 | + def content_elements |
| 51 | + ContentElement.all_for_revision(entry.draft) |
| 52 | + end |
| 53 | + |
| 54 | + def copy_fragment_to(fragment_perma_id:, entry:, chapter:) |
| 55 | + SectionsCopy |
| 56 | + .new(source_entry: self.entry, destination_entry: entry) |
| 57 | + .perform(chapters.find_by!(perma_id: fragment_perma_id).sections, chapter:) |
| 58 | + end |
| 59 | + |
| 60 | + def extract_fragment_from(entry:, chapter:, title: nil) |
| 61 | + ActiveRecord::Base.transaction do |
| 62 | + @entry ||= create_entry(locale: entry.locale) |
| 63 | + |
| 64 | + SectionsCopy |
| 65 | + .new(source_entry: entry, destination_entry: self.entry) |
| 66 | + .perform(chapter.sections, chapter: create_fragment_chapter(chapter, title)) |
| 67 | + end |
| 68 | + end |
| 69 | + |
| 70 | + private |
| 71 | + |
| 72 | + def create_entry(locale:) |
| 73 | + Pageflow::DraftEntry.new( |
| 74 | + Pageflow::Entry.create!( |
| 75 | + account:, |
| 76 | + site: account.default_site, |
| 77 | + type_name: 'scrolled', |
| 78 | + fragment_library: 'shared', |
| 79 | + title: I18n.t('pageflow_scrolled.fragment_library.title', |
| 80 | + account_name: account.name, |
| 81 | + locale: locale == 'de' ? :de : :en) |
| 82 | + ) |
| 83 | + ) |
| 84 | + end |
| 85 | + |
| 86 | + def create_fragment_chapter(chapter, title) |
| 87 | + storyline = storylines.detect { |candidate| candidate.configuration['main'] } |
| 88 | + |
| 89 | + storyline.chapters.create!(position: (storyline.chapters.maximum(:position) || -1) + 1, |
| 90 | + configuration: fragment_configuration(chapter, title)) |
| 91 | + end |
| 92 | + |
| 93 | + def fragment_configuration(chapter, title) |
| 94 | + title = title.presence&.strip |
| 95 | + title ? chapter.configuration.merge('title' => title) : chapter.configuration |
| 96 | + end |
| 97 | + |
| 98 | + # @api private |
| 99 | + class Query |
| 100 | + include Enumerable |
| 101 | + |
| 102 | + def initialize(entries) |
| 103 | + @entries = entries |
| 104 | + end |
| 105 | + |
| 106 | + def model |
| 107 | + FragmentLibrary |
| 108 | + end |
| 109 | + |
| 110 | + def all |
| 111 | + self |
| 112 | + end |
| 113 | + |
| 114 | + def none |
| 115 | + Query.new(@entries.none) |
| 116 | + end |
| 117 | + |
| 118 | + def where(...) |
| 119 | + Query.new(@entries.where(...)) |
| 120 | + end |
| 121 | + |
| 122 | + def merge(entries) |
| 123 | + Query.new(@entries.merge(entries)) |
| 124 | + end |
| 125 | + |
| 126 | + def find(id) |
| 127 | + library(@entries.find(id)) |
| 128 | + end |
| 129 | + |
| 130 | + def each(&) |
| 131 | + @entries.order(:id).map { |entry| library(entry) }.each(&) |
| 132 | + end |
| 133 | + |
| 134 | + private |
| 135 | + |
| 136 | + def library(entry) |
| 137 | + FragmentLibrary.new(Pageflow::DraftEntry.new(entry)) |
| 138 | + end |
| 139 | + end |
| 140 | + end |
| 141 | +end |
0 commit comments