Skip to content

[Model] Fix ActiveRecord sorted collection #618

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

Closed
wants to merge 2 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
3 changes: 3 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ namespace :bundle do
sh "bundle install --gemfile #{__current__.join('elasticsearch-model/gemfiles')}/3.0.gemfile"
puts '-'*80
sh "bundle install --gemfile #{__current__.join('elasticsearch-model/gemfiles')}/4.0.gemfile"
puts '-'*80
sh "bundle install --gemfile #{__current__.join('elasticsearch-model/gemfiles')}/5.0.gemfile"
end

desc "Remove Gemfile.lock in all subprojects"
Expand All @@ -45,6 +47,7 @@ namespace :bundle do
end
sh "rm -f #{__current__.join('elasticsearch-model/gemfiles')}/3.0.gemfile.lock"
sh "rm -f #{__current__.join('elasticsearch-model/gemfiles')}/4.0.gemfile.lock"
sh "rm -f #{__current__.join('elasticsearch-model/gemfiles')}/5.0.gemfile.lock"
end
end

Expand Down
1 change: 1 addition & 0 deletions elasticsearch-model/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ tmp

gemfiles/3.0.gemfile.lock
gemfiles/4.0.gemfile.lock
gemfiles/5.0.gemfile.lock
3 changes: 2 additions & 1 deletion elasticsearch-model/Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ namespace :test do
test.test_files = FileList["test/integration/**/*_test.rb"]
end

desc "Run integration tests against ActiveModel 3 and 4"
desc "Run integration tests against ActiveModel 3, 4 and 5"
task :integration do
sh "BUNDLE_GEMFILE='#{File.expand_path('../gemfiles/3.0.gemfile', __FILE__)}' bundle exec rake test:run_integration" unless defined?(RUBY_VERSION) && RUBY_VERSION > '2.2'
sh "BUNDLE_GEMFILE='#{File.expand_path('../gemfiles/4.0.gemfile', __FILE__)}' bundle exec rake test:run_integration"
sh "BUNDLE_GEMFILE='#{File.expand_path('../gemfiles/5.0.gemfile', __FILE__)}' bundle exec rake test:run_integration"
end

desc "Run unit and integration tests"
Expand Down
2 changes: 1 addition & 1 deletion elasticsearch-model/elasticsearch-model.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Gem::Specification.new do |s|
s.add_development_dependency "kaminari"
s.add_development_dependency "will_paginate"

s.add_development_dependency "minitest", "~> 4.2"
s.add_development_dependency "minitest", ">= 4.2"
s.add_development_dependency "test-unit" if defined?(RUBY_VERSION) && RUBY_VERSION > '2.2'
s.add_development_dependency "shoulda-context"
s.add_development_dependency "mocha"
Expand Down
12 changes: 12 additions & 0 deletions elasticsearch-model/gemfiles/5.0.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Usage:
#
# $ BUNDLE_GEMFILE=./gemfiles/5.0.gemfile bundle install
# $ BUNDLE_GEMFILE=./gemfiles/5.0.gemfile bundle exec rake test:integration

source 'https://rubygems.org'

gemspec path: '../'

gem 'activemodel', '~> 5'
gem 'activerecord', '~> 5'
gem 'sqlite3'
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ def records
# by redefining `to_a`, unless the user has called `order()`
#
sql_records.instance_exec(response.response['hits']['hits']) do |hits|
define_singleton_method :to_a do
ar_records_method_name = :to_a
ar_records_method_name = :records if defined?(::ActiveRecord) && ::ActiveRecord::VERSION::MAJOR >= 5

define_singleton_method(ar_records_method_name) do
if defined?(::ActiveRecord) && ::ActiveRecord::VERSION::MAJOR >= 4
self.load
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,10 @@ def as_indexed_json(options = {})
end

should "preserve the search results order for records" do
response = Article.search('title:code')
response = Article.search query: { match: { title: 'code' }}, sort: { clicks: :desc }

assert_equal response.records[0].clicks, 3
assert_equal response.records[1].clicks, 2

response.records.each_with_hit do |r, h|
assert_equal h._id, r.id.to_s
Expand Down