Skip to content

Commit 16f93b3

Browse files
committed
Allow time dimensions for date columns
1 parent 8c0dbb9 commit 16f93b3

File tree

5 files changed

+31
-2
lines changed

5 files changed

+31
-2
lines changed

lib/active_reporting/dimension.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
module ActiveReporting
44
class Dimension
55
TYPES = { degenerate: :degenerate, standard: :standard }.freeze
6+
TIME_COLUMN_TYPES = %i[datetime date].freeze
67
attr_reader :name
78

89
# @param model [ActiveRecord::Base]
@@ -30,11 +31,13 @@ def type
3031
end
3132
end
3233

33-
# Whether the dimension is a datetime column
34+
# Whether the dimension is a datetime or date column
3435
#
3536
# @return [Boolean]
3637
def datetime?
37-
@datetime ||= type == TYPES[:degenerate] && model.column_for_attribute(@name).type == :datetime
38+
@datetime ||= type == TYPES[:degenerate] && TIME_COLUMN_TYPES.include?(
39+
model.column_for_attribute(@name).type
40+
)
3841
end
3942

4043
# 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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
(1..5).each do |i|
66
user = User.create!(
77
created_at: Time.now - i.months,
8+
birthday_on: (10 + i).years.ago ,
89
username: "user_#{i}"
910
)
1011

0 commit comments

Comments
 (0)