Skip to content

Commit 461405c

Browse files
bors[bot]anderson
andauthored
Merge #247
247: Fix rails cache serialization (marshal dump) r=brunoocasali a=anderson # Pull Request ## Related issue Fixes #246 ## What does this PR do? - Fix rails cache serialization (marshal dump) from database result after Meilisearch result. ## PR checklist Please check if your PR fulfills the following requirements: - [x] Does this PR fix an existing issue, or have you listed the changes applied in the PR description (and why they are needed)? - [x] Have you read the contributing guidelines? - [x] Have you made sure that the title is accurate and descriptive of the changes? Co-authored-by: Anderson Peligrini <[email protected]>
2 parents 33eceb5 + 9732c0a commit 461405c

File tree

3 files changed

+52
-12
lines changed

3 files changed

+52
-12
lines changed

.rubocop_todo.yml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config`
3-
# on 2023-03-06 23:49:40 UTC using RuboCop version 1.27.0.
3+
# on 2023-03-17 21:09:14 UTC using RuboCop version 1.27.0.
44
# The point is for the user to remove these configuration records
55
# one by one as the offenses are removed from the code base.
66
# Note that changes in the inspected code, or installation of new
@@ -49,7 +49,7 @@ Metrics/BlockLength:
4949
# Offense count: 1
5050
# Configuration parameters: CountComments, CountAsOne.
5151
Metrics/ClassLength:
52-
Max: 157
52+
Max: 156
5353

5454
# Offense count: 9
5555
# Configuration parameters: IgnoredMethods.
@@ -102,8 +102,7 @@ RSpec/DescribeClass:
102102
Exclude:
103103
- 'spec/integration_spec.rb'
104104

105-
106-
# Offense count: 34
105+
# Offense count: 35
107106
# Configuration parameters: CountAsOne.
108107
RSpec/ExampleLength:
109108
Max: 19
@@ -140,6 +139,11 @@ RSpec/VerifiedDoubles:
140139
Exclude:
141140
- 'spec/configuration_spec.rb'
142141

142+
# Offense count: 1
143+
Security/MarshalLoad:
144+
Exclude:
145+
- 'spec/integration_spec.rb'
146+
143147
# Offense count: 2
144148
# This cop supports safe auto-correction (--auto-correct).
145149
# Configuration parameters: EnforcedStyle.

lib/meilisearch-rails.rb

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ class IndexSettings
5959
attributesToHighlight
6060
attributesToCrop
6161
cropLength
62-
faceting
6362
pagination
6463
faceting
6564
typoTolerance
@@ -599,13 +598,6 @@ def ms_raw_search(q, params = {})
599598
end
600599

601600
module AdditionalMethods
602-
def self.extended(base)
603-
class << base
604-
alias_method :raw_answer, :ms_raw_answer unless method_defined? :raw_answer
605-
alias_method :facets_distribution, :ms_facets_distribution unless method_defined? :facets_distribution
606-
end
607-
end
608-
609601
def ms_raw_answer
610602
@ms_json
611603
end
@@ -614,6 +606,9 @@ def ms_facets_distribution
614606
@ms_json['facetDistribution']
615607
end
616608

609+
alias raw_answer ms_raw_answer unless method_defined? :raw_answer
610+
alias facets_distribution ms_facets_distribution unless method_defined? :facets_distribution
611+
617612
private
618613

619614
def ms_init_raw_answer(json)

spec/integration_spec.rb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,47 @@
610610
expect(genres.size).to be > 3
611611
expect(results.facets_distribution['genre'].size).to eq(3)
612612
end
613+
614+
context 'with Marshal serialization' do
615+
let(:found_books) { Book.search('*') }
616+
let(:marshaled_books) { Marshal.dump(found_books) }
617+
618+
it 'returns all books in the marshaled format' do
619+
# Perform the search and marshal the results
620+
expect(marshaled_books).to be_present
621+
622+
# Load the marshaled data and check the content
623+
loaded_books = Marshal.load(marshaled_books)
624+
expect(loaded_books).to match_array(found_books)
625+
end
626+
end
627+
628+
context 'with Rails caching' do
629+
let(:memory_store) { ActiveSupport::Cache.lookup_store(:memory_store) }
630+
let(:cache) { Rails.cache }
631+
632+
let(:search_query) { '*' }
633+
let(:cache_key) { "book_search:#{search_query}" }
634+
635+
before do
636+
allow(Rails).to receive(:cache).and_return(memory_store)
637+
Rails.cache.clear
638+
end
639+
640+
it 'caches the search results' do
641+
# Ensure the cache is empty before the test
642+
expect(Rails.cache.read(cache_key)).to be_nil
643+
644+
# Perform the search and cache the results
645+
Rails.cache.fetch(cache_key) do
646+
Book.search(search_query)
647+
end
648+
649+
# Check if the search result is cached
650+
not_cached_books = Book.search(search_query)
651+
expect(Rails.cache.read(cache_key)).to match_array(not_cached_books)
652+
end
653+
end
613654
end
614655

615656
describe 'Movie' do

0 commit comments

Comments
 (0)