class Groupdate::Adapters::BaseAdapter

Attributes

column[R]
day_start[R]
n_seconds[R]
period[R]
week_start[R]

Public Class Methods

new(relation, column:, period:, time_zone:, time_range:, week_start:, day_start:, n_seconds:, adapter_name: nil) click to toggle source
# File lib/groupdate/adapters/base_adapter.rb, line 6
def initialize(relation, column:, period:, time_zone:, time_range:, week_start:, day_start:, n_seconds:, adapter_name: nil)
  @relation = relation
  @column = column
  @period = period
  @time_zone = time_zone
  @time_range = time_range
  @week_start = week_start
  @day_start = day_start
  @n_seconds = n_seconds
  @adapter_name = adapter_name

  if ActiveRecord::VERSION::MAJOR >= 7
    if ActiveRecord.default_timezone == :local
      raise Groupdate::Error, "ActiveRecord.default_timezone must be :utc to use Groupdate"
    end
  else
    if relation.default_timezone == :local
      raise Groupdate::Error, "ActiveRecord::Base.default_timezone must be :utc to use Groupdate"
    end
  end
end

Public Instance Methods

generate() click to toggle source
# File lib/groupdate/adapters/base_adapter.rb, line 28
def generate
  @relation.group(group_clause).where(*where_clause)
end

Private Instance Methods

where_clause() click to toggle source
# File lib/groupdate/adapters/base_adapter.rb, line 34
def where_clause
  if @time_range.is_a?(Range)
    if @time_range.end
      op = @time_range.exclude_end? ? "<" : "<="
      if @time_range.begin
        ["#{column} >= ? AND #{column} #{op} ?", @time_range.begin, @time_range.end]
      else
        ["#{column} #{op} ?", @time_range.end]
      end
    else
      ["#{column} >= ?", @time_range.begin]
    end
  else
    ["#{column} IS NOT NULL"]
  end
end