Skip to content

Commit 736de7a

Browse files
authored
Merge pull request #302 from matestack/action-view-interoperability
action-view interoperability
2 parents ce97453 + bcbfd39 commit 736de7a

File tree

9 files changed

+589
-4
lines changed

9 files changed

+589
-4
lines changed

.circleci/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ jobs:
5151
name: run tests
5252
command: |
5353
mkdir /tmp/test-results
54-
TEST_FILES="$(circleci tests glob "spec/usage/**/*_spec.rb" | circleci tests split --split-by=timings)"
54+
TEST_FILES="$(circleci tests glob "spec/usage/**/*_spec.rb" "spec/matestack/**/*_spec.rb" | circleci tests split --split-by=timings)"
5555
5656
bundle exec rspec --format progress \
5757
--out /tmp/test-results/rspec.xml \

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ class Dynamic < Trailblazer::Cell
44
include ::Cell::Haml
55
include Matestack::Ui::Core::ApplicationHelper
66
include Matestack::Ui::Core::ToCell
7+
include Matestack::Ui::Core::HasViewContext
78

89
view_paths << "#{Matestack::Ui::Core::Engine.root}/app/concepts"
910
view_paths << "#{::Rails.root}/app/matestack"

app/concepts/matestack/ui/core/page/page.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ class Page < Trailblazer::Cell
55
include ::Cell::Haml
66
include Matestack::Ui::Core::ApplicationHelper
77
include Matestack::Ui::Core::ToCell
8+
include Matestack::Ui::Core::HasViewContext
89

910
view_paths << "#{Matestack::Ui::Core::Engine.root}/app/concepts"
1011

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ module Matestack
22
module Ui
33
module Core
44
module ApplicationHelper
5+
include Matestack::Ui::Core::Render
56

67
def render_page(page_class, only_page=false)
78
page_class.new(nil, context: {
89
params: params,
9-
request: request
10+
request: request,
11+
view_context: view_context
1012
}, controller_instance: self).call(:show, nil, only_page)
1113
end
1214

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ def initialize(component_instance, included_config)
2121

2222
def method_missing meth, *args, &block
2323
begin
24-
@component_instance.send(meth, *args, &block)
24+
if (result = @component_instance.send(meth, *args, &block)).kind_of? ActiveSupport::SafeBuffer
25+
plain result
26+
else
27+
result
28+
end
2529
rescue
2630
node_id = @node_start_id + 1
2731
@node_start_id = node_id
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module Matestack::Ui::Core::HasViewContext
2+
def initialize(model = nil, options = {})
3+
@view_context = options[:context][:view_context]
4+
super
5+
end
6+
7+
def method_missing(*args, &block)
8+
@view_context.send(*args, &block)
9+
end
10+
end

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ def initialize(page_instance, included_config, url_params)
2222

2323
def method_missing meth, *args, &block
2424
begin
25-
@page_instance.send(meth, *args, &block)
25+
if (result = @page_instance.send(meth, *args, &block)).kind_of? ActiveSupport::SafeBuffer
26+
plain result
27+
else
28+
result
29+
end
2630
rescue
2731
node_id = @node_start_id + 1
2832
@node_start_id = node_id

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

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
module Matestack::Ui::Core::Render
2+
3+
# Matestack allows you to use `render` to render matestack pages.
4+
#
5+
# render Pages::Member::Bookings::Index
6+
# render matestack: Pages::Member::Bookings::Index
7+
# render matestack: 'member/bookings/index'
8+
#
9+
def render(*args)
10+
if (matestack_class = args.first).is_a?(Class) && (matestack_class < Matestack::Ui::Page)
11+
responder_for matestack_class
12+
elsif (options = args.first).kind_of?(Hash) && (matestack_arg = options[:matestack]).present?
13+
if (matestack_path = matestack_arg).kind_of? String
14+
matestack_path = "pages/#{matestack_path}" unless matestack_path.start_with?("pages/") or matestack_path.start_with?("components/")
15+
matestack_class = matestack_path.split("/").collect { |str| str.camelcase }.join("::").constantize
16+
elsif matestack_arg.is_a?(Class) && (matestack_arg < Matestack::Ui::Page)
17+
matestack_class = matestack_arg
18+
end
19+
responder_for matestack_class
20+
else
21+
super
22+
end
23+
end
24+
25+
# Matestack allows implicit rendering. The matestack page class name is inferred from the
26+
# controller path and action name.
27+
#
28+
# class Clients::BookingsController < ApplicationController
29+
# def index
30+
# @bookings = Booking.all
31+
# # looks for Pages::Clients::Bookings::Index
32+
# end
33+
#
34+
# def show
35+
# @booking = Booking.find params[:id]
36+
# # looks for Pages::Clients::Bookings::Show
37+
# end
38+
# end
39+
#
40+
# In this example, `clients/bookings#index` will render `Pages::Clients::Bookings::Index`,
41+
# `clients/bookings#show` will render `Pages::Clients::Bookings::Show`.
42+
#
43+
# Custom action names translate also into page names.
44+
#
45+
# class Clients::BookingsController < ApplicationController
46+
# def step1
47+
# # looks for Pages::Clients::Bookings::Step1
48+
# end
49+
# end
50+
#
51+
# In this example, the `clients/bookings#step1` action will render
52+
# `Pages::Clients::Bookings::Step1`.
53+
#
54+
def default_render(*args)
55+
if matestack_page_class = default_render_matestack_page_class
56+
render matestack: matestack_page_class
57+
else
58+
super
59+
end
60+
end
61+
62+
def possible_default_render_matestack_page_paths
63+
paths = []
64+
paths << "pages/#{controller_path}/#{action_name}"
65+
paths << "pages/#{controller_path}" if action_name == "index"
66+
paths << "pages/#{controller_path.singularize}" if action_name == "show"
67+
paths << "#{controller_path}/#{action_name}_page"
68+
paths << "#{controller_path}_page" if action_name == "index"
69+
paths << "#{controller_path.singularize}_page" if action_name == "show"
70+
paths
71+
end
72+
73+
def possible_default_render_matestack_page_class_names
74+
possible_default_render_matestack_page_paths.collect { |page_path|
75+
page_path.split("/").collect { |str| str.camelcase }.join("::")
76+
}
77+
end
78+
79+
def default_render_matestack_page_class
80+
possible_default_render_matestack_page_class_names.each do |class_name|
81+
begin
82+
return matestack_class = class_name.constantize
83+
rescue NameError
84+
end
85+
end
86+
return nil
87+
end
88+
89+
end

0 commit comments

Comments
 (0)