can_be_published.rb 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. module CanBePublished
  3. extend ActiveSupport::Concern
  4. def can_be_published_aasm
  5. @can_be_published_aasm ||= StateMachine.new(self)
  6. end
  7. def visible?
  8. can_be_published_aasm.published?
  9. end
  10. def visible_internally?
  11. can_be_published_aasm.internal? || visible?
  12. end
  13. class_methods do
  14. def inverse_relation_name(scope_name)
  15. "can_be_published_#{scope_name}_#{model_name.plural}"
  16. end
  17. end
  18. included do
  19. validate :archived_after_internal
  20. validate :archived_after_published
  21. validate :published_after_internal
  22. before_save :update_user_references
  23. after_save :schedule_touch
  24. after_save :update_active_publicly
  25. after_destroy :update_active_publicly
  26. after_touch :update_active_publicly
  27. %i[archived published internal].each do |scope_name|
  28. local = :"#{scope_name}_by"
  29. remote = inverse_relation_name(scope_name).to_sym
  30. belongs_to local, class_name: 'User', inverse_of: remote, optional: true
  31. # Deletion of users is handled in User.destroy_move_dependency_ownership and resets fields to user_id: 1, so skip dependent: here.
  32. User.has_many remote, class_name: model_name, inverse_of: local, foreign_key: "#{local}_id" # rubocop:disable Rails/HasManyOrHasOneDependent
  33. User.association_attributes_ignored remote
  34. end
  35. scope :published, lambda {
  36. timestamp = Time.zone.now
  37. date_earlier(:published_at, timestamp).date_later_or_nil(:archived_at, timestamp)
  38. }
  39. scope :archived, lambda {
  40. timestamp = Time.zone.now
  41. date_earlier(:archived_at, timestamp)
  42. }
  43. scope :only_internal, lambda {
  44. timestamp = Time.zone.now
  45. date_earlier(:internal_at, timestamp)
  46. .date_later_or_nil(:archived_at, timestamp)
  47. .date_later_or_nil(:published_at, timestamp)
  48. }
  49. scope :internal, lambda {
  50. timestamp = Time.zone.now
  51. internal = arel_table[:internal_at].lt(timestamp)
  52. published = arel_table[:published_at].lt(timestamp)
  53. where(internal.or(published))
  54. .date_later_or_nil(:archived_at, timestamp)
  55. }
  56. scope :date_earlier, lambda { |field, timestamp|
  57. where arel_table[field].lt(timestamp)
  58. }
  59. scope :date_later_or_nil, lambda { |field, timestamp|
  60. where arel_table[field].gt(timestamp).or(arel_table[field].eq(nil))
  61. }
  62. end
  63. def update_user_references
  64. return if can_be_published_aasm.aasm.current_event.present? # state machine is handling it
  65. %i[archived internal published].each do |scope_name|
  66. update_user_reference_item(scope_name)
  67. end
  68. end
  69. def update_user_reference_item(scope_name)
  70. return if !send(:"#{scope_name}_at_changed?")
  71. send(:"#{scope_name}_by_id=", UserInfo.current_user_id)
  72. end
  73. def archived_after_internal
  74. return if internal_at.nil? || archived_at.nil? || archived_at >= internal_at
  75. errors.add(:archived_at, __('date must be no earlier than internal date'))
  76. end
  77. def archived_after_published
  78. return if published_at.nil? || archived_at.nil? || archived_at >= published_at
  79. errors.add(:archived_at, __('date must be no earlier than published date'))
  80. end
  81. def published_after_internal
  82. return if published_at.nil? || internal_at.nil? || published_at >= internal_at
  83. errors.add(:published_at, __('date must be no earlier than internal date'))
  84. end
  85. def schedule_touch_for(attr)
  86. date = saved_changes[attr]&.last
  87. return if date.nil? || date <= Time.zone.now
  88. ScheduledTouchJob.touch_at(self, date)
  89. end
  90. def schedule_touch
  91. %i[published_at archived_at].each { |attr| schedule_touch_for(attr) }
  92. end
  93. def update_active_publicly
  94. CanBePublished.update_active_publicly!
  95. end
  96. def self.update_active_publicly!
  97. Setting.set('kb_active_publicly', active_publicly?)
  98. end
  99. def self.active_publicly?
  100. KnowledgeBase::Answer
  101. .published
  102. .joins(category: :knowledge_base)
  103. .exists?(knowledge_bases: { active: true })
  104. end
  105. end