diff --git a/lib/rom/runtime.rb b/lib/rom/runtime.rb index 8916bc2e7..a1effd937 100644 --- a/lib/rom/runtime.rb +++ b/lib/rom/runtime.rb @@ -136,7 +136,15 @@ def auto_register(directory, **options) # @api private def register_constant(type, constant) if config.key?(constant.config.component.type) - constant.config.component.join!(config[constant.config.component.type]) + parent_config = config[constant.config.component.type] + const_config = constant.config.component + + const_config.inherit!(parent_config).join!(parent_config) + + # TODO: make this work with all components + if const_config.key?(:infer_id_from_class) && const_config.infer_id_from_class + const_config.id = const_config.inflector.component_id(constant.name)&.to_sym + end end components.add(type, constant: constant, config: constant.config.component) diff --git a/lib/rom/settings.rb b/lib/rom/settings.rb index 05c0693e8..ba0dbff0b 100644 --- a/lib/rom/settings.rb +++ b/lib/rom/settings.rb @@ -62,6 +62,7 @@ module ROM setting :type, default: :relation setting :abstract setting :id + setting :infer_id_from_class, inherit: true setting :namespace, default: "relations" setting :dataset setting :adapter diff --git a/spec/suite/rom/runtime/register_relation_spec.rb b/spec/suite/rom/runtime/register_relation_spec.rb new file mode 100644 index 000000000..3388e812a --- /dev/null +++ b/spec/suite/rom/runtime/register_relation_spec.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +require "rom/runtime" + +RSpec.describe ROM::Runtime, "#register_relation" do + subject(:runtime) do + ROM::Runtime.new + end + + let(:resolver) do + runtime.resolver + end + + it "registers a relation class using provided component's id" do + stub_const("Users", Class.new(ROM::Relation) { config.component.id = :users }) + + runtime.register_relation(Users) + + expect(resolver["relations.users"]).to be_instance_of(Users) + end + + it "registers a relation class with component's id inferred from the class name" do + runtime.config.relation.infer_id_from_class = true + + stub_const("Users", Class.new(ROM::Relation)) + + runtime.register_relation(Users) + + expect(resolver["relations.users"]).to be_instance_of(Users) + end +end