Skip to content

Commit

Permalink
DRYs up activities controller
Browse files Browse the repository at this point in the history
  • Loading branch information
Kostas Sklias committed Dec 12, 2023
1 parent eafee33 commit 023b765
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 59 deletions.
24 changes: 21 additions & 3 deletions app/controllers/activities_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ def index
@page = params[:page].present? ? params[:page].to_i : 1

activities = current_project.activities.includes(:trackable)
activities = activities.filter_by_user_id(params[:user]) if params[:user].present?
activities = activities.filter_by_type(params[:type]) if params[:type].present?
activities = activities.filter_by_date(period_start, period_end) if params[:period_start].present?
activities = filter_activities(activities)
activities = activities.order(created_at: :desc).page(@page)

@activities_groups = activities.group_by do |activity|
Expand Down Expand Up @@ -45,4 +43,24 @@ def period_end
DateTime.parse params[:period_end]
end
end

def filtering_params
params.slice(:user, :type, :period_start, :period_end)
end

def filter_activities(activities)
filtering_params.each do |k, value|
next if value.blank?

if k == 'period_start'
value = DateTime.parse(value).beginning_of_day
elsif k == 'period_end'
value = DateTime.parse(value).end_of_day
end

activities = activities.send("by_#{k}", value)
end

activities
end
end
9 changes: 4 additions & 5 deletions app/models/activity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,10 @@ def project=(new_project); end
scope :latest, -> do
includes(:trackable).order('activities.created_at DESC').limit(20)
end
scope :filter_by_user_id, -> (id) { where user_id: id }
scope :filter_by_type, -> (type) { where trackable_type: type }
scope :filter_by_date, -> (period_start, period_end) {
where('created_at >= ? AND created_at <= ?', period_start.beginning_of_day, period_end.end_of_day)
}
scope :by_user, -> (id) { where user_id: id }
scope :by_type, -> (type) { where trackable_type: type }
scope :by_period_start, -> (period_start) { where('created_at >= ?', period_start) }
scope :by_period_end, -> (period_end) { where('created_at <= ?', period_end) }

# -- Callbacks ------------------------------------------------------------

Expand Down
2 changes: 1 addition & 1 deletion app/views/activities/_filters.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<%= form_with url: project_activities_path(current_project.id), method: :get, remote: true, class: 'd-flex flex-xl-row flex-column gap-3', data: { behavior: 'activity-filters' } do |f| %>
<%= form_with url: project_activities_path(current_project), method: :get, remote: true, class: 'd-flex flex-xl-row flex-column gap-3', data: { behavior: 'activity-filters' } do |f| %>
<div class="d-flex flex-column flex-sm-row w-100 gap-3">
<div class="w-100">
<%= f.label :user, class: 'form-label' %>
Expand Down
6 changes: 3 additions & 3 deletions spec/features/activity_pages/activity_pages_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
expect(page).to have_selector('#user', count: 1)
end

it 'user filter works' do
it 'by user' do
visit project_activities_path(current_project, user: second_user.id)
expect(page).to have_selector('.activity', count: 10)
end
Expand All @@ -114,7 +114,7 @@
expect(page).to have_selector('#type', count: 1)
end

it 'type filter works' do
it 'by type' do
visit project_activities_path(current_project, type: 'Card')
expect(page).to have_selector('.activity', count: 15)
end
Expand All @@ -124,7 +124,7 @@
expect(page).to have_selector('#period_end', count: 1)
end

it 'daterange filter works' do
it 'by date' do
period_start = Time.current - 3.days
period_end = Time.current

Expand Down
89 changes: 42 additions & 47 deletions spec/models/activity_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,71 +25,66 @@
end
end

describe '#filter_by_user_id' do
describe 'filtering' do
before do
card = create(:card)
issue = create(:issue)
@user = create(:user)
activity = create(:activity, trackable: issue, user: @user)
activity2 = create(:activity, trackable: issue, user: @user)
activity3 = create(:activity, trackable: issue)
activity = create(:activity, trackable: issue, user: @user, created_at: DateTime.now.beginning_of_year)
activity2 = create(:activity, trackable: card, user: @user, created_at: DateTime.now.beginning_of_year)
activity3 = create(:activity, trackable: card, created_at: DateTime.now.beginning_of_year - 1.year)
end

context 'when passed a valid user id' do
it 'returns activities of this user' do
expect(Activity.filter_by_user_id(@user.id).count).to eq 2
expect(Activity.all.count).to eq 3
describe '#by_user' do
context 'when passed a valid user id' do
it 'returns activities of this user' do
expect(Activity.by_user(@user.id).count).to eq 2
expect(Activity.all.count).to eq 3
end
end
end

context 'when passed a non valid user id' do
it 'returns an empty collection' do
user_id = User.last.id + 1
expect(Activity.filter_by_user_id(user_id).count).to eq 0
expect(Activity.all.count).to eq 3
context 'when passed a non valid user id' do
it 'returns an empty collection' do
user_id = User.last.id + 1
expect(Activity.by_user(user_id).count).to eq 0
expect(Activity.all.count).to eq 3
end
end
end
end

describe '#filter_by_type' do
before do
issue = create(:issue)
card = create(:card)
activity = create(:activity, trackable: issue)
activity2 = create(:activity, trackable: card)
activity3 = create(:activity, trackable: card)
end

context 'when passed a valid type' do
it 'returns activities of this user' do
expect(Activity.filter_by_type('Card').count).to eq 2
expect(Activity.all.count).to eq 3
describe '#by_type' do
context 'when passed a valid type' do
it 'returns activities of this user' do
expect(Activity.by_type('Card').count).to eq 2
expect(Activity.all.count).to eq 3
end
end
end

context 'when passed a non valid type' do
it 'returns an empty collection' do
expect(Activity.filter_by_type('galaxy').count).to eq 0
expect(Activity.all.count).to eq 3
context 'when passed a non valid type' do
it 'returns an empty collection' do
expect(Activity.by_type('galaxy').count).to eq 0
expect(Activity.all.count).to eq 3
end
end
end
end

describe '#filter_by_date' do
before do
issue = create(:issue)
activity = create(:activity, trackable: issue)
activity2 = create(:activity, trackable: issue, created_at: DateTime.now.beginning_of_year - 1.year)
activity3 = create(:activity, trackable: issue, created_at: DateTime.now.beginning_of_year - 1.year)
describe '#by_period_start' do
context 'when passed a valid date' do
it 'returns activities within the date' do
year_start = DateTime.now.beginning_of_year

expect(Activity.by_period_start(year_start).count).to eq 2
end
end
end

context 'when passed a valid date' do
it 'returns activities within the date' do
year_start = DateTime.now.beginning_of_year
last_year_start = year_start - 1.year
period_end = DateTime.now.end_of_day
describe '#by_period_end' do
context 'when passed a valid date' do
it 'returns activities within the date' do
period_end = DateTime.now.end_of_day - 1.year

expect(Activity.filter_by_date(year_start, period_end).count).to eq 1
expect(Activity.filter_by_date(last_year_start, period_end).count).to eq 3
expect(Activity.by_period_end(period_end).count).to eq 1
end
end
end
end
Expand Down

0 comments on commit 023b765

Please sign in to comment.