From b3c0d3ea9729e594d1ecb4b2adddb793fc5d0162 Mon Sep 17 00:00:00 2001 From: "Marcos G. Zimmermann" Date: Mon, 5 Aug 2024 17:48:36 -0300 Subject: [PATCH] feat: add connect_with option to the collection definition --- CHANGELOG.md | 3 +++ lib/esse/plugins/active_record.rb | 1 + .../active_record/collection_definition_spec.rb | 14 ++++++++++++++ 3 files changed, 18 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 32d6511..0f28493 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,9 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## 0.3.8 - 2024-? +* Add `connect_with:` option to the collection definition. + ## 0.3.7 - 2024-08-05 * Add `connected_to` to the collection for custom connection handling diff --git a/lib/esse/plugins/active_record.rb b/lib/esse/plugins/active_record.rb index 4bf84a3..7a4f0be 100644 --- a/lib/esse/plugins/active_record.rb +++ b/lib/esse/plugins/active_record.rb @@ -18,6 +18,7 @@ def collection(*args, **kwargs, &block) repo = Class.new(Esse::ActiveRecord::Collection) repo.base_scope = -> { model_class } repo.batch_size = kwargs.delete(:batch_size) if kwargs.key?(:batch_size) + repo.connect_with = kwargs.delete(:connect_with) if kwargs.key?(:connect_with) repo.class_eval(&block) if block super(repo, *args, **kwargs) diff --git a/spec/esse/plugin/active_record/collection_definition_spec.rb b/spec/esse/plugin/active_record/collection_definition_spec.rb index c601f2c..7ea52fe 100644 --- a/spec/esse/plugin/active_record/collection_definition_spec.rb +++ b/spec/esse/plugin/active_record/collection_definition_spec.rb @@ -61,6 +61,20 @@ expect(col.batch_size).to eq(10) end + it 'define a collection with custom connect_with' do + expect { + stub_esse_index(:animals) do + plugin :active_record + + repository :animal do + collection(Animal, connect_with: { role: :reading }) + end + end + }.not_to raise_error + expect(col = AnimalsIndex.repo.instance_variable_get(:@collection_proc)).to be < Esse::ActiveRecord::Collection + expect(col.connect_with).to eq(role: :reading) + end + it 'evaluates the block in the Esse::ActiveRecord::Collection' do expect { stub_esse_index(:animals) do