4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
# File 'lib/komponent/komponent_helper.rb', line 4
def component(component, locals = {}, &block)
component_path = Komponent::ComponentPathResolver.new.resolve(component)
parts = component.split("/")
component_name = parts.join("_")
component_path = component_path.join("#{component_name}_component")
require_dependency(component_path)
component_module = "#{component_name}_component".camelize.constantize
context = controller.view_context.dup
context.view_flow = view_flow
view_renderer = context.view_renderer = context.view_renderer.dup
lookup_context = view_renderer.lookup_context = view_renderer.lookup_context.dup
lookup_context.prefixes = ["components/#{component}"]
context.class_eval { prepend component_module }
context.class_eval { prepend Komponent::Translation }
capture_block = proc { capture(&block) } if block
context.instance_eval do
if component_module.respond_to?(:properties)
locals = locals.dup
component_module.properties.each do |name, options|
unless locals.has_key?(name)
if options.has_key?(:default)
locals[name] = options[:default]
elsif options[:required]
raise "Missing required component parameter: #{name}"
end
end
end
end
locals.each do |name, value|
instance_variable_set(:"@#{name}", locals[name])
end
define_singleton_method(:properties) { locals }
instance_variable_set(:"@block_given_to_component", block)
end
begin
context.render("components/#{component}/#{parts.join('_')}", &capture_block)
rescue ActionView::MissingTemplate
warn "[DEPRECATION WARNING] `#{parts.last}` filename in namespace is deprecated in favor of `#{parts.join('_')}`."
context.render("components/#{component}/#{parts.last}", &capture_block)
end
end
|