Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow time dimensions for date columns #20

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this not work with MySQL as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did a helper function in the other pr, I can change here after merge the other.

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