-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement work report query builder (#21)
- Loading branch information
Nick Flückiger
authored
May 5, 2021
1 parent
d77a3b2
commit 0d7e2a1
Showing
9 changed files
with
138 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# frozen_string_literal: true | ||
|
||
class WorkReportQuery | ||
include ActiveModel::Model | ||
extend ActiveModel::Naming | ||
|
||
attr_accessor :period, :date | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,52 @@ | ||
# frozen_string_literal: true | ||
|
||
class WorkReportQueryBuilder | ||
include Rails.application.routes.url_helpers | ||
|
||
AVAILABLE_PERIODS = %i[day week month year].freeze | ||
DATE_FORMAT_FOR_QUERY = '%Y-%m-%d' | ||
COLUMNS_TO_QUERY = %w[project spent_on activity issue comments hours].freeze | ||
BETWEEN_OPERATOR = '><' | ||
FILTERS_TO_USE = ['spent_on', 'user_id', ''].freeze | ||
QUERY_CURRENT_USER_SYMBOL = 'me' | ||
FILTER_STATUS = '1' | ||
|
||
def initialize(work_report_query) | ||
@work_report_query = work_report_query | ||
@date = Date.parse(@work_report_query.date) | ||
end | ||
|
||
def initialize(date, period: :week) | ||
@date = date | ||
@period = period | ||
def build_query | ||
date_range = build_date_range.map { |date| date.strftime(DATE_FORMAT_FOR_QUERY) } | ||
build_report_query(date_range) | ||
end | ||
|
||
def build_report_query; end | ||
private | ||
|
||
def build_report_query(date_range) | ||
time_entries_path( | ||
set_filter: FILTER_STATUS, sort: 'spent_on:desc', | ||
f: FILTERS_TO_USE, | ||
op: { spent_on: BETWEEN_OPERATOR, user_id: '=' }, | ||
v: { spent_on: [date_range.first, date_range.last], user_id: [QUERY_CURRENT_USER_SYMBOL] }, | ||
c: COLUMNS_TO_QUERY, group_by: '', t: ['hours'] | ||
) | ||
end | ||
|
||
def valid? | ||
AVAILABLE_PERIODS.include?(@work_report_query.period) | ||
end | ||
|
||
def build_date_range | ||
case @work_report_query.period.to_sym | ||
when :day | ||
[@date, @date] | ||
when :week | ||
[@date.beginning_of_week, @date.end_of_week] | ||
when :month | ||
[@date.beginning_of_month, @date.end_of_month] | ||
when :year | ||
[@date.beginning_of_year, @date.end_of_year] | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
.box | ||
= form_for :entry_report_query do |f| | ||
= f.date_field :date | ||
- current_report_query = current_report || WorkReportQuery.new | ||
= labelled_form_for(current_report_query, url: timer_sessions_report_path, method: :get) do |f| | ||
= f.date_field :date, value: Time.zone.now.to_date | ||
br | ||
= f.select :period, %i[day week month year] | ||
br | ||
= f.submit :see_query |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
require File.expand_path('../test_helper', __dir__) | ||
|
||
class WorkReportQueryBuilderTest < ActiveSupport::TestCase | ||
test 'build query for day report' do | ||
work_report_query = WorkReportQuery.new( | ||
date: Date.new(2021, 10, 30).to_s, | ||
period: :day | ||
) | ||
expected_query = URI.decode("/time_entries?c%5B%5D=project&c%5B%5D=spent_on"+ | ||
"&c%5B%5D=activity&c%5B%5D=issue&c%5B%5D=comments"+ | ||
"&c%5B%5D=hours&f%5B%5D=spent_on&f%5B%5D=user_id"+ | ||
"&f%5B%5D=&group_by=&op%5Bspent_on%5D=%3E%3C&op%5Buser_id"+ | ||
"%5D=%3D&set_filter=1&sort=spent_on%3Adesc&t%5B%5D=hours&v%5Bspent_on"+ | ||
"%5D%5B%5D=2021-10-30&v%5Bspent_on%5D%5B%5D=2021-10-30&v%5Buser_id%5D%5B%5D=me") | ||
assert_equal expected_query, URI.decode(WorkReportQueryBuilder.new(work_report_query).build_query) | ||
end | ||
|
||
test 'build query for week report' do | ||
work_report_query = WorkReportQuery.new( | ||
date: Date.new(2021, 10, 30).to_s, | ||
period: :week | ||
) | ||
expected_query = URI.decode("/time_entries?c%5B%5D=project&c%5B%5D=spent_on"+ | ||
"&c%5B%5D=activity&c%5B%5D=issue&c%5B%5D=comments"+ | ||
"&c%5B%5D=hours&f%5B%5D=spent_on&f%5B%5D=user_id"+ | ||
"&f%5B%5D=&group_by=&op%5Bspent_on%5D=%3E%3C&op%5Buser_id"+ | ||
"%5D=%3D&set_filter=1&sort=spent_on%3Adesc&t%5B%5D=hours&v%5Bspent_on"+ | ||
"%5D%5B%5D=2021-10-25&v%5Bspent_on%5D%5B%5D=2021-10-31&v%5Buser_id%5D%5B%5D=me") | ||
assert_equal expected_query, URI.decode(WorkReportQueryBuilder.new(work_report_query).build_query) | ||
end | ||
|
||
test 'build query for month report' do | ||
work_report_query = WorkReportQuery.new( | ||
date: Date.new(2021, 10, 30).to_s, | ||
period: :month | ||
) | ||
expected_query = URI.decode("/time_entries?c%5B%5D=project&c%5B%5D=spent_on"+ | ||
"&c%5B%5D=activity&c%5B%5D=issue&c%5B%5D=comments"+ | ||
"&c%5B%5D=hours&f%5B%5D=spent_on&f%5B%5D=user_id"+ | ||
"&f%5B%5D=&group_by=&op%5Bspent_on%5D=%3E%3C&op%5Buser_id"+ | ||
"%5D=%3D&set_filter=1&sort=spent_on%3Adesc&t%5B%5D=hours&v%5Bspent_on"+ | ||
"%5D%5B%5D=2021-10-01&v%5Bspent_on%5D%5B%5D=2021-10-31&v%5Buser_id%5D%5B%5D=me") | ||
assert_equal expected_query, URI.decode(WorkReportQueryBuilder.new(work_report_query).build_query) | ||
end | ||
|
||
test 'build query for year report' do | ||
work_report_query = WorkReportQuery.new( | ||
date: Date.new(2021, 10, 30).to_s, | ||
period: :year | ||
) | ||
expected_query = URI.decode("/time_entries?c%5B%5D=project&c%5B%5D=spent_on"+ | ||
"&c%5B%5D=activity&c%5B%5D=issue&c%5B%5D=comments"+ | ||
"&c%5B%5D=hours&f%5B%5D=spent_on&f%5B%5D=user_id"+ | ||
"&f%5B%5D=&group_by=&op%5Bspent_on%5D=%3E%3C&op%5Buser_id"+ | ||
"%5D=%3D&set_filter=1&sort=spent_on%3Adesc&t%5B%5D=hours&v%5Bspent_on"+ | ||
"%5D%5B%5D=2021-01-01&v%5Bspent_on%5D%5B%5D=2021-12-31&v%5Buser_id%5D%5B%5D=me") | ||
assert_equal expected_query, URI.decode(WorkReportQueryBuilder.new(work_report_query).build_query) | ||
end | ||
end |