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 Rails 3 doesn't have #none method #2

Merged
merged 8 commits into from
Feb 22, 2017
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 find_with_order.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ Gem::Specification.new do |spec|

spec.add_development_dependency "bundler", "~> 1.11"
spec.add_development_dependency "rake", "~> 12.0"
spec.add_development_dependency "mysql2", "~> 0.3.21"
spec.add_development_dependency "minitest", "~> 5.0"

spec.add_dependency "rails", ">= 3"
spec.add_dependency "mysql2", ">= 0.3"

end
1 change: 1 addition & 0 deletions gemfiles/3.2.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ source 'https://rubygems.org'
gem "activerecord", "~> 3.2"

group :test do
gem 'mysql2' , '0.3.21'
gem "simplecov"
gem "codeclimate-test-reporter", "~> 1.0.0"
end
Expand Down
1 change: 1 addition & 0 deletions gemfiles/4.2.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ source 'https://rubygems.org'
gem "activerecord", "~> 4.2"

group :test do
gem 'mysql2' , '0.3.21'
gem "simplecov"
gem "codeclimate-test-reporter", "~> 1.0.0"
end
Expand Down
1 change: 1 addition & 0 deletions gemfiles/5.0.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ source 'https://rubygems.org'
gem "activerecord", "~> 5.0"

group :test do
gem 'mysql2' , '0.3.21'
gem "simplecov"
gem "codeclimate-test-reporter", "~> 1.0.0"
end
Expand Down
7 changes: 7 additions & 0 deletions lib/find_with_order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,10 @@ def find_with_order(ids)
where(id: ids).order("field(id, #{ids.join(',')})")
end
end
unless ActiveRecord::Base.respond_to?(:none) # extend only if not implement yet
class ActiveRecord::Base
def self.none #For Rails 3
where('1=0')
end
end
end
25 changes: 20 additions & 5 deletions test/find_with_order_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,25 @@ def test_that_it_has_a_version_number
refute_nil ::FindWithOrder::VERSION
end

def test_basic_find_with_order
order = [2, 1, 3]
order_map = order.each_with_index.to_h
expected = User.where(:id => order).to_a.sort_by{|user| order_map[user.id] }
assert_equal expected, User.find_with_order(order)
def test_find_id_with_order
test_order = proc{|order|
expected = User.where(:id => order).to_a.sort_by{|user| order.index(user.id) }
assert_equal expected, User.find_with_order(order)
}
test_order.call [1, 2, 3]
test_order.call [1, 3, 2]
test_order.call [2, 1, 3]
test_order.call [2, 3, 1]
test_order.call [3, 1, 2]
test_order.call [3, 2, 1]
end

def test_none
assert_equal [], User.none.find_with_order([1, 2, 3]).to_a
end

def test_find_empty
expected = []
assert_equal expected, User.find_with_order([])
end
end