require_relative 'user_track_queries'

class UserTrack
  extend UserTrack::Queries

  def self.all_for(user)
    totals = exercise_counts_per_track(user.id)
    views = viewed_counts_per_track(user.id)
    track_ids = (totals.keys + views.keys).uniq
    tracks = track_ids.each_with_object([]) do |track_id, user_tracks|
      user_tracks << new(track_id, totals[track_id], views[track_id])
    end
    tracks.sort_by(&:id)
  end

  attr_reader :id, :total, :unread, :name
  def initialize(id, total, viewed)
    @id = id
    @total = total
    @unread = total-viewed
    @name = Language.of(id)
  end
end

require_relative 'user_track_queries'

class UserTrack
  extend UserTrack::Queries

  def self.all_for(user)
    totals = exercise_counts_per_track(user.id)
    views = viewed_counts_per_track(user.id)
    track_ids = (totals.keys + views.keys).uniq
    tracks = track_ids.each_with_object([]) do |track_id, user_tracks|
      user_tracks << new(track_id, totals[track_id], views[track_id])
    end
    tracks.sort_by(&:id)
  end

  attr_reader :id, :total, :unread, :name
  def initialize(id, total, viewed)
    @id = id
    @total = total
    @unread = total.to_i-viewed.to_i
    @name = Language.of(id)
  end
end

Fix potential nil issue in inbox
Fix potential nil issue in inbox

When querying viewed/total exercises for people one of the values might come
back as nil. Or both of them, for that matter. This should ensure that we get
a reasonable default rather than a 500 error.
