Class: Screen

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
ActiveModel::ForbiddenAttributesProtection, PublicActivity::Common
Defined in:
app/models/screen.rb

Constant Summary

ONLINE_THRESHOLD =

Scopes

5.minutes
OFFLINE_THRESHOLD =
5.minutes
SCREEN_OWNER_TYPES =

types of entities that may “own” a screen

["User", "Group"]

Instance Method Summary (collapse)

Instance Method Details

- (Object) aspect_ratio

Determine the screen's aspect ratio. If it doesn't exist, calculate it



35
36
37
38
39
40
41
42
43
# File 'app/models/screen.rb', line 35

def aspect_ratio
  if width.nil? || height.nil?
    return { :width=> "", :height=> "" }
  end
  gcd = gcd(width,height)
  aspect_width = width/gcd
  aspect_height = height/gcd
  return {:width => aspect_width, :height => aspect_height }
end

- (Object) gcd(a, b)

Run Euclidean algorithm to find GCD



46
47
48
49
50
51
# File 'app/models/screen.rb', line 46

def gcd (a,b)
  if b == 0
    return a
  end
  return gcd(b, a.modulo(b))
end

- (Boolean) is_offline?

Returns:

  • (Boolean)


68
69
70
# File 'app/models/screen.rb', line 68

def is_offline?
  frontend_updated_at.nil? || frontend_updated_at < (Clock.time - Screen::OFFLINE_THRESHOLD)
end

- (Boolean) is_online?

Returns:

  • (Boolean)


64
65
66
# File 'app/models/screen.rb', line 64

def is_online?
  !frontend_updated_at.nil? && frontend_updated_at >= (Clock.time - Screen::ONLINE_THRESHOLD)
end

- (Object) mark_updated



53
54
55
# File 'app/models/screen.rb', line 53

def mark_updated
  update_column(:frontend_updated_at, Clock.time)
end

- (Object) sometimes_mark_updated(pct = 0.1)

Mark the screen as updated some percentage of the time. Doesn't always mark the screen as updated to avoid flooding the database but does it frequently enought for online / offline detection.



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

def sometimes_mark_updated(pct=0.1)
  mark_updated if rand() <= pct
end