diff options
Diffstat (limited to 'app/models/node.rb')
| -rw-r--r-- | app/models/node.rb | 64 |
1 files changed, 57 insertions, 7 deletions
diff --git a/app/models/node.rb b/app/models/node.rb index 0796ea4..a5a40d3 100644 --- a/app/models/node.rb +++ b/app/models/node.rb | |||
| @@ -442,6 +442,63 @@ class Node < ApplicationRecord | |||
| 442 | end | 442 | end |
| 443 | end | 443 | end |
| 444 | 444 | ||
| 445 | # Attaches an asset to every current lifecycle row -- head, draft and | ||
| 446 | # autosave -- that does not already carry it. Attachments are page- | ||
| 447 | # scoped content (RelatedAsset belongs_to :page; drafts and autosaves | ||
| 448 | # are wholesale clones), so attaching to a single layer is how an | ||
| 449 | # attachment gets silently lost when another layer replaces it at | ||
| 450 | # publish or save. This is the out-of-band counterpart to the | ||
| 451 | # in-editor attach UI; it refuses when someone else holds the editing | ||
| 452 | # lock. Attaching to a head row changes the public page immediately, | ||
| 453 | # by design -- same reasoning as formalizing an already-existing | ||
| 454 | # editorial link. | ||
| 455 | # | ||
| 456 | # headline is a node-level decision: the flag is set on the newly | ||
| 457 | # created joins only when no current row has a headline yet and the | ||
| 458 | # asset is eligible; otherwise the asset is attached plain and the | ||
| 459 | # result says why, so the caller can point the editor at the star in | ||
| 460 | # the editor instead. | ||
| 461 | # | ||
| 462 | # Returns { :attached => n, :already => n, | ||
| 463 | # :headline => nil | :set | :kept_existing | :not_eligible } | ||
| 464 | def attach_asset! asset, user:, headline: false | ||
| 465 | if in_trash? || trash_node? | ||
| 466 | raise ActiveRecord::RecordInvalid.new(self), "Cannot attach assets to a node in the Trash" | ||
| 467 | end | ||
| 468 | |||
| 469 | if lock_owner && lock_owner != user | ||
| 470 | raise( | ||
| 471 | LockedByAnotherUser, | ||
| 472 | "Page is locked by another user who is working on it! " \ | ||
| 473 | "Last modification: #{(self.autosave || self.draft || self.head).updated_at.to_fs(:db)}" | ||
| 474 | ) | ||
| 475 | end | ||
| 476 | |||
| 477 | rows = [head, draft, autosave].compact | ||
| 478 | to_attach = rows.reject { |row| row.related_assets.exists?(:asset_id => asset.id) } | ||
| 479 | |||
| 480 | headline_state = | ||
| 481 | if headline && to_attach.any? | ||
| 482 | if !(asset.image? || asset.pdf?) | ||
| 483 | :not_eligible | ||
| 484 | elsif rows.any? { |row| row.headline_asset.present? } | ||
| 485 | :kept_existing | ||
| 486 | else | ||
| 487 | :set | ||
| 488 | end | ||
| 489 | end | ||
| 490 | |||
| 491 | ActiveRecord::Base.transaction do | ||
| 492 | to_attach.each do |row| | ||
| 493 | row.related_assets.create!(:asset => asset, :headline => headline_state == :set) | ||
| 494 | end | ||
| 495 | end | ||
| 496 | |||
| 497 | { :attached => to_attach.size, | ||
| 498 | :already => rows.size - to_attach.size, | ||
| 499 | :headline => headline_state } | ||
| 500 | end | ||
| 501 | |||
| 445 | def title | 502 | def title |
| 446 | editable_page&.title | 503 | editable_page&.title |
| 447 | end | 504 | end |
| @@ -533,13 +590,6 @@ class Node < ApplicationRecord | |||
| 533 | throw :abort | 590 | throw :abort |
| 534 | end | 591 | end |
| 535 | 592 | ||
| 536 | def self.recently_changed | ||
| 537 | includes(:head).where( | ||
| 538 | "pages.updated_at < ? AND pages.updated_at > ? AND nodes.parent_id IS NOT NULL", | ||
| 539 | Time.now, Time.now - 14.days | ||
| 540 | ).order("pages.updated_at desc").references(:head) | ||
| 541 | end | ||
| 542 | |||
| 543 | protected | 593 | protected |
| 544 | def lock_for! current_user | 594 | def lock_for! current_user |
| 545 | self.lock_owner = current_user | 595 | self.lock_owner = current_user |
