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

Remove searchable attr warning for nested attrs #369

Merged
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
31 changes: 23 additions & 8 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2024-05-27 18:22:30 UTC using RuboCop version 1.27.0.
# on 2024-09-13 15:45:46 UTC using RuboCop version 1.27.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 1
# This cop supports safe auto-correction (--auto-correct).
# Configuration parameters: Include.
# Include: **/*.gemspec
Gemspec/RequireMFA:
Exclude:
- 'meilisearch-rails.gemspec'

# Offense count: 1
# Configuration parameters: Include.
# Include: **/*.gemspec
Gemspec/RequiredRubyVersion:
Exclude:
- 'meilisearch-rails.gemspec'

# Offense count: 1
# This cop supports safe auto-correction (--auto-correct).
# Configuration parameters: EnforcedStyle.
Expand Down Expand Up @@ -64,14 +79,14 @@ Metrics/BlockLength:
# Offense count: 1
# Configuration parameters: CountComments, CountAsOne.
Metrics/ClassLength:
Max: 169
Max: 171

# Offense count: 8
# Offense count: 9
# Configuration parameters: IgnoredMethods.
Metrics/CyclomaticComplexity:
Max: 27

# Offense count: 18
# Offense count: 19
# Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods.
Metrics/MethodLength:
Max: 102
Expand Down Expand Up @@ -123,7 +138,7 @@ RSpec/ContextWording:
- 'spec/options_spec.rb'
- 'spec/system/tech_shop_spec.rb'

# Offense count: 54
# Offense count: 55
# Configuration parameters: CountAsOne.
RSpec/ExampleLength:
Max: 16
Expand Down Expand Up @@ -175,7 +190,7 @@ Style/GuardClause:
Exclude:
- 'lib/meilisearch-rails.rb'

# Offense count: 8
# Offense count: 7
# This cop supports safe auto-correction (--auto-correct).
Style/IfUnlessModifier:
Exclude:
Expand Down Expand Up @@ -217,9 +232,9 @@ Style/StringLiterals:
Exclude:
- 'spec/ms_clean_up_job_spec.rb'

# Offense count: 16
# Offense count: 15
# This cop supports safe auto-correction (--auto-correct).
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
# URISchemes: http, https
Layout/LineLength:
Max: 170
Max: 159
14 changes: 8 additions & 6 deletions lib/meilisearch-rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,16 @@ def initialize(options, &block)
end

def warn_searchable_missing_attributes
searchables = get_setting(:searchable_attributes)
attrs = get_setting(:attributes)&.keys
searchables = get_setting(:searchable_attributes)&.map { |searchable| searchable.to_s.split('.').first }
attrs = get_setting(:attributes)&.map { |k, _| k.to_s }

if searchables.present? && attrs.present?
(searchables.map(&:to_s) - attrs.map(&:to_s)).each do |missing_searchable|
MeiliSearch::Rails.logger.warn(
"[meilisearch-rails] #{missing_searchable} declared in searchable_attributes but not in attributes. Please add it to attributes if it should be searchable."
)
(searchables - attrs).each do |missing_searchable|
warning = <<~WARNING
[meilisearch-rails] #{missing_searchable} declared in searchable_attributes but not in attributes. \
Please add it to attributes if it should be searchable.
WARNING
MeiliSearch::Rails.logger.warn(warning)
end
end
end
Expand Down
20 changes: 20 additions & 0 deletions spec/settings_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,26 @@
expect(logger).to have_received(:warn).with(/meilisearch-rails.+last_name/)
end
end

context 'when a searchable attribute is nested' do
let(:logger) { instance_double('Logger', warn: nil) }

before do
allow(MeiliSearch::Rails).to receive(:logger).and_return(logger)
end

it 'we cannot be certain that it is not defined and don\'t warn the user' do
Comment.meilisearch_settings.add_index(safe_index_uid('nested_searchable_attr_spec')) do
attribute :post do
{ title: post&.title }
end

searchable_attributes ['post.title']
end

expect(logger).not_to have_received(:warn)
end
end
end

describe 'add_index' do
Expand Down
5 changes: 5 additions & 0 deletions spec/support/models/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,9 @@ class Post < ActiveRecord::Base
end

class Comment < ActiveRecord::Base
belongs_to :post

include MeiliSearch::Rails

meilisearch
end