Skip to content

Commit b9fa3f9

Browse files
committed
add config to constantize polymorphic class names
1 parent a7e3430 commit b9fa3f9

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

lib/subroutine.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,18 @@
1616

1717
module Subroutine
1818

19+
# Used by polymorphic association fields to resolve the class name to a ruby class
20+
def constantize_polymorphic_class_name(class_name)
21+
return @constantize_polymorphic_class_name.call(class_name) if defined?(@constantize_polymorphic_class_name)
22+
23+
class_name.camelize.constantize
24+
end
25+
26+
# When you need to customize how a polymorphic class name is resolved, you can set this callable/lambda/proc
27+
def constantize_polymorphic_class_name=(callable)
28+
@constantize_polymorphic_class_name = callable
29+
end
30+
1931
def self.include_defaults_in_params=(bool)
2032
@include_defaults_in_params = !!bool
2133
end

lib/subroutine/association_fields.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ def fetch_association_instance(config)
177177
get_field(config.foreign_type_method)
178178
end
179179

180-
klass = klass.camelize.constantize if klass.is_a?(String)
180+
klass = Subroutine.constantize_polymorphic_class_name(klass) if klass.is_a?(String)
181181
return nil unless klass
182182

183183
foreign_key = config.foreign_key_method

0 commit comments

Comments
 (0)