Skip to content

Commit e4aecac

Browse files
committed
Fix component kit autoload order bug (#729)
Instead of loading all the constants, now we use method missing to lazy-load them. I believe this fixes #727
1 parent f078432 commit e4aecac

File tree

4 files changed

+37
-17
lines changed

4 files changed

+37
-17
lines changed

fixtures/components.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ module Components
44
extend Phlex::Kit
55

66
autoload :SayHi, "components/say_hi"
7+
autoload :Article, "components/article"
78
end

fixtures/components/article.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# frozen_string_literal: true
2+
3+
class Components::Article < Phlex::HTML
4+
def view_template(&block)
5+
article(&block)
6+
end
7+
end

fixtures/components/say_hi.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ def initialize(name, times: 1)
77
end
88

99
def view_template
10-
article {
10+
Article {
1111
@times.times { h1 { "Hi #{@name}" } }
1212
yield
1313
}

lib/phlex/kit.rb

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,38 @@
11
# frozen_string_literal: true
22

33
module Phlex::Kit
4-
# When a kit is included in a module, we need to load all of its components.
5-
def included(mod)
6-
constants.each { |c| const_get(c) if autoload?(c) }
7-
super
8-
end
4+
module LazyLoader
5+
def method_missing(name, *args, **kwargs, &block)
6+
if name[0] == name[0].upcase && __phlex_kit_constants__.include?(name) && __get_phlex_kit_constant__(name) && methods.include?(name)
7+
public_send(name, *args, **kwargs, &block)
8+
else
9+
super
10+
end
11+
end
912

10-
def method_missing(name, *args, **kwargs, &block)
11-
if name[0] == name[0].upcase && constants.include?(name) && const_get(name) && methods.include?(name)
12-
public_send(name, *args, **kwargs, &block)
13-
else
14-
super
13+
def respond_to_missing?(name, include_private = false)
14+
if name[0] == name[0].upcase && __phlex_kit_constants__.include?(name) && __get_phlex_kit_constant__(name) && methods.include?(name)
15+
true
16+
else
17+
super
18+
end
1519
end
1620
end
1721

18-
def respond_to_missing?(name, include_private = false)
19-
if name[0] == name[0].upcase && constants.include?(name) && const_get(name) && methods.include?(name)
20-
true
21-
else
22-
super
23-
end
22+
include LazyLoader
23+
24+
def self.extended(mod)
25+
mod.include(LazyLoader)
26+
mod.define_method(:__phlex_kit_constants__) { mod.__phlex_kit_constants__ }
27+
mod.define_method(:__get_phlex_kit_constant__) { |name| mod.__get_phlex_kit_constant__(name) }
28+
end
29+
30+
def __phlex_kit_constants__
31+
constants
32+
end
33+
34+
def __get_phlex_kit_constant__(name)
35+
const_get(name)
2436
end
2537

2638
def const_added(name)

0 commit comments

Comments
 (0)