Skip to content

Commit 7a1520a

Browse files
committed
Allow time dimensions for date columns
1 parent 8c0dbb9 commit 7a1520a

File tree

5 files changed

+29
-1
lines changed

5 files changed

+29
-1
lines changed

lib/active_reporting/dimension.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ def type
3434
#
3535
# @return [Boolean]
3636
def datetime?
37-
@datetime ||= type == TYPES[:degenerate] && model.column_for_attribute(@name).type == :datetime
37+
col_type = model.column_for_attribute(@name).type
38+
@datetime ||= type == TYPES[:degenerate] && %i[datetime date].include?(col_type)
3839
end
3940

4041
# Tells if the dimension is hierarchical

test/active_reporting/report_test.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,29 @@ def test_report_runs_with_an_aggregate_other_than_count
3232
assert data.all? { |r| r.key?('a_metric') }
3333
end
3434

35+
def test_report_runs_with_a_year_grouping
36+
if ENV['DB'] == 'pg'
37+
metric = ActiveReporting::Metric.new(
38+
:a_metric,
39+
fact_model: UserFactModel,
40+
dimensions: [{birthday_on: :year}]
41+
)
42+
report = ActiveReporting::Report.new(metric)
43+
data = report.run
44+
assert data.all? { |r| r.key?('birthday_on_year') }
45+
assert data.size == 5
46+
else
47+
assert_raises ActiveReporting::InvalidDimensionLabel do
48+
metric = ActiveReporting::Metric.new(
49+
:a_metric,
50+
fact_model: UserFactModel,
51+
dimensions: [{birthday_on: :year}]
52+
)
53+
report = ActiveReporting::Report.new(metric)
54+
end
55+
end
56+
end
57+
3558
def test_report_runs_with_a_date_grouping
3659
if ENV['DB'] == 'pg'
3760
metric = ActiveReporting::Metric.new(:a_metric, fact_model: UserFactModel, dimensions: [{created_at: :month}])

test/fact_models.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class DateDimensionFactModel < ActiveReporting::FactModel
3939

4040
class UserFactModel < ActiveReporting::FactModel
4141
default_dimension_label :username
42+
dimension :birthday_on
4243
dimension :created_at
4344
end
4445

test/schema.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757

5858
create_table :users, force: true do |t|
5959
t.string :username
60+
t.date :birthday_on
6061
t.timestamps null: false
6162
end
6263

test/seed.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
# -*- coding: utf-8 -*-
12
(Date.new(2014,1,1)..Date.new(2017,12,31)).each do |d|
23
DateDimension.new_from_date(d).save!
34
end
45

56
(1..5).each do |i|
67
user = User.create!(
78
created_at: Time.now - i.months,
9+
birthday_on: (10 + i).years.ago ,
810
username: "user_#{i}"
911
)
1012

0 commit comments

Comments
 (0)