Skip to content

Commit

Permalink
Allow time dimensions for date columns
Browse files Browse the repository at this point in the history
  • Loading branch information
andresgutgon committed Oct 15, 2018
1 parent 8c0dbb9 commit 7a1520a
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/active_reporting/dimension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ def type
#
# @return [Boolean]
def datetime?
@datetime ||= type == TYPES[:degenerate] && model.column_for_attribute(@name).type == :datetime
col_type = model.column_for_attribute(@name).type
@datetime ||= type == TYPES[:degenerate] && %i[datetime date].include?(col_type)
end

# Tells if the dimension is hierarchical
Expand Down
23 changes: 23 additions & 0 deletions test/active_reporting/report_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,29 @@ def test_report_runs_with_an_aggregate_other_than_count
assert data.all? { |r| r.key?('a_metric') }
end

def test_report_runs_with_a_year_grouping
if ENV['DB'] == 'pg'
metric = ActiveReporting::Metric.new(
:a_metric,
fact_model: UserFactModel,
dimensions: [{birthday_on: :year}]
)
report = ActiveReporting::Report.new(metric)
data = report.run
assert data.all? { |r| r.key?('birthday_on_year') }
assert data.size == 5
else
assert_raises ActiveReporting::InvalidDimensionLabel do
metric = ActiveReporting::Metric.new(
:a_metric,
fact_model: UserFactModel,
dimensions: [{birthday_on: :year}]
)
report = ActiveReporting::Report.new(metric)
end
end
end

def test_report_runs_with_a_date_grouping
if ENV['DB'] == 'pg'
metric = ActiveReporting::Metric.new(:a_metric, fact_model: UserFactModel, dimensions: [{created_at: :month}])
Expand Down
1 change: 1 addition & 0 deletions test/fact_models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class DateDimensionFactModel < ActiveReporting::FactModel

class UserFactModel < ActiveReporting::FactModel
default_dimension_label :username
dimension :birthday_on
dimension :created_at
end

Expand Down
1 change: 1 addition & 0 deletions test/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@

create_table :users, force: true do |t|
t.string :username
t.date :birthday_on
t.timestamps null: false
end

Expand Down
2 changes: 2 additions & 0 deletions test/seed.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# -*- coding: utf-8 -*-
(Date.new(2014,1,1)..Date.new(2017,12,31)).each do |d|
DateDimension.new_from_date(d).save!
end

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

Expand Down

0 comments on commit 7a1520a

Please sign in to comment.