Class: Groupdate::RelationBuilder

Inherits:
Object
  • Object
show all
Includes:
SqlServerGroupClause
Defined in:
lib/groupdate/relation_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SqlServerGroupClause

#sql_server_group_clause

Constructor Details

#initialize(relation, column:, period:, time_zone:, time_range:, week_start:, day_start:, series_label:) ⇒ RelationBuilder

Returns a new instance of RelationBuilder.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/groupdate/relation_builder.rb', line 8

def initialize(relation, column:, period:, time_zone:, time_range:, week_start:, day_start:, series_label:)
  @relation = relation
  @column = resolve_column(relation, column)
  @period = period
  @time_zone = time_zone
  @time_range = time_range
  @week_start = week_start
  @day_start = day_start
  @series_label = series_label

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

Instance Attribute Details

#columnObject (readonly)

Returns the value of attribute column.



6
7
8
# File 'lib/groupdate/relation_builder.rb', line 6

def column
  @column
end

#day_startObject (readonly)

Returns the value of attribute day_start.



6
7
8
# File 'lib/groupdate/relation_builder.rb', line 6

def day_start
  @day_start
end

#periodObject (readonly)

Returns the value of attribute period.



6
7
8
# File 'lib/groupdate/relation_builder.rb', line 6

def period
  @period
end

#series_labelObject (readonly)

Returns the value of attribute series_label.



6
7
8
# File 'lib/groupdate/relation_builder.rb', line 6

def series_label
  @series_label
end

#week_startObject (readonly)

Returns the value of attribute week_start.



6
7
8
# File 'lib/groupdate/relation_builder.rb', line 6

def week_start
  @week_start
end

Instance Method Details

#generateObject



23
24
25
26
27
28
# File 'lib/groupdate/relation_builder.rb', line 23

def generate
  group_by = group_clause
  @relation = @relation.group(group_by).where(*where_clause)
  @relation.select_values += ["#{group_by} AS #{series_label}"] if series_label
  @relation
end