diff --git a/lib/active_reporting/dimension.rb b/lib/active_reporting/dimension.rb index 16135c6..7fbcea0 100644 --- a/lib/active_reporting/dimension.rb +++ b/lib/active_reporting/dimension.rb @@ -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 diff --git a/test/active_reporting/report_test.rb b/test/active_reporting/report_test.rb index e62d056..8f6837b 100644 --- a/test/active_reporting/report_test.rb +++ b/test/active_reporting/report_test.rb @@ -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}]) diff --git a/test/fact_models.rb b/test/fact_models.rb index e81f755..31a973a 100644 --- a/test/fact_models.rb +++ b/test/fact_models.rb @@ -39,6 +39,7 @@ class DateDimensionFactModel < ActiveReporting::FactModel class UserFactModel < ActiveReporting::FactModel default_dimension_label :username + dimension :birthday_on dimension :created_at end diff --git a/test/schema.rb b/test/schema.rb index 39b72ef..7905e03 100644 --- a/test/schema.rb +++ b/test/schema.rb @@ -57,6 +57,7 @@ create_table :users, force: true do |t| t.string :username + t.date :birthday_on t.timestamps null: false end diff --git a/test/seed.rb b/test/seed.rb index 7d53e20..6905acb 100644 --- a/test/seed.rb +++ b/test/seed.rb @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- (Date.new(2014,1,1)..Date.new(2017,12,31)).each do |d| DateDimension.new_from_date(d).save! end @@ -5,6 +6,7 @@ (1..5).each do |i| user = User.create!( created_at: Time.now - i.months, + birthday_on: (10 + i).years.ago , username: "user_#{i}" )