Skip to content

Commit 166f208

Browse files
committed
Detect the Phlex superclass to extend
1 parent ce729f8 commit 166f208

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

lib/superform/rails.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,19 @@ module Rails
55
# When this is included in a Rails app, check if `Components::Base` is defined and
66
# inherit from that so we get all the users stuff; otherwise inherit from Phlex::HTML,
77
# which means we won't have all the users methods and overrides.
8-
Component = defined?(::Components::Base) ? ::Components::Base : ::Phlex::HTML
8+
SUPERCLASSES = [
9+
"::Components::Base", # Phlex 2.x base class in a Rails project
10+
"::ApplicationComponent", # Phlex 1.x base class in a Rails project
11+
"::Phlex::HTML", # Couldn't detect a base Phlex Rails class, so use Phlex::HTML
12+
]
13+
14+
# Find the base class for the Rails app.
15+
def self.base_class
16+
const_get SUPERCLASSES.find { |const| const_defined?(const) }
17+
end
18+
19+
# Set the base class for the remainder of this library.
20+
Component = base_class
921

1022
# A Phlex::HTML view module that accepts a model and sets a `Superform::Namespace`
1123
# with the `Object#model_name` as the key and maps the object to form fields

0 commit comments

Comments
 (0)