forked from radioactive-labs/plutonium-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.rb
116 lines (99 loc) · 4.26 KB
/
controller.rb
1
2
3
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
module Plutonium
module Core
module Controller
extend ActiveSupport::Concern
include Plutonium::Core::Controllers::Bootable
include Plutonium::Core::Controllers::EntityScoping
include Plutonium::Core::Controllers::Authorizable
included do
add_flash_types :success, :warning, :error
before_action do
next unless defined?(ActiveStorage)
ActiveStorage::Current.url_options = {protocol: request.protocol, host: request.host, port: request.port}
end
helper Plutonium::Helpers
helper_method :make_page_title, :resource_url_for,
:resource_url_args_for, :root_path, :app_name
append_view_path File.expand_path("app/views", Plutonium.root)
layout -> { turbo_frame_request? ? false : "resource" }
helper_method :registered_resources
end
private
def set_page_title(page_title)
@page_title = page_title
end
def make_page_title(title)
[title.presence, app_name].compact.join(" | ")
end
def app_name
helpers.application_name
end
#
# Returns a dynamic list of args to be used with `url_for`, which considers the route namespace and nesting.
# The current entity and parent record (for nested routes) are inserted appropriately, ensuring that generated URLs
# obey the current routing.
#
# e.g., of route helpers that will be invoked given the output of this method
#
# - when invoked in a root route (/acme/dashboard/users)
#
# `resource_url_args_for User` => `entity_users_*`
# `resource_url_args_for @user` => `entity_user_*`
# `resource_url_args_for @user, action: :edit` => `edit_entity_user_*`
# `resource_url_args_for @user, Post` => `entity_user_posts_*`
#
# - when invoked in a nested route (/acme/dashboard/users/1/post/1)
#
# `resource_url_args_for Post` => `entity_user_posts_*`
# `resource_url_args_for @post` => `entity_user_post_*`
# `resource_url_args_for @post, action: :edit` => `edit_entity_user_post_*`
#
# @param [Class, ApplicationRecord] *args arguments you would normally pass to `url_for`
# @param [Symbol] action optional action to invoke, e.g., :new, :edit
# @param [ApplicationRecord] parent the parent record for nested routes, if any
# @param [Hash] kwargs additional keyword arguments to pass to `url_for`
#
# @return [Hash] args to pass to `url_for`
#
def resource_url_args_for(*args, action: nil, parent: nil, **kwargs)
url_args = {**kwargs, action: action}.compact
controller_chain = [current_package&.to_s].compact
[*args].compact.each_with_index do |element, index|
if element.is_a?(Class)
controller_chain << element.to_s.pluralize
else
controller_chain << element.class.to_s.pluralize
if index == args.length - 1
resource_route_config = current_engine.routes.resource_route_config_for(element.model_name.plural)[0]
url_args[:id] = element.to_param unless resource_route_config[:route_type] == :resource
url_args[:action] ||= :show
else
url_args[element.model_name.singular_route_key.to_sym] = element.to_param
end
end
end
url_args[:controller] = "/#{controller_chain.join("::").underscore}"
url_args[:"#{parent.model_name.singular_route_key}_id"] = parent.to_param if parent.present?
if scoped_to_entity? && scoped_entity_strategy == :path
url_args[scoped_entity_param_key] = current_scoped_entity
end
url_args
end
def resource_url_for(...)
args = resource_url_args_for(...)
if current_package.present?
send(current_package.name.underscore.to_sym).url_for(args)
else
url_for(args)
end
end
def root_path(*)
return send(:"#{scoped_entity_param_key}_root_path", *) if scoped_to_entity? && scoped_entity_strategy == :path
super
end
def registered_resources
current_engine.resource_register.resources
end
end
end
end