Class: React::TopLevelRailsComponent

Inherits:
Object
  • Object
show all
Includes:
Hyperloop::Component::Mixin
Defined in:
lib/rails-helpers/top_level_rails_component.rb

Instance Attribute Summary

Attributes included from Hyperloop::Component::Mixin

#waiting_on_resources

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Hyperloop::Component::Mixin

#_render_wrapper, #component_did_catch, #component_did_mount, #component_did_update, #component_will_mount, #component_will_receive_props, #component_will_unmount, #component_will_update, #define_state, deprecation_warning, #deprecation_warning, #emit, included, #initialize, #set_state_synchronously?, #update_react_js_state, #watch

Class Method Details

.search_pathObject



5
6
7
# File 'lib/rails-helpers/top_level_rails_component.rb', line 5

def self.search_path
  @search_path ||= [Object]
end

Instance Method Details

#renderObject



17
18
19
# File 'lib/rails-helpers/top_level_rails_component.rb', line 17

def render
  top_level_render
end

#top_level_renderObject



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
57
58
59
60
61
62
63
# File 'lib/rails-helpers/top_level_rails_component.rb', line 21

def top_level_render
  paths_searched = []
  component = nil
  if params.component_name.start_with?('::')
    # if absolute path of component is given, look it up and fail if not found
    paths_searched << params.component_name
    component = begin
                  Object.const_get(params.component_name)
                rescue NameError
                  nil
                end
  else
    # if relative path is given, look it up like this
    # 1) we check each path + controller-name + component-name
    # 2) if we can't find it there we check each path + component-name
    # if we can't find it we just try const_get
    # so (assuming controller name is Home)
    # ::Foo::Bar will only resolve to some component named ::Foo::Bar
    # but Foo::Bar will check (in this order) ::Home::Foo::Bar, ::Components::Home::Foo::Bar, ::Foo::Bar, ::Components::Foo::Bar
    self.class.search_path.each do |scope|
      paths_searched << "#{scope.name}::#{params.controller}::#{params.component_name}"
      component = begin
                    scope.const_get(params.controller, false).const_get(params.component_name, false)
                  rescue NameError
                    nil
                  end
      break if component != nil
    end
    unless component
      self.class.search_path.each do |scope|
        paths_searched << "#{scope.name}::#{params.component_name}"
        component = begin
                      scope.const_get(params.component_name, false)
                    rescue NameError
                      nil
                    end
        break if component != nil
      end
    end
  end
  return React::RenderingContext.render(component, params.render_params) if component && component.method_defined?(:render)
  raise "Could not find component class '#{params.component_name}' for params.controller '#{params.controller}' in any component directory. Tried [#{paths_searched.join(", ")}]"
end