Skip to content

Commit

Permalink
Merge 645cf76 into ef5d96c
Browse files Browse the repository at this point in the history
  • Loading branch information
ellnix authored Nov 3, 2023
2 parents ef5d96c + 645cf76 commit 007d085
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 19 deletions.
32 changes: 16 additions & 16 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2023-03-17 21:09:14 UTC using RuboCop version 1.27.0.
# on 2023-11-01 17:06:53 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
# This cop supports safe auto-correction (--auto-correct).
# Configuration parameters: EnforcedStyle.
Expand All @@ -22,6 +14,14 @@ Layout/EmptyLinesAroundModuleBody:
Exclude:
- 'lib/meilisearch-rails.rb'

# Offense count: 1
# This cop supports safe auto-correction (--auto-correct).
# Configuration parameters: EnforcedStyle, IndentationWidth.
# SupportedStyles: aligned, indented, indented_relative_to_receiver
Layout/MultilineMethodCallIndentation:
Exclude:
- 'lib/meilisearch/rails/pagination.rb'

# Offense count: 1
# Configuration parameters: AllowComments, AllowNil.
Lint/SuppressedException:
Expand All @@ -38,7 +38,7 @@ Lint/UnusedMethodArgument:
# Offense count: 10
# Configuration parameters: IgnoredMethods, CountRepeatedAttributes.
Metrics/AbcSize:
Max: 101
Max: 102

# Offense count: 1
# Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods.
Expand All @@ -59,12 +59,12 @@ Metrics/CyclomaticComplexity:
# Offense count: 16
# Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods.
Metrics/MethodLength:
Max: 98
Max: 99

# Offense count: 1
# Configuration parameters: CountComments, CountAsOne.
Metrics/ModuleLength:
Max: 416
Max: 428

# Offense count: 9
# Configuration parameters: IgnoredMethods.
Expand Down Expand Up @@ -102,7 +102,7 @@ RSpec/DescribeClass:
Exclude:
- 'spec/integration_spec.rb'

# Offense count: 35
# Offense count: 36
# Configuration parameters: CountAsOne.
RSpec/ExampleLength:
Max: 19
Expand Down Expand Up @@ -158,7 +158,7 @@ Style/GuardClause:
Exclude:
- 'lib/meilisearch-rails.rb'

# Offense count: 9
# Offense count: 8
# This cop supports safe auto-correction (--auto-correct).
Style/IfUnlessModifier:
Exclude:
Expand All @@ -184,9 +184,9 @@ Style/RescueModifier:
Exclude:
- 'lib/meilisearch-rails.rb'

# Offense count: 19
# Offense count: 21
# This cop supports safe auto-correction (--auto-correct).
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
# URISchemes: http, https
Layout/LineLength:
Max: 269
Max: 178
21 changes: 20 additions & 1 deletion lib/meilisearch-rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ def included(klass)
include InstanceMethods
end
end

def logger
@logger ||= (::Rails.logger || Logger.new($stdout))
end
end

class IndexSettings
Expand Down Expand Up @@ -304,7 +308,7 @@ def self.log_or_throw(method, raise_on_failure, &block)
raise e if raise_on_failure

# log the error
(::Rails.logger || Logger.new($stdout)).info("[meilisearch-rails] #{e.message}")
MeiliSearch::Rails.logger.info("[meilisearch-rails] #{e.message}")
# return something
case method.to_s
when 'search'
Expand Down Expand Up @@ -444,6 +448,8 @@ def meilisearch(options = {}, &block)
after_destroy { |searchable| searchable.ms_enqueue_remove_from_index!(ms_synchronous?) }
end
end

warn_searchable_missing_attributes
end

def ms_without_auto_index(&block)
Expand Down Expand Up @@ -858,6 +864,19 @@ def ms_attribute_changed?(document, attr_name)
# We don't know if the attribute has changed, so conservatively assume it has
true
end

def warn_searchable_missing_attributes
searchables = meilisearch_settings.get_setting(:searchable_attributes)
attrs = meilisearch_settings.get_setting(:attributes)&.keys

if searchables.present? && attrs.present?
(searchables.map(&:to_s) - attrs.map(&:to_s)).each do |missing_searchable|
MeiliSearch::Rails.logger.warn(
"[meilisearch-rails] #{name}##{missing_searchable} declared in searchable_attributes but not in attributes. Please add it to attributes if it should be searchable."
)
end
end
end
end

# these are the instance methods included
Expand Down
2 changes: 1 addition & 1 deletion lib/meilisearch/rails/pagination.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def self.create(results, total_hits, options = {})
end

def self.log_pagy_error
(::Rails.logger || Logger.new($stdout))
MeiliSearch::Rails.logger
.warning('[meilisearch-rails] Remove `pagination_backend: :pagy` from your initializer, `pagy` it is not required for `pagy`')
end

Expand Down
27 changes: 26 additions & 1 deletion spec/integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -782,7 +782,7 @@
it 'has meaningful error when pagy is set as the pagination_backend' do
logger = double
allow(logger).to receive(:warning)
allow(::Rails).to receive(:logger).and_return(logger)
allow(MeiliSearch::Rails).to receive(:logger).and_return(logger)

Movie.search('')

Expand Down Expand Up @@ -1032,6 +1032,31 @@
end
end

context 'when a searchable attribute is not an attribute' do
let(:other_people_class) do
Class.new(People) do
def self.name
'People'
end
end
end

let(:logger) { instance_double('Logger', warn: nil) }

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

other_people_class.meilisearch index_uid: safe_index_uid('Others'), primary_key: :card_number do
attribute :first_name
searchable_attributes %i[first_name last_name]
end
end

it 'warns the user' do
expect(logger).to have_received(:warn).with(/meilisearch-rails.+last_name/)
end
end

context "when have a internal class defined in the app's scope" do
it 'does not raise NoMethodError' do
Task.create(title: 'my task #1')
Expand Down

0 comments on commit 007d085

Please sign in to comment.