Skip to content

Commit f8c820d

Browse files
committed
Remove searchable attr warning for nested attrs
Since attributes can be defined with a block the only thing we know about them is the top level attribute name. Therefore, we trust that the user knows what they are doing when they use nested attributes.
1 parent c4abc60 commit f8c820d

File tree

4 files changed

+56
-14
lines changed

4 files changed

+56
-14
lines changed

.rubocop_todo.yml

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,26 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config`
3-
# on 2024-05-27 18:22:30 UTC using RuboCop version 1.27.0.
3+
# on 2024-09-13 15:45:46 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
77
# versions of RuboCop, may require this file to be generated again.
88

9+
# Offense count: 1
10+
# This cop supports safe auto-correction (--auto-correct).
11+
# Configuration parameters: Include.
12+
# Include: **/*.gemspec
13+
Gemspec/RequireMFA:
14+
Exclude:
15+
- 'meilisearch-rails.gemspec'
16+
17+
# Offense count: 1
18+
# Configuration parameters: Include.
19+
# Include: **/*.gemspec
20+
Gemspec/RequiredRubyVersion:
21+
Exclude:
22+
- 'meilisearch-rails.gemspec'
23+
924
# Offense count: 1
1025
# This cop supports safe auto-correction (--auto-correct).
1126
# Configuration parameters: EnforcedStyle.
@@ -64,14 +79,14 @@ Metrics/BlockLength:
6479
# Offense count: 1
6580
# Configuration parameters: CountComments, CountAsOne.
6681
Metrics/ClassLength:
67-
Max: 169
82+
Max: 171
6883

69-
# Offense count: 8
84+
# Offense count: 9
7085
# Configuration parameters: IgnoredMethods.
7186
Metrics/CyclomaticComplexity:
7287
Max: 27
7388

74-
# Offense count: 18
89+
# Offense count: 19
7590
# Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods.
7691
Metrics/MethodLength:
7792
Max: 102
@@ -123,7 +138,7 @@ RSpec/ContextWording:
123138
- 'spec/options_spec.rb'
124139
- 'spec/system/tech_shop_spec.rb'
125140

126-
# Offense count: 54
141+
# Offense count: 55
127142
# Configuration parameters: CountAsOne.
128143
RSpec/ExampleLength:
129144
Max: 16
@@ -175,7 +190,7 @@ Style/GuardClause:
175190
Exclude:
176191
- 'lib/meilisearch-rails.rb'
177192

178-
# Offense count: 8
193+
# Offense count: 7
179194
# This cop supports safe auto-correction (--auto-correct).
180195
Style/IfUnlessModifier:
181196
Exclude:
@@ -217,9 +232,9 @@ Style/StringLiterals:
217232
Exclude:
218233
- 'spec/ms_clean_up_job_spec.rb'
219234

220-
# Offense count: 16
235+
# Offense count: 15
221236
# This cop supports safe auto-correction (--auto-correct).
222237
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
223238
# URISchemes: http, https
224239
Layout/LineLength:
225-
Max: 170
240+
Max: 159

lib/meilisearch-rails.rb

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,16 @@ def initialize(options, &block)
8888
end
8989

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

9494
if searchables.present? && attrs.present?
95-
(searchables.map(&:to_s) - attrs.map(&:to_s)).each do |missing_searchable|
96-
MeiliSearch::Rails.logger.warn(
97-
"[meilisearch-rails] #{missing_searchable} declared in searchable_attributes but not in attributes. Please add it to attributes if it should be searchable."
98-
)
95+
(searchables - attrs).each do |missing_searchable|
96+
warning = <<~WARNING
97+
[meilisearch-rails] #{missing_searchable} declared in searchable_attributes but not in attributes. \
98+
Please add it to attributes if it should be searchable.
99+
WARNING
100+
MeiliSearch::Rails.logger.warn(warning)
99101
end
100102
end
101103
end

spec/settings_spec.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,26 @@
152152
expect(logger).to have_received(:warn).with(/meilisearch-rails.+last_name/)
153153
end
154154
end
155+
156+
context 'when a searchable attribute is nested' do
157+
let(:logger) { instance_double('Logger', warn: nil) }
158+
159+
before do
160+
allow(MeiliSearch::Rails).to receive(:logger).and_return(logger)
161+
end
162+
163+
it 'we cannot be certain that it is not defined and don\'t warn the user' do
164+
Comment.meilisearch_settings.add_index(safe_index_uid('nested_searchable_attr_spec')) do
165+
attribute :post do
166+
{ title: post&.title }
167+
end
168+
169+
searchable_attributes ['post.title']
170+
end
171+
172+
expect(logger).not_to have_received(:warn)
173+
end
174+
end
155175
end
156176

157177
describe 'add_index' do

spec/support/models/post.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,9 @@ class Post < ActiveRecord::Base
2424
end
2525

2626
class Comment < ActiveRecord::Base
27+
belongs_to :post
28+
29+
include MeiliSearch::Rails
30+
31+
meilisearch
2732
end

0 commit comments

Comments
 (0)