Skip to content

Commit

Permalink
feat: add early return for when the lazy attr does not exist
Browse files Browse the repository at this point in the history
  • Loading branch information
marcosgz committed Aug 28, 2024
1 parent 191b6d2 commit 33a7184
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 6 deletions.
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ GIT
PATH
remote: .
specs:
esse-async_indexing (0.1.0.rc1)
esse-async_indexing (0.1.0.rc2)
background_job
esse (>= 0.4.0.rc01)
esse (>= 0.4.0.rc1)
multi_json

GEM
Expand Down
9 changes: 7 additions & 2 deletions lib/esse/async_indexing/actions/bulk_update_lazy_attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@ def self.call(index_class_name, repo_name, attr_name, ids, options = {})
_index_class, repo_class = CoerceIndexRepository.call(index_class_name, repo_name)
kwargs = Esse::HashUtils.deep_transform_keys(options, &:to_sym)

attr_name = repo_class.lazy_document_attributes.keys.find { |key| key.to_s == attr_name.to_s }
repo_class.update_documents_attribute(attr_name, ids, **kwargs)
real_attr_name = repo_class.lazy_document_attribute_names(attr_name).first
if real_attr_name.nil? # let the job fail? or log and return?
Esse.logger.warn("Lazy attribute #{attr_name.inspect} not found in `#{repo_name}` repository of `#{index_class_name}` index.")
return
end

repo_class.update_documents_attribute(real_attr_name, ids, **kwargs)
ids
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def validate_options!
end

def repo_attributes(repo)
return repo.lazy_document_attributes.keys if attributes.empty?
return repo.lazy_document_attribute_names(true) if attributes.empty?

repo.lazy_document_attribute_names(attributes)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/esse/async_indexing/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

module Esse
module AsyncIndexing
VERSION = "0.1.0.rc1"
VERSION = "0.1.0.rc2"
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,10 @@

expect(described_class.call("GeosIndex", "city", "total_neighborhoods", city_ids, suffix: "2024", refresh: true)).to eq(["1"])
end

it "logs a warning when the lazy attribute is not found" do
expect(Esse.logger).to receive(:warn).with("Lazy attribute :unknown not found in `city` repository of `GeosIndex` index.")
expect(described_class.call("GeosIndex", "city", :unknown, city_ids)).to be_nil
end
end
end

0 comments on commit 33a7184

Please sign in to comment.