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

Fix order bug #261

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions lib/active_hash/relation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,7 @@ def order(*options)
processed_args = preprocess_order_args(options)
candidates = relation.dup

order_by_args!(candidates, processed_args)

candidates
order_by_args(candidates, processed_args)
end

def to_ary
Expand Down Expand Up @@ -179,7 +177,9 @@ def preprocess_order_args(order_args)
ary.map! { |e| e.split(/\W+/) }.reverse!
end

def order_by_args!(candidates, args)
def order_by_args(candidates, args)
ordered_candidates = []

args.each do |arg|
field, dir = if arg.is_a?(Hash)
arg.to_a.flatten.map(&:to_sym)
Expand All @@ -189,14 +189,15 @@ def order_by_args!(candidates, args)
arg.to_sym
end

candidates.sort! do |a, b|
ordered_candidates = candidates.sort do |a, b|
if dir.present? && dir.to_sym.upcase.equal?(:DESC)
b[field] <=> a[field]
else
a[field] <=> b[field]
end
end
end
ordered_candidates
end
end
end
11 changes: 11 additions & 0 deletions spec/active_hash/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -992,6 +992,17 @@ class Region < ActiveHash::Base
expect(countries.first).to eq Country.find_by(name: "Canada")
expect(countries.second).to eq Country.find_by(name: "US")
end

it "doesn't change the order of original records" do
countries = Country.order(id: :desc)
expect(countries.first).to eq Country.find_by(name: "Mexico")
expect(countries.second).to eq Country.find_by(name: "Canada")
expect(countries.third).to eq Country.find_by(name: "US")

expect(Country.all.first).to eq Country.find_by(name: "US")
expect(Country.all.second).to eq Country.find_by(name: "Canada")
expect(Country.all.third).to eq Country.find_by(name: "Mexico")
end
end

describe "#method_missing" do
Expand Down
16 changes: 16 additions & 0 deletions spec/active_hash/relation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,20 @@
expect(array.size).to eq(2)
end
end

describe '#find' do
it 'returns a correct record' do
expect(subject.find(1).attributes).to eq(model_class.data.select{|e| e[:id] == 1}.first)
end

context 'when data ordered' do
before do
model_class.order(id: "DESC")
end

it 'returns a correct record' do
expect(subject.find(1).attributes).to eq(model_class.data.select{|e| e[:id] == 1}.first)
end
end
end
end