Skip to content
Merged
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
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ gem 'google-cloud-storage', require: false
gem 'image_processing'

# Authentication
gem 'acts_as_list'
gem 'acts_as_list', '1.2.6'
gem 'acts-as-taggable-on', '12.0.0'
gem 'cocoon'
gem 'csv'
Expand Down
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ GEM
acts-as-taggable-on (12.0.0)
activerecord (>= 7.1, < 8.1)
zeitwerk (>= 2.4, < 3.0)
acts_as_list (1.2.2)
acts_as_list (1.2.6)
activerecord (>= 6.1)
activesupport (>= 6.1)
addressable (2.8.7)
Expand Down Expand Up @@ -807,7 +807,7 @@ PLATFORMS

DEPENDENCIES
acts-as-taggable-on (= 12.0.0)
acts_as_list
acts_as_list (= 1.2.6)
annotate
aws-sdk-s3
azure-storage-blob!
Expand Down
2 changes: 1 addition & 1 deletion app/models/deal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# lost_at :datetime
# lost_reason :string default(""), not null
# name :string default(""), not null
# position :integer default(1), not null
# position :integer
# status :string default("open"), not null
# total_deal_products_amount_in_cents :bigint default(0), not null
# won_at :datetime
Expand Down
2 changes: 1 addition & 1 deletion app/models/stage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#
# id :bigint not null, primary key
# name :string default(""), not null
# position :integer default(1), not null
# position :integer
# created_at :datetime not null
# updated_at :datetime not null
# pipeline_id :bigint not null
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

class RemoveDefaultAndNullFromPositionOnDealsAndStages < ActiveRecord::Migration[7.1]
def change
change_column_default :deals, :position, from: 1, to: nil
change_column_null :deals, :position, true

change_column_default :stages, :position, from: 1, to: nil
change_column_null :stages, :position, true
end
end
6 changes: 3 additions & 3 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

64 changes: 0 additions & 64 deletions spec/controllers/accounts/deals_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
let!(:user) { create(:user) }
let!(:pipeline) { create(:pipeline) }
let!(:stage) { create(:stage, pipeline:) }
let!(:stage_2) { create(:stage, pipeline:, name: 'Stage 2') }
let!(:contact) { create(:contact) }
let(:event) { create(:event, deal:, kind: 'activity') }
let(:last_event) { Event.last }
Expand Down Expand Up @@ -73,69 +72,6 @@
expect(response).to have_http_status(:redirect)
end
end
context 'update deal position and create deal_stage_change event' do
around(:each) do |example|
Sidekiq::Testing.inline! do
example.run
end
end
let!(:deal_stage_1_position_1) { create(:deal, stage:, position: 1) }
let!(:deal_stage_1_position_2) { create(:deal, stage:, position: 2) }
let!(:deal_stage_1_position_3) { create(:deal, stage:, position: 3) }
let!(:deal_stage_2_position_1) { create(:deal, stage: stage_2, position: 1) }
let!(:deal_stage_2_position_2) { create(:deal, stage: stage_2, position: 2) }
let!(:deal_stage_2_position_3) { create(:deal, stage: stage_2, position: 3) }
skip 'between different stages' do
it 'stage 1 position 3 to stage 2 position 1' do
params = { deal: { stage_id: stage_2.id, position: 1 } }

put("/accounts/#{account.id}/deals/#{deal_stage_1_position_3.id}",
params:)
# expect(response).to have_http_status(:success)
expect(deal_stage_1_position_3.reload.position).to eq(1)
expect(deal_stage_1_position_3.reload.stage).to eq(stage_2)
expect(deal_stage_2_position_1.reload.position).to eq(2)
end
it 'stage 1 position 1 to stage 2 position 1' do
params = { deal: { stage_id: stage_2.id, position: 1 } }
expect do
put("/accounts/#{account.id}/deals/#{deal_stage_1_position_1.id}",
params:)
end.to change(Event, :count).by(1)
# expect(response).to have_http_status(:success)
expect(deal_stage_1_position_1.reload.position).to eq(1)
expect(deal_stage_1_position_1.reload.stage).to eq(stage_2)
expect(deal_stage_2_position_1.reload.position).to eq(2)
expect(last_event.kind).to eq('deal_stage_change')
end
end
context 'in the same stage' do
it 'position 3 to position 1' do
params = { deal: { stage_id: stage.id, position: 1 } }
put("/accounts/#{account.id}/deals/#{deal_stage_1_position_3.id}",
params:)
# expect(response).to have_http_status(:success)
expect(deal_stage_1_position_3.reload.position).to eq(1)
expect(deal_stage_1_position_3.reload.stage).to eq(stage)
end
it 'position 1 to position 3' do
params = { deal: { stage_id: stage.id, position: 3 } }
put("/accounts/#{account.id}/deals/#{deal_stage_1_position_1.id}",
params:)
# expect(response).to have_http_status(:success)
expect(deal_stage_1_position_1.reload.position).to eq(3)
expect(deal_stage_1_position_1.reload.stage).to eq(stage)
end
it 'position 2 to position 1' do
params = { deal: { stage_id: stage.id, position: 1 } }
put("/accounts/#{account.id}/deals/#{deal_stage_1_position_2.id}",
params:)
# expect(response).to have_http_status(:success)
expect(deal_stage_1_position_2.reload.position).to eq(1)
expect(deal_stage_1_position_2.reload.stage).to eq(stage)
end
end
end

context 'update status deal' do
it 'update to won and create deal_won event' do
Expand Down
2 changes: 1 addition & 1 deletion spec/factories/deals.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# lost_at :datetime
# lost_reason :string default(""), not null
# name :string default(""), not null
# position :integer default(1), not null
# position :integer
# status :string default("open"), not null
# total_deal_products_amount_in_cents :bigint default(0), not null
# won_at :datetime
Expand Down
2 changes: 1 addition & 1 deletion spec/factories/stages.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#
# id :bigint not null, primary key
# name :string default(""), not null
# position :integer default(1), not null
# position :integer
# created_at :datetime not null
# updated_at :datetime not null
# pipeline_id :bigint not null
Expand Down
134 changes: 134 additions & 0 deletions spec/models/deals_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
require 'rails_helper'

RSpec.describe Deal do
let!(:account) { create(:account) }
let(:last_event) { Event.last }

describe 'position' do
let!(:pipeline) { create(:pipeline) }
let!(:stage1) { create(:stage, pipeline:) }
let!(:stage2) { create(:stage, pipeline:) }

let!(:stage1_position1) { create(:deal, stage: stage1, position: 1) }
let!(:stage1_position2) { create(:deal, stage: stage1, position: 2) }
let!(:stage1_position3) { create(:deal, stage: stage1, position: 3) }

let!(:stage2_position1) { create(:deal, stage: stage2, position: 1) }
let!(:stage2_position2) { create(:deal, stage: stage2, position: 2) }
let!(:stage2_position3) { create(:deal, stage: stage2, position: 3) }

context 'moving between stages' do
it 'move stage1_position1 to stage2 position 1 and create deal_stage_change event' do
expect do
Deal::CreateOrUpdate.new(stage1_position1, { stage: stage2, position: 1 }).call
end.to change(Event, :count).by(1)

expect(stage1_position2.reload.position).to eq 1
expect(stage1_position3.reload.position).to eq 2

expect(stage1_position1.reload.stage_id).to eq stage2.id

expect(stage1_position1.position).to eq 1
expect(stage2_position1.reload.position).to eq 2
expect(stage2_position2.reload.position).to eq 3
expect(stage2_position3.reload.position).to eq 4
expect(last_event.kind).to eq('deal_stage_change')
end
Comment on lines +21 to +36
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Criei esse pr, pois esse teste está falhando.
E se analisar, parece ser um bug da gem act_as_list.
Fiz o update da gem para a ultima versão, mas esse teste ainda está falhando.

Copy link
Collaborator Author

@YukioArie YukioArie Feb 13, 2026

Choose a reason for hiding this comment

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

Abri uma issue no repostorio da gem act_as_list, e o rapaz que me respondeu falou que esse bug acontece pq as positions estão com default: 1. Removendo o default, esse bug é corrigido. Fiz isso nesse commit fdfaf83 e esse teste parou de falhar.
Segue o q o rapaz comentou:
So yes, you do have a default of 1 for that column. You'll need to remove this. It's best if this column doesn't have a default. What is happening is that acts_as_list can't see that the column value is being set explicitly so it's adding the record to the end of the table. Let me know how you get on


it 'move stage1_position1 to stage2 position 2 and create deal_stage_change event' do
expect do
Deal::CreateOrUpdate.new(stage1_position1, { stage: stage2, position: 2 }).call
end.to change(Event, :count).by(1)

expect(stage1_position2.reload.position).to eq 1
expect(stage1_position3.reload.position).to eq 2

expect(stage1_position1.reload.stage_id).to eq stage2.id

expect(stage2_position1.reload.position).to eq 1
expect(stage1_position1.position).to eq 2
expect(stage2_position2.reload.position).to eq 3
expect(stage2_position3.reload.position).to eq 4
expect(last_event.kind).to eq('deal_stage_change')
end

it 'move stage1_position1 to stage2 position 3 and create deal_stage_change event' do
expect do
Deal::CreateOrUpdate.new(stage1_position1, { stage: stage2, position: 3 }).call
end.to change(Event, :count).by(1)

expect(stage1_position2.reload.position).to eq 1
expect(stage1_position3.reload.position).to eq 2

expect(stage1_position1.reload.stage_id).to eq stage2.id

expect(stage2_position1.reload.position).to eq 1
expect(stage2_position2.reload.position).to eq 2
expect(stage1_position1.position).to eq 3
expect(stage2_position3.reload.position).to eq 4
expect(last_event.kind).to eq('deal_stage_change')
end

it 'move stage1_position1 to stage2 position 250 and create deal_stage_change event' do
expect do
Deal::CreateOrUpdate.new(stage1_position1, { stage: stage2, position: 250 }).call
end.to change(Event, :count).by(1)

expect(stage1_position2.reload.position).to eq 1
expect(stage1_position3.reload.position).to eq 2

expect(stage1_position1.reload.stage_id).to eq stage2.id

expect(stage2_position1.reload.position).to eq 1
expect(stage2_position2.reload.position).to eq 2
expect(stage2_position3.reload.position).to eq 3
expect(stage1_position1.position).to eq 250
expect(last_event.kind).to eq('deal_stage_change')
end
end

context 'moving within the same stage' do
it 'move stage1_position1 to position 2' do
Deal::CreateOrUpdate.new(stage1_position1, { position: 2 }).call

expect(stage1_position1.reload.position).to eq 2
expect(stage1_position2.reload.position).to eq 1
expect(stage1_position3.reload.position).to eq 3
end

it 'move stage1_position1 to position 3' do
Deal::CreateOrUpdate.new(stage1_position1, { position: 3 }).call

expect(stage1_position1.reload.position).to eq 3
expect(stage1_position2.reload.position).to eq 1
expect(stage1_position3.reload.position).to eq 2
end
end

context 'create a deal' do
context 'when position is not provided' do
it 'places the new deal at the end of the stage' do
new_deal = build(:deal, stage: stage1)
new_deal = Deal::CreateOrUpdate.new(new_deal, {}).call

expect(stage1_position1.reload.position).to eq 1
expect(stage1_position2.reload.position).to eq 2
expect(stage1_position3.reload.position).to eq 3
expect(new_deal.reload.position).to eq 4
end
end

context 'when position is provided' do
it 'inserts the new deal at the specified position' do
new_deal = build(:deal, stage: stage1, position: 2)
new_deal = Deal::CreateOrUpdate.new(new_deal, {}).call

expect(stage1_position1.reload.position).to eq 1
expect(new_deal.reload.position).to eq 2
expect(stage1_position2.reload.position).to eq 3
expect(stage1_position3.reload.position).to eq 4
end
end
end
end
end
19 changes: 19 additions & 0 deletions spec/models/stage_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
# == Schema Information
#
# Table name: stages
#
# id :bigint not null, primary key
# name :string default(""), not null
# position :integer
# created_at :datetime not null
# updated_at :datetime not null
# pipeline_id :bigint not null
#
# Indexes
#
# index_stages_on_pipeline_id (pipeline_id)
#
# Foreign Keys
#
# fk_rails_... (pipeline_id => pipelines.id)
#
require 'rails_helper'

RSpec.describe Stage do
Expand Down
Loading