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