Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip-reranking when current_first/current_last is nil #148

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 16 additions & 4 deletions lib/ranked-model/ranker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,22 @@ def rank_at_average(min, max)
end

def assure_unique_position
if ( new_record? || rank_changed? )
if (rank > RankedModel::MAX_RANK_VALUE) || current_at_rank(rank)
rearrange_ranks
end
if ( new_record? || rank_changed? ) && rearrange_ranks?
rearrange_ranks
end
end

def rearrange_ranks?
return true if rank > RankedModel::MAX_RANK_VALUE
_current_at_rank = current_at_rank(rank)
return false if _current_at_rank.nil?
_current_first = current_first
_current_last = current_last
if _current_first.nil? || _current_last.nil?
instance.logger.warn "[RANKED_MODEL] cannot rearrange ranks current_first=#{_current_first.inspect} current_last=#{_current_last.inspect} current_at_rank=#{_current_at_rank.inspect} instance=#{instance.inspect}"
false
else
true
end
end

Expand Down
23 changes: 23 additions & 0 deletions spec/duck-model/duck_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,29 @@

end

describe Duck do
describe "when re-arranging ranks " do
context "cannot be re-arranged" do

it "fails to save" do
patched_duck = Duck.new(
:name => 'Quacky',
:pond => 'Shin',
:age_position => 1,
)
age_ranker = Duck.rankers.detect {|ranker| ranker.name == :age }
Duck.expects(:rankers).once.returns([age_ranker])
patched_duck_ranker = age_ranker.with(patched_duck)
age_ranker.expects(:with).once.returns(patched_duck_ranker)
patched_duck_ranker.expects(:current_first).returns(nil)
patched_duck_ranker.expects(:current_at_rank).returns(:something_truthy)
Copy link
Author

Choose a reason for hiding this comment

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

This is otherwise nil. It's need to be truthy for rearrange_ranks to be called

Copy link
Author

Choose a reason for hiding this comment

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

return value never seems to need to be anything but truthy since it was introduced 8280511

Copy link
Owner

Choose a reason for hiding this comment

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

That's true. Perhaps it was intended for future use. For it to return a boolean you'd probably want to implement a current_at_rank? method.

Copy link
Author

@bf4 bf4 Jul 25, 2019

Choose a reason for hiding this comment

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

@brendon So, the reason I'm looking at current_at_rank at all is because I wanted to reproduce the problem I experienced: creating a record, specifying a position, rearrange ranks failing due to current_first being nil

In order to do that, I discovered that in order for rearrange_ranks to be called in my situation, current_at_rank had to be truthy. I looked at the method and determined, for mocking purposes, I didn't need to care about what it was doing or returning

I was a little curious about current_at_rank since I didn't see it used anywhere else or directly tested. So, I looked through the version history and found as noted above that it was introduced to replace the condition (current_order.find do |rankable| rankable.rank.nil? || rankable.rank == rank end) which confirmed that its usage in assure_unique_position is only to return something truthy.

Interestingly, it's truthiness was replaced by finder.except( :order ).where( ranker.column => rank ).first where 'finder' is more or less equivalent to 'current_order'

now, why would current_at_rank(rank) be truthy when current_first is nil, when current_first is defined as finder.first. I'm not sure, but it could be a race condition or a query cache issue or related to the except call (though I doubt it)

For the purpose of reproducing the failure and testing fixes, I think my test and and prs are sufficient

Copy link
Author

Choose a reason for hiding this comment

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

I'm not sure exactly what the intended meaning of current_at_rank is, or if it changed during the refactor. I think it is that another record exists?

Copy link
Owner

Choose a reason for hiding this comment

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

Let's continue the discussion here: #149


expect(patched_duck.save).to eq(true)
end
end

end
end
describe Duck do

before {
Expand Down