Class: GlobalAlerts::Alert
- Inherits:
-
Object
- Object
- GlobalAlerts::Alert
- Includes:
- ActiveModel::Model
- Defined in:
- app/models/global_alerts/alert.rb
Constant Summary collapse
- CACHE_KEY =
'global-alerts'
Instance Attribute Summary collapse
-
#application_name ⇒ Object
Returns the value of attribute application_name.
-
#from ⇒ Object
Returns the value of attribute from.
-
#html ⇒ Object
Returns the value of attribute html.
-
#to ⇒ Object
Returns the value of attribute to.
Class Method Summary collapse
- .active(time = GlobalAlerts::Alert.global_alert_time) ⇒ Object
- .all ⇒ Object
- .cache ⇒ Object
- .global_alert_time ⇒ Object
- .global_alert_time=(time) ⇒ Object
Instance Method Summary collapse
Instance Attribute Details
#application_name ⇒ Object
Returns the value of attribute application_name.
52 53 54 |
# File 'app/models/global_alerts/alert.rb', line 52 def application_name @application_name end |
#from ⇒ Object
Returns the value of attribute from.
52 53 54 |
# File 'app/models/global_alerts/alert.rb', line 52 def from @from end |
#html ⇒ Object
Returns the value of attribute html.
52 53 54 |
# File 'app/models/global_alerts/alert.rb', line 52 def html @html end |
#to ⇒ Object
Returns the value of attribute to.
52 53 54 |
# File 'app/models/global_alerts/alert.rb', line 52 def to @to end |
Class Method Details
.active(time = GlobalAlerts::Alert.global_alert_time) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 |
# File 'app/models/global_alerts/alert.rb', line 36 def self.active(time = GlobalAlerts::Alert.global_alert_time) active_alert = all.find do |alert| alert.active?(time: time, for_application: GlobalAlerts::Engine.config.application_name) end active_alert ||= all.find do |alert| alert.active?(time: time) end active_alert end |
.all ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'app/models/global_alerts/alert.rb', line 17 def self.all return to_enum(:all) unless block_given? body = cache.fetch(CACHE_KEY) do HTTP.follow.get(GlobalAlerts::Engine.config.url).body.to_s end data = YAML.safe_load(body) data.dig('alerts')&.each do |yaml| yield new(yaml) end rescue => e Rails.logger.warn(e) raise(e) if Rails.env.development? [] end |
.cache ⇒ Object
48 49 50 |
# File 'app/models/global_alerts/alert.rb', line 48 def self.cache GlobalAlerts::Engine.config.cache || Rails.cache end |
.global_alert_time ⇒ Object
9 10 11 |
# File 'app/models/global_alerts/alert.rb', line 9 def self.global_alert_time @global_alert_time || Time.zone.now end |
.global_alert_time=(time) ⇒ Object
13 14 15 |
# File 'app/models/global_alerts/alert.rb', line 13 def self.global_alert_time=(time) @global_alert_time = time end |
Instance Method Details
#active?(time: Time.zone.now, for_application: nil) ⇒ Boolean
56 57 58 59 60 61 62 |
# File 'app/models/global_alerts/alert.rb', line 56 def active?(time: Time.zone.now, for_application: nil) return false if for_application != application_name return true if from.nil? && to.nil? range.cover?(time) end |
#as_html ⇒ Object
72 73 74 |
# File 'app/models/global_alerts/alert.rb', line 72 def as_html html.html_safe end |
#range ⇒ Object
64 65 66 67 68 69 70 |
# File 'app/models/global_alerts/alert.rb', line 64 def range start_of_range = from.presence && Time.zone.parse(from) start_of_range ||= Time.at(0) unless RUBY_VERSION > '2.7' end_of_range = to.presence && Time.zone.parse(to) start_of_range...end_of_range end |