Skip to content

Commit

Permalink
Updates are never updated!
Browse files Browse the repository at this point in the history
  • Loading branch information
hennevogel committed Feb 11, 2020
1 parent bb03be6 commit a519845
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
2 changes: 1 addition & 1 deletion app/models/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Project < ApplicationRecord
has_many :project_interests
has_many :keywords, through: :project_interests

has_many :updates, dependent: :destroy
has_many :updates, -> { order 'created_at DESC' }, dependent: :destroy

has_many :comments, as: :commentable, dependent: :destroy

Expand Down
2 changes: 1 addition & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class User < ApplicationRecord
validates_uniqueness_of :email

has_many :originated_projects, foreign_key: 'originator_id', class_name: 'Project'
has_many :updates, -> { order 'updated_at DESC' }, foreign_key: 'author_id', dependent: :destroy
has_many :updates, -> { order 'created_at DESC' }, foreign_key: 'author_id', dependent: :destroy

has_many :memberships
has_many :comments, foreign_key: 'commenter_id'
Expand Down
14 changes: 9 additions & 5 deletions spec/models/project_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@
before do
@project = FactoryBot.create(:project)
@idea = FactoryBot.create(:idea)
# We sort updates by updated_at, make sure it's different from the creation updates
sleep 1
@user = FactoryBot.create(:user)
@project.join!(@user)
@idea.join!(@user)
Expand All @@ -78,21 +80,23 @@
end

it 'creates an Update object assigned to the user' do
expect(@project.updates.last.author).to eq(@user)
expect(@project.updates.first.author).to eq(@user)
end

context 'when project has no users (state == idea)' do
it { expect(@idea.updates.last.text).to eq('started') }
it { expect(@idea.updates.first.text).to eq('started') }
end

context 'when project has a user (state == project)' do
it { expect(@project.updates.last.text).to eq('joined') }
it { expect(@project.updates.first.text).to eq('joined') }
end
end

describe 'leave!' do
before do
@project = FactoryBot.create(:project)
# We sort updates by updated_at, make sure it's different from the creation updates
sleep 1
@user = @project.users.first
@project.leave!(@user)
end
Expand All @@ -102,8 +106,8 @@
end

it 'creates an Update for the user with text "left"' do
expect(@project.updates.last.author).to eq(@user)
expect(@project.updates.last.text).to eq('left')
expect(@project.updates.first.author).to eq(@user)
expect(@project.updates.first.text).to eq('left')
end

context 'when the removed user was the last one' do
Expand Down

0 comments on commit a519845

Please sign in to comment.