snapshot_concern.rb 1003 B

1234567891011121314151617181920212223242526272829303132333435
  1. # frozen_string_literal: true
  2. module Status::SnapshotConcern
  3. extend ActiveSupport::Concern
  4. included do
  5. has_many :edits, class_name: 'StatusEdit', inverse_of: :status, dependent: :destroy
  6. end
  7. def edited?
  8. edited_at.present?
  9. end
  10. def build_snapshot(account_id: nil, at_time: nil, rate_limit: true)
  11. # We don't use `edits#new` here to avoid it having saved when the
  12. # status is saved, since we want to control that manually
  13. StatusEdit.new(
  14. status_id: id,
  15. text: text,
  16. spoiler_text: spoiler_text,
  17. sensitive: sensitive,
  18. ordered_media_attachment_ids: ordered_media_attachment_ids&.dup || media_attachments.pluck(:id),
  19. media_descriptions: ordered_media_attachments.map(&:description),
  20. poll_options: preloadable_poll&.options&.dup,
  21. account_id: account_id || self.account_id,
  22. created_at: at_time || edited_at,
  23. rate_limit: rate_limit
  24. )
  25. end
  26. def snapshot!(**options)
  27. build_snapshot(**options).save!
  28. end
  29. end