Class: BroadcastMessage

Inherits:
ApplicationRecord show all
Includes:
CacheMarkdownField, Sortable
Defined in:
app/models/broadcast_message.rb

Constant Summary collapse

CACHE_KEY =
'broadcast_message_current_json'
'broadcast_message_current_banner_json'
NOTIFICATION_CACHE_KEY =
'broadcast_message_current_notification_json'

Constants included from CacheMarkdownField

CacheMarkdownField::INVALIDATED_BY

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CacheMarkdownField

#attribute_invalidated?, #banzai_render_context, #cached_html_for, #cached_html_up_to_date?, #can_cache_field?, #invalidated_markdown_cache?, #latest_cached_markdown_version, #local_version, #parent_user, #refresh_markdown_cache, #refresh_markdown_cache!, #rendered_field_content, #skip_project_check?, #updated_cached_html_for

Methods inherited from ApplicationRecord

at_most, id_in, id_not_in, iid_in, pluck_primary_key, primary_key_in, safe_ensure_unique, safe_find_or_create_by, safe_find_or_create_by!, underscore, without_order

Class Method Details

.cacheObject


54
55
56
57
58
# File 'app/models/broadcast_message.rb', line 54

def cache
  ::Gitlab::SafeRequestStore.fetch(:broadcast_message_json_cache) do
    Gitlab::JsonCache.new(cache_key_with_version: false)
  end
end

.cache_expires_inObject


60
61
62
# File 'app/models/broadcast_message.rb', line 60

def cache_expires_in
  2.weeks
end

.current(current_path = nil) ⇒ Object


44
45
46
47
48
# File 'app/models/broadcast_message.rb', line 44

def current(current_path = nil)
  fetch_messages CACHE_KEY, current_path do
    current_and_future_messages
  end
end

.current_and_future_messagesObject


50
51
52
# File 'app/models/broadcast_message.rb', line 50

def current_and_future_messages
  where('ends_at > :now', now: Time.current).order_id_asc
end

.current_banner_messages(current_path = nil) ⇒ Object


32
33
34
35
36
# File 'app/models/broadcast_message.rb', line 32

def current_banner_messages(current_path = nil)
  fetch_messages BANNER_CACHE_KEY, current_path do
    current_and_future_messages.banner
  end
end

.current_notification_messages(current_path = nil) ⇒ Object


38
39
40
41
42
# File 'app/models/broadcast_message.rb', line 38

def current_notification_messages(current_path = nil)
  fetch_messages NOTIFICATION_CACHE_KEY, current_path do
    current_and_future_messages.notification
  end
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)

81
82
83
# File 'app/models/broadcast_message.rb', line 81

def active?
  started? && !ended?
end

#ended?Boolean

Returns:

  • (Boolean)

89
90
91
# File 'app/models/broadcast_message.rb', line 89

def ended?
  ends_at < Time.current
end

#flush_redis_cacheObject


114
115
116
117
118
# File 'app/models/broadcast_message.rb', line 114

def flush_redis_cache
  [CACHE_KEY, BANNER_CACHE_KEY, NOTIFICATION_CACHE_KEY].each do |key|
    self.class.cache.expire(key)
  end
end

#future?Boolean

Returns:

  • (Boolean)

97
98
99
# File 'app/models/broadcast_message.rb', line 97

def future?
  starts_at > Time.current
end

#matches_current_path(current_path) ⇒ Object


105
106
107
108
109
110
111
112
# File 'app/models/broadcast_message.rb', line 105

def matches_current_path(current_path)
  return true if current_path.blank? || target_path.blank?

  escaped = Regexp.escape(target_path).gsub('\\*', '.*')
  regexp = Regexp.new "^#{escaped}$", Regexp::IGNORECASE

  regexp.match(current_path)
end

#now?Boolean

Returns:

  • (Boolean)

93
94
95
# File 'app/models/broadcast_message.rb', line 93

def now?
  (starts_at..ends_at).cover?(Time.current)
end

#now_or_future?Boolean

Returns:

  • (Boolean)

101
102
103
# File 'app/models/broadcast_message.rb', line 101

def now_or_future?
  now? || future?
end

#started?Boolean

Returns:

  • (Boolean)

85
86
87
# File 'app/models/broadcast_message.rb', line 85

def started?
  Time.current >= starts_at
end