Skip to content

Commit

Permalink
Merge #278
Browse files Browse the repository at this point in the history
278: Changed camel case keywords to snake case r=brunoocasali a=jason-hobbs

Was getting warnings in tests and logs like so:
Non-conforming attributes: searchableAttributes, sortableAttributes
Attributes will be expected to be snake_case in future versions of Meilisearch Ruby.

Changed all instances of camel case to snake case to make warnings go away and to resolve the deprecations.

Co-authored-by: Jason Hobbs <[email protected]>
Co-authored-by: Bruno Casali <[email protected]>
  • Loading branch information
3 people committed Sep 5, 2023
2 parents 3f41be9 + 102e1d9 commit 945b791
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 30 deletions.
42 changes: 21 additions & 21 deletions lib/meilisearch-rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,23 @@ class IndexSettings

# Meilisearch settings
OPTIONS = %i[
searchableAttributes
filterableAttributes
sortableAttributes
displayedAttributes
distinctAttribute
searchable_attributes
filterable_attributes
sortable_attributes
displayed_attributes
distinct_attribute
synonyms
stopWords
rankingRules
attributesToHighlight
attributesToCrop
cropLength
stop_words
ranking_rules
attributes_to_highlight
attributes_to_crop
crop_length
pagination
faceting
typoTolerance
typo_tolerance
].freeze

CAMELIZE_OPTIONS = %i[pagination faceting typoTolerance].freeze
CAMELIZE_OPTIONS = %i[pagination faceting typo_tolerance].freeze

OPTIONS.each do |option|
define_method option do |value|
Expand Down Expand Up @@ -265,9 +265,9 @@ def initialize(index_uid, raise_on_failure, options)
::MeiliSearch::Index.instance_methods(false).each do |m|
define_method(m) do |*args, &block|
if m == :update_settings
args[0].delete(:attributesToHighlight) if args[0][:attributesToHighlight]
args[0].delete(:attributesToCrop) if args[0][:attributesToCrop]
args[0].delete(:cropLength) if args[0][:cropLength]
args[0].delete(:attributes_to_highlight) if args[0][:attributes_to_highlight]
args[0].delete(:attributes_to_crop) if args[0][:attributes_to_crop]
args[0].delete(:crop_length) if args[0][:crop_length]
end

SafeIndex.log_or_throw(m, @raise_on_failure) do
Expand Down Expand Up @@ -581,15 +581,15 @@ def ms_clear_index!(synchronous = false)
def ms_raw_search(q, params = {})
index_uid = params.delete(:index) || params.delete('index')

unless meilisearch_settings.get_setting(:attributesToHighlight).nil?
params[:attributesToHighlight] = meilisearch_settings.get_setting(:attributesToHighlight)
unless meilisearch_settings.get_setting(:attributes_to_highlight).nil?
params[:attributes_to_highlight] = meilisearch_settings.get_setting(:attributes_to_highlight)
end

unless meilisearch_settings.get_setting(:attributesToCrop).nil?
params[:attributesToCrop] = meilisearch_settings.get_setting(:attributesToCrop)
unless meilisearch_settings.get_setting(:attributes_to_crop).nil?
params[:attributes_to_crop] = meilisearch_settings.get_setting(:attributes_to_crop)

unless meilisearch_settings.get_setting(:cropLength).nil?
params[:cropLength] = meilisearch_settings.get_setting(:cropLength)
unless meilisearch_settings.get_setting(:crop_length).nil?
params[:crop_length] = meilisearch_settings.get_setting(:crop_length)
end
end

Expand Down
19 changes: 10 additions & 9 deletions spec/integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,20 @@
describe 'Settings change detection' do
it 'detects settings changes' do
expect(Color.send(:meilisearch_settings_changed?, nil, {})).to be(true)
expect(Color.send(:meilisearch_settings_changed?, {}, { 'searchableAttributes' => ['name'] })).to be(true)
expect(Color.send(:meilisearch_settings_changed?, { 'searchableAttributes' => ['name'] },
{ 'searchableAttributes' => %w[name hex] })).to be(true)
expect(Color.send(:meilisearch_settings_changed?, { 'searchableAttributes' => ['name'] },
{ 'rankingRules' => ['words', 'typo', 'proximity', 'attribute', 'sort', 'exactness', 'hex:asc'] })).to be(true)
expect(Color.send(:meilisearch_settings_changed?, {}, { 'searchable_attributes' => ['name'] })).to be(true)
expect(Color.send(:meilisearch_settings_changed?, { 'searchable_attributes' => ['name'] },
{ 'searchable_attributes' => %w[name hex] })).to be(true)
expect(Color.send(:meilisearch_settings_changed?, { 'searchable_attributes' => ['name'] },
{ 'ranking_rules' => ['words', 'typo', 'proximity', 'attribute', 'sort', 'exactness', 'hex:asc'] })).to be(true)
end

it 'does not detect settings changes' do
expect(Color.send(:meilisearch_settings_changed?, {}, {})).to be(false)
expect(Color.send(:meilisearch_settings_changed?, { 'searchableAttributes' => ['name'] },
{ searchableAttributes: ['name'] })).to be(false)
expect(Color.send(:meilisearch_settings_changed?, { 'searchable_attributes' => ['name'] },
{ searchable_attributes: ['name'] })).to be(false)
expect(Color.send(:meilisearch_settings_changed?,
{ 'searchableAttributes' => ['name'], 'rankingRules' => ['words', 'typo', 'proximity', 'attribute', 'sort', 'exactness', 'hex:asc'] }, { 'rankingRules' => ['words', 'typo', 'proximity', 'attribute', 'sort', 'exactness', 'hex:asc'] })).to be(false)
{ 'searchable_attributes' => ['name'], 'ranking_rules' => ['words', 'typo', 'proximity', 'attribute', 'sort', 'exactness', 'hex:asc'] },
{ 'ranking_rules' => ['words', 'typo', 'proximity', 'attribute', 'sort', 'exactness', 'hex:asc'] })).to be(false)
end
end

Expand Down Expand Up @@ -536,7 +537,7 @@
it 'sanitizes attributes' do
_hack = Book.create! name: '"><img src=x onerror=alert(1)> hack0r',
author: '<script type="text/javascript">alert(1)</script>', premium: true, released: true
b = Book.raw_search('hack', { attributesToHighlight: ['*'] })
b = Book.raw_search('hack', { attributes_to_highlight: ['*'] })
expect(b['hits'].length).to eq(1)
begin
expect(b['hits'][0]['name']).to eq('"> hack0r').and_raise(StandardError)
Expand Down

0 comments on commit 945b791

Please sign in to comment.