Sha256: 20bf9973c8fcd5a3dc769ad3fe65217acc731c6676395c5d2aa240cd1a09a747

Contents?: true

Size: 1008 Bytes

Versions: 2

Compression:

Stored size: 1008 Bytes

Contents

# frozen_string_literal: true
module GroupMetrics
  # Tabulate count of records (within a study week) for a Group.
  class WeeklyCount < ActiveRecord::Base
    self.abstract_class = true

    has_many :memberships,
             foreign_key: :participant_id,
             primary_key: :participant_id

    def self.fetch(group_id)
      pg_timezone = ActiveSupport::TimeZone[Time.zone.name].tzinfo.name

      joins(memberships: :participant)
        .select(<<-SQL
          ( FLOOR ( EXTRACT ( EPOCH FROM (
            ( #{connection.quote_table_name(table_name)}.created_at
              AT TIME ZONE 'UTC' ) AT TIME ZONE #{connection.quote(pg_timezone)}
            - memberships.start_date ) ) / 604800 ) + 1
          )::int AS week, COUNT(1)
        SQL
               )
        .merge(Participant.not_moderator)
        .merge(Membership.where(group_id: group_id))
        .group(:week)
    end

    def week
      attributes["week"]
    end

    def count
      attributes["count"]
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
think_feel_do_engine-3.21.0 app/models/group_metrics/weekly_count.rb
think_feel_do_engine-3.20.1 app/models/group_metrics/weekly_count.rb