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 16f93b3
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/active_reporting/dimension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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
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
1 change: 1 addition & 0 deletions test/seed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
)

Expand Down

0 comments on commit 16f93b3

Please sign in to comment.