Skip to content

Commit fd52db5

Browse files
committed
added support for action view and url helper access in standalone component rendering through matestack_component helper
1 parent 9c2f305 commit fd52db5

File tree

13 files changed

+55
-10
lines changed

13 files changed

+55
-10
lines changed

app/concepts/matestack/ui/core/component/base.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ def add_child(child_class, *args, &block)
245245

246246
# check only allowed keys are passed to isolated components
247247
if child_class < Matestack::Ui::Core::Isolated::Isolated
248-
unless args.empty? || args[0].keys.all? { |key| [:defer, :public_options, :rerender_on, :init_on, :rerender_delay, :matestack_context].include? key }
248+
unless args.empty? || args[0].keys.all? { |key| [:defer, :public_options, :rerender_on, :init_on, :rerender_delay, :matestack_context, :context].include? key }
249249
raise "isolated components can only take params in a public_options hash, which will be exposed to the client side in order to perform an async request with these params."
250250
end
251251
if args.any? { |arg| arg[:init_on].present? } && @matestack_skip_defer == true

app/helpers/matestack/ui/core/application_helper.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ def matestack_component(component, options = {}, &block)
102102
controller = (self.class <= ActionController::Base) ? self : @_controller
103103
context = (options[:matestack_context] ||= {}).merge(controller: controller)
104104
Matestack::Ui::Core::Component::Base
105-
.new(options.merge(matestack_context: context))
106-
.send(component, options.merge(matestack_context: context), &block).to_s
105+
.new(options.merge(context: context, matestack_context: context))
106+
.send(component, options.merge(context: context, matestack_context: context), &block).to_s
107107
end
108108
end
109109
end

app/lib/matestack/ui/core/has_view_context.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ module Matestack::Ui::Core::HasViewContext
22
def initialize(*args)
33
super
44
@view_context = @options.dig(:context, :view_context)
5+
unless @view_context
6+
@view_context = @options.dig(:matestack_context, :controller)&.view_context
7+
end
58
end
6-
79
def method_missing(*args, &block)
810
if @view_context.respond_to? args.first
911
@view_context.send(*args, &block)
1012
else
1113
raise NameError, "NameError: undefined method or local variable `#{args.first}' for #{self.class.name}"
1214
end
1315
end
14-
end
16+
end

spec/dummy/app/controllers/legacy_views/pages_controller.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ class LegacyViews::PagesController < ApplicationController
66

77
def action_inline
88
end
9-
9+
1010
def action_custom_component
1111
@title = 'Test Title'
1212
end
13-
13+
1414
def async_custom_component
1515
end
16-
16+
1717
def async_inline
1818
end
1919

@@ -45,6 +45,9 @@ def form_custom_component
4545
def onclick_custom_component
4646
end
4747

48+
def viewcontext_custom_component
49+
end
50+
4851
def isolated_custom_component
4952
render 'isolated_custom_component'
5053
end
@@ -78,4 +81,4 @@ def dummy_model_params
7881
)
7982
end
8083

81-
end
84+
end
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Components::LegacyViews::Pages::Viewcontext < Matestack::Ui::Component
2+
3+
def response
4+
div id: "my-component" do
5+
if @view_context.view_renderer.instance_of?(ActionView::Renderer)
6+
plain "has access to ActionView Context"
7+
end
8+
plain link_to "Test Link", "/some/page" # calling an ActionView Url Helper here
9+
plain time_ago_in_words(3.minutes.from_now) # calling an ActionView Date Helper here
10+
plain "root_path: #{root_path}" # calling a Path Helper here
11+
end
12+
end
13+
14+
end

spec/dummy/app/matestack/components/registry.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ module Components::Registry
1313
legacy_views_pages_form: Components::LegacyViews::Pages::Form,
1414
legacy_views_pages_onclick: Components::LegacyViews::Pages::Onclick,
1515
legacy_views_pages_isolated: Components::LegacyViews::Pages::Isolated,
16+
legacy_views_pages_viewcontext: Components::LegacyViews::Pages::Viewcontext,
1617
)
1718

1819
end
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<h1>Isolated Custom Component</h1>
22
<%=
33
matestack_component(:legacy_views_pages_isolated, rerender_on: 'update_time' )
4-
%>
4+
%>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<h1>Viewcontext Custom Component</h1>
2+
<%= matestack_component(:legacy_views_pages_viewcontext) %>

spec/dummy/config/routes.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
mount Matestack::Ui::Core::Engine, at: '/matestack'
44

5+
root to: 'my_app#my_first_page'
6+
57
scope :components_tests do
68
get "static_rendering_test/:component", to: 'components_tests#static_rendering_test', as: "components_tests"
79
get "custom_components_test", to: 'components_tests#custom_components_test'
@@ -55,6 +57,7 @@
5557
get 'form_custom_component', to: 'pages#form_custom_component'
5658
get 'onclick_custom_component', to: 'pages#onclick_custom_component'
5759
get 'isolated_custom_component', to: 'pages#isolated_custom_component'
60+
get 'viewcontext_custom_component', to: 'pages#viewcontext_custom_component'
5861
post 'success', to: 'pages#success'
5962
post 'failure', to: 'pages#failure'
6063
post 'create', to: 'pages#create'

spec/test/base/core/app/action_view_context_spec.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ def response
2727
end
2828
plain link_to "Test Link", "/some/page" # calling an ActionView Url Helper here
2929
plain time_ago_in_words(3.minutes.from_now) # calling an ActionView Date Helper here
30+
plain "root_path: #{root_path}" # calling a Path Helper here
3031
main do
3132
page_content
3233
end
@@ -55,6 +56,7 @@ def page1
5556
expect(page).to have_content("Test Link")
5657
expect(page).to have_content("has access to ActionView Context")
5758
expect(page).to have_content("3 minutes")
59+
expect(page).to have_content("root_path: /")
5860
end
5961

6062
end

0 commit comments

Comments
 (0)