Commit 64779366 authored by Douglas Barbosa Alexandre's avatar Douglas Barbosa Alexandre Committed by Robert Speicher

Update last_repository_updated_at when a push event is created

parent 8df81668
...@@ -30,6 +30,7 @@ class Event < ActiveRecord::Base ...@@ -30,6 +30,7 @@ class Event < ActiveRecord::Base
# Callbacks # Callbacks
after_create :reset_project_activity after_create :reset_project_activity
after_create :set_last_repository_updated_at, if: :push?
# Scopes # Scopes
scope :recent, -> { reorder(id: :desc) } scope :recent, -> { reorder(id: :desc) }
...@@ -352,12 +353,6 @@ class Event < ActiveRecord::Base ...@@ -352,12 +353,6 @@ class Event < ActiveRecord::Base
Project.unscoped.where(id: project_id). Project.unscoped.where(id: project_id).
where('last_activity_at <= ?', RESET_PROJECT_ACTIVITY_INTERVAL.ago). where('last_activity_at <= ?', RESET_PROJECT_ACTIVITY_INTERVAL.ago).
update_all(last_activity_at: created_at) update_all(last_activity_at: created_at)
if push?
Project.unscoped.where(id: project_id).
where('last_repository_updated_at <= ?', RESET_PROJECT_ACTIVITY_INTERVAL.ago).
update_all(last_repository_updated_at: created_at)
end
end end
def authored_by?(user) def authored_by?(user)
...@@ -369,4 +364,9 @@ class Event < ActiveRecord::Base ...@@ -369,4 +364,9 @@ class Event < ActiveRecord::Base
def recent_update? def recent_update?
project.last_activity_at > RESET_PROJECT_ACTIVITY_INTERVAL.ago project.last_activity_at > RESET_PROJECT_ACTIVITY_INTERVAL.ago
end end
def set_last_repository_updated_at
Project.unscoped.where(id: project_id).
update_all(last_repository_updated_at: created_at)
end
end end
...@@ -15,15 +15,41 @@ describe Event, models: true do ...@@ -15,15 +15,41 @@ describe Event, models: true do
end end
describe 'Callbacks' do describe 'Callbacks' do
describe 'after_create :reset_project_activity' do
let(:project) { create(:empty_project) } let(:project) { create(:empty_project) }
describe 'after_create :reset_project_activity' do
it 'calls the reset_project_activity method' do it 'calls the reset_project_activity method' do
expect_any_instance_of(described_class).to receive(:reset_project_activity) expect_any_instance_of(described_class).to receive(:reset_project_activity)
create_push_event(project, project.owner) create_push_event(project, project.owner)
end end
end end
describe 'after_create :set_last_repository_updated_at' do
context 'with a push event' do
it 'updates the project last_repository_updated_at' do
project.update(last_repository_updated_at: 1.year.ago)
create_push_event(project, project.owner)
project.reload
expect(project.last_repository_updated_at).to be_within(1.minute).of(Time.now)
end
end
context 'without a push event' do
it 'does not update the project last_repository_updated_at' do
project.update(last_repository_updated_at: 1.year.ago)
create(:closed_issue_event, project: project, author: project.owner)
project.reload
expect(project.last_repository_updated_at).to be_within(1.minute).of(1.year.ago)
end
end
end
end end
describe "Push event" do describe "Push event" do
...@@ -243,38 +269,19 @@ describe Event, models: true do ...@@ -243,38 +269,19 @@ describe Event, models: true do
expect(project).not_to receive(:update_column). expect(project).not_to receive(:update_column).
with(:last_activity_at, a_kind_of(Time)) with(:last_activity_at, a_kind_of(Time))
expect(project).not_to receive(:update_column).
with(:last_repository_updated_at, a_kind_of(Time))
create_push_event(project, project.owner) create_push_event(project, project.owner)
end end
end end
context 'when a project was updated more than 1 hour ago' do context 'when a project was updated more than 1 hour ago' do
context 'with a push event' do it 'updates the project' do
it 'updates the project last_activity_at and last_repository_updated_at' do project.update(last_activity_at: 1.year.ago)
project.update(last_activity_at: 1.year.ago, last_repository_updated_at: 1.year.ago)
create_push_event(project, project.owner) create_push_event(project, project.owner)
project.reload project.reload
expect(project.last_activity_at).to be_within(1.minute).of(Time.now) expect(project.last_activity_at).to be_within(1.minute).of(Time.now)
expect(project.last_repository_updated_at).to be_within(1.minute).of(Time.now)
end
end
context 'without a push event' do
it 'does not update the project last_repository_updated_at' do
project.update(last_activity_at: 1.year.ago, last_repository_updated_at: 1.year.ago)
create(:closed_issue_event, project: project, author: project.owner)
project.reload
expect(project.last_activity_at).to be_within(1.minute).of(Time.now)
expect(project.last_repository_updated_at).to be_within(1.minute).of(1.year.ago)
end
end end
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment