Sha256: 43f68fd7bf97055da0583aa454eb3ec3e584b6045bb5ceb9c11b89ebe8fca2d0

Contents?: true

Size: 1.05 KB

Versions: 11

Compression:

Stored size: 1.05 KB

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, timestamp_column = "created_at")
      pg_timezone = ActiveSupport::TimeZone[Time.zone.name].tzinfo.name

      joins(memberships: :participant)
        .select(<<-SQL
          ( FLOOR ( EXTRACT ( EPOCH FROM (
            ( #{connection.quote_table_name(table_name)}.#{connection.quote_table_name(timestamp_column)}
              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

11 entries across 11 versions & 1 rubygems

Version Path
think_feel_do_engine-3.22.9 app/models/group_metrics/weekly_count.rb
think_feel_do_engine-3.22.8 app/models/group_metrics/weekly_count.rb
think_feel_do_engine-3.22.7 app/models/group_metrics/weekly_count.rb
think_feel_do_engine-3.22.6 app/models/group_metrics/weekly_count.rb
think_feel_do_engine-3.22.5 app/models/group_metrics/weekly_count.rb
think_feel_do_engine-3.22.4 app/models/group_metrics/weekly_count.rb
think_feel_do_engine-3.22.2 app/models/group_metrics/weekly_count.rb
think_feel_do_engine-3.22.1 app/models/group_metrics/weekly_count.rb
think_feel_do_engine-3.22.0 app/models/group_metrics/weekly_count.rb
think_feel_do_engine-3.21.2 app/models/group_metrics/weekly_count.rb
think_feel_do_engine-3.21.1 app/models/group_metrics/weekly_count.rb