diff --git a/lib/active_reporting/dimension.rb b/lib/active_reporting/dimension.rb index 16135c6..eb36f4e 100644 --- a/lib/active_reporting/dimension.rb +++ b/lib/active_reporting/dimension.rb @@ -3,6 +3,7 @@ module ActiveReporting class Dimension TYPES = { degenerate: :degenerate, standard: :standard }.freeze + TIME_COLUMN_TYPES = %i[datetime date].freeze attr_reader :name # @param model [ActiveRecord::Base] @@ -30,11 +31,13 @@ def type end end - # Whether the dimension is a datetime column + # Whether the dimension is a datetime or date column # # @return [Boolean] def datetime? - @datetime ||= type == TYPES[:degenerate] && model.column_for_attribute(@name).type == :datetime + @datetime ||= type == TYPES[:degenerate] && TIME_COLUMN_TYPES.include?( + model.column_for_attribute(@name).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..98ed73c 100644 --- a/test/seed.rb +++ b/test/seed.rb @@ -5,6 +5,7 @@ (1..5).each do |i| user = User.create!( created_at: Time.now - i.months, + birthday_on: (10 + i).years.ago , username: "user_#{i}" )