Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion lib/rom/runtime.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions lib/rom/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 31 additions & 0 deletions spec/suite/rom/runtime/register_relation_spec.rb
Original file line number Diff line number Diff line change
@@ -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