Skip to content

Commit 73523bd

Browse files
authored
Merge pull request #661 from rom-rb/add-optional-component-id-inference-to-relations
Add support for relation.infer_id_from_class setting [changelog] added: "New relation setting: `component.infer_id_from_class` (via #661) (@solnic)"
2 parents 72ef199 + 7bfd595 commit 73523bd

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

lib/rom/runtime.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,15 @@ def auto_register(directory, **options)
136136
# @api private
137137
def register_constant(type, constant)
138138
if config.key?(constant.config.component.type)
139-
constant.config.component.join!(config[constant.config.component.type])
139+
parent_config = config[constant.config.component.type]
140+
const_config = constant.config.component
141+
142+
const_config.inherit!(parent_config).join!(parent_config)
143+
144+
# TODO: make this work with all components
145+
if const_config.key?(:infer_id_from_class) && const_config.infer_id_from_class
146+
const_config.id = const_config.inflector.component_id(constant.name)&.to_sym
147+
end
140148
end
141149

142150
components.add(type, constant: constant, config: constant.config.component)

lib/rom/settings.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ module ROM
6262
setting :type, default: :relation
6363
setting :abstract
6464
setting :id
65+
setting :infer_id_from_class, inherit: true
6566
setting :namespace, default: "relations"
6667
setting :dataset
6768
setting :adapter
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# frozen_string_literal: true
2+
3+
require "rom/runtime"
4+
5+
RSpec.describe ROM::Runtime, "#register_relation" do
6+
subject(:runtime) do
7+
ROM::Runtime.new
8+
end
9+
10+
let(:resolver) do
11+
runtime.resolver
12+
end
13+
14+
it "registers a relation class using provided component's id" do
15+
stub_const("Users", Class.new(ROM::Relation) { config.component.id = :users })
16+
17+
runtime.register_relation(Users)
18+
19+
expect(resolver["relations.users"]).to be_instance_of(Users)
20+
end
21+
22+
it "registers a relation class with component's id inferred from the class name" do
23+
runtime.config.relation.infer_id_from_class = true
24+
25+
stub_const("Users", Class.new(ROM::Relation))
26+
27+
runtime.register_relation(Users)
28+
29+
expect(resolver["relations.users"]).to be_instance_of(Users)
30+
end
31+
end

0 commit comments

Comments
 (0)