Skip to content

Commit

Permalink
Bring back Repository#set_relation but deprecate it (fix #698)
Browse files Browse the repository at this point in the history
It was part of private API used by hanami/db. This marks prepare_relation as replacement
  • Loading branch information
flash-gordon committed Jan 9, 2025
1 parent 05110e9 commit 0a6f1bc
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
14 changes: 11 additions & 3 deletions repository/lib/rom/repository/relation_reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ class Repository
# @api private
class RelationReader < ::Module
module InstanceMethods
extend ::Dry::Core::Deprecations[:'rom-repository']

private

# @api private
# @api public
def prepare_relation(name, **)
container
.relations[name]
Expand All @@ -17,15 +19,21 @@ def prepare_relation(name, **)
)
end

# @api private
def set_relation(name) # rubocop:disable Naming/AccessorMethodName
prepare_relation(name)
end
deprecate :set_relation, :prepare_relation

# @api private
def relation_reader(cache, ...)
cache_key = relation_cache_key(...)
cache.fetch_or_store(*cache_key) { prepare_relation(...) }
end

# @api private
def relation_cache_key(name, **)
[name, auto_struct, struct_namespace]
def relation_cache_key(name, **kwargs)
[name, auto_struct, struct_namespace, kwargs]
end
end

Expand Down
37 changes: 37 additions & 0 deletions repository/spec/integration/repository_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -423,4 +423,41 @@ class Post < OpenStruct; end

expect(post).to be(post_from_user)
end

context 'using #prepare_relation' do
let(:repo_class) do
Class.new(ROM::Repository[:users]) do
def prepare_relation(name, custom: 'default')
super.select_append { `'#{custom}'`.as(:extra_column) }
end
end
end

it 'is uses modified relation' do
expect(repo.users.to_a.map(&:extra_column)).to eql(%w[default default])
expect(repo.users(custom: 'custom').to_a.map(&:extra_column)).to eql(%w[custom custom])
end
end

context 'using #set_relation' do
let(:log_file) do
Tempfile.new('dry_deprecations')
end

before do
Dry::Core::Deprecations.set_logger!(log_file)
end

let(:repo_class) do
Class.new(ROM::Repository[:users]) do
def custom_users
set_relation(:users).select_append { `'modified'`.as(:modified) }
end
end
end

it "constructs a relation but it's deprecated" do
expect(repo.custom_users.to_a.map(&:modified)).to eql(%w[modified modified])
end
end
end

0 comments on commit 0a6f1bc

Please sign in to comment.