active_scaffold_mcp 0.1.2 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +40 -0
- data/README.md +46 -1
- data/lib/active_scaffold/config/mcp.rb +31 -31
- data/lib/active_scaffold/mcp/action_config.rb +2 -2
- data/lib/active_scaffold/mcp/engine.rb +2 -2
- data/lib/active_scaffold/mcp/filtered_tools.rb +1 -1
- data/lib/active_scaffold/mcp/generator_factory.rb +3 -3
- data/lib/active_scaffold/mcp/list_tool.rb +7 -7
- data/lib/active_scaffold/mcp/list_tool_filters.rb +204 -0
- data/lib/active_scaffold/mcp/list_tool_generator.rb +49 -209
- data/lib/active_scaffold/mcp/list_views_tool.rb +3 -4
- data/lib/active_scaffold/mcp/presenter.rb +155 -24
- data/lib/active_scaffold/mcp/profile_user.rb +2 -2
- data/lib/active_scaffold/mcp/settings_validator.rb +1 -1
- data/lib/active_scaffold/mcp/show_tool.rb +7 -6
- data/lib/active_scaffold/mcp/show_tool_generator.rb +40 -21
- data/lib/active_scaffold/mcp/tool.rb +7 -38
- data/lib/active_scaffold/mcp/tool_generator.rb +6 -6
- data/lib/active_scaffold/mcp/tool_params.rb +49 -0
- data/lib/active_scaffold/mcp/version.rb +1 -1
- data/lib/active_scaffold/mcp.rb +17 -18
- data/lib/active_scaffold_mcp.rb +5 -5
- data/lib/tasks/active_scaffold_mcp_tasks.rake +2 -2
- metadata +6 -2
|
@@ -1,27 +1,38 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require 'active_scaffold/mcp/list_tool_filters'
|
|
4
|
+
|
|
3
5
|
module ActiveScaffold
|
|
4
6
|
module MCP
|
|
5
7
|
class ListToolGenerator < ToolGenerator
|
|
6
|
-
|
|
7
|
-
eq: :equal,
|
|
8
|
-
equality: :equal,
|
|
9
|
-
range: :between,
|
|
10
|
-
not_eq: :not_equal,
|
|
11
|
-
neq: :not_equal,
|
|
12
|
-
not_range: :not_between
|
|
13
|
-
}.freeze
|
|
8
|
+
extend ListToolFilters
|
|
14
9
|
|
|
15
10
|
class << self
|
|
16
11
|
def generate_tool_for(controller_class:, action_name:, action_config:, mcp_config:, settings:)
|
|
17
12
|
core_config = mcp_config.core
|
|
18
13
|
mappings = {}
|
|
14
|
+
list_tool = build_list_tool(
|
|
15
|
+
controller_class:, action_name:, action_config:, core_config:, mcp_config:, settings:, mappings:
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
return list_tool unless saved_views_enabled?(core_config)
|
|
19
19
|
|
|
20
|
-
|
|
20
|
+
[
|
|
21
|
+
list_tool,
|
|
22
|
+
build_list_views_tool(
|
|
23
|
+
controller_class:, action_name:, core_config:, mcp_config:, settings:, list_tool:
|
|
24
|
+
)
|
|
25
|
+
]
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
private
|
|
29
|
+
|
|
30
|
+
def build_list_tool(controller_class:, action_name:, action_config:, core_config:, mcp_config:, settings:,
|
|
31
|
+
mappings:)
|
|
32
|
+
Class.new(ActiveScaffold::MCP::ListTool).tap do |tool_class|
|
|
21
33
|
configure_tool_class(tool_class, controller_class:, action_name:, core_config:, mcp_config:, settings:)
|
|
22
34
|
include_config_list(tool_class, core_config)
|
|
23
35
|
tool_class.read_only
|
|
24
|
-
|
|
25
36
|
views_tool_name = "#{tool_class.tool_name}_views" if saved_views_enabled?(core_config)
|
|
26
37
|
list_views = add_list_view_property(tool_class, core_config, views_tool_name)
|
|
27
38
|
add_column_filter_properties(tool_class, core_config, settings, mappings)
|
|
@@ -32,34 +43,18 @@ module ActiveScaffold
|
|
|
32
43
|
generated_description = description(tool_class.title, mappings, sortable_columns, action_config, list_views)
|
|
33
44
|
tool_class.description(settings[:description] || generated_description)
|
|
34
45
|
tool_class.mcp_param_mappings = mappings.freeze
|
|
35
|
-
|
|
36
46
|
include_extensions(tool_class, settings)
|
|
37
47
|
end
|
|
38
|
-
|
|
39
|
-
return list_tool unless saved_views_enabled?(core_config)
|
|
40
|
-
|
|
41
|
-
[
|
|
42
|
-
list_tool,
|
|
43
|
-
build_list_views_tool(
|
|
44
|
-
controller_class:, action_name:, core_config:, mcp_config:, settings:, list_tool:
|
|
45
|
-
)
|
|
46
|
-
]
|
|
47
48
|
end
|
|
48
49
|
|
|
49
|
-
private
|
|
50
|
-
|
|
51
50
|
def include_config_list(tool_class, core_config)
|
|
52
51
|
return unless core_config.actions.include?(:config_list)
|
|
53
52
|
|
|
54
|
-
tool_class.include
|
|
53
|
+
tool_class.include 'ActiveScaffold::Actions::ConfigList'.constantize
|
|
55
54
|
end
|
|
56
55
|
|
|
57
56
|
def description(title, mappings, sortable_columns, action_config, list_views)
|
|
58
|
-
filter_types =
|
|
59
|
-
filter_types << "column filters" if mappings.any? { |_name, mapping| mapping[:source].nil? }
|
|
60
|
-
filter_types << "named list filters" if mappings.any? { |_name, mapping| mapping[:source] == :named_filter }
|
|
61
|
-
filter_types << "full-text search" if mappings.any? { |_name, mapping| mapping[:source] == :search }
|
|
62
|
-
|
|
57
|
+
filter_types = filter_types_for(mappings)
|
|
63
58
|
sentences = ["#{title} as a paginated collection"]
|
|
64
59
|
sentences << "Supports #{filter_types.to_sentence}" if filter_types.any?
|
|
65
60
|
search_mapping = mappings.values.find { |mapping| mapping[:source] == :search }
|
|
@@ -69,10 +64,19 @@ module ActiveScaffold
|
|
|
69
64
|
sentences << default_sorting_description(action_config)
|
|
70
65
|
end
|
|
71
66
|
sentences << list_views[:description] if list_views
|
|
72
|
-
sentences <<
|
|
67
|
+
sentences << 'Use limit and page for pagination'
|
|
73
68
|
"#{sentences.join('. ')}."
|
|
74
69
|
end
|
|
75
70
|
|
|
71
|
+
def filter_types_for(mappings)
|
|
72
|
+
sources = mappings.values.pluck(:source)
|
|
73
|
+
filter_types = []
|
|
74
|
+
filter_types << 'column filters' if sources.include?(nil)
|
|
75
|
+
filter_types << 'named list filters' if sources.include?(:named_filter)
|
|
76
|
+
filter_types << 'full-text search' if sources.include?(:search)
|
|
77
|
+
filter_types
|
|
78
|
+
end
|
|
79
|
+
|
|
76
80
|
def add_list_view_property(tool_class, core_config, views_tool_name)
|
|
77
81
|
return unless core_config.actions.include?(:config_list)
|
|
78
82
|
|
|
@@ -81,13 +85,13 @@ module ActiveScaffold
|
|
|
81
85
|
saved_views = saved_views_enabled?(core_config)
|
|
82
86
|
return if configured_names.empty? && !saved_views
|
|
83
87
|
|
|
84
|
-
sentences = [
|
|
88
|
+
sentences = ['Select a named list view to change the returned columns and default sorting']
|
|
85
89
|
sentences << "Configured views: #{formatted_column_names(configured_names)}" if configured_names.any?
|
|
86
90
|
if saved_views
|
|
87
91
|
sentences << "Call `#{views_tool_name}` to get all views available to the current user"
|
|
88
92
|
end
|
|
89
|
-
description = sentences.join(
|
|
90
|
-
property_options = {type:
|
|
93
|
+
description = sentences.join('. ')
|
|
94
|
+
property_options = {type: 'string', description: description}
|
|
91
95
|
property_options[:enum] = configured_names unless saved_views
|
|
92
96
|
tool_class.property :list_view, **property_options
|
|
93
97
|
{description: description}
|
|
@@ -107,7 +111,7 @@ module ActiveScaffold
|
|
|
107
111
|
tool_class.title("List views for #{tool_resource_label(mcp_config, settings)}")
|
|
108
112
|
tool_class.description(
|
|
109
113
|
"List all named views available to the current user for `#{list_tool.tool_name}`. " \
|
|
110
|
-
|
|
114
|
+
'Pass a returned `value` to its `list_view` argument; omit `list_view` to use the default columns.'
|
|
111
115
|
)
|
|
112
116
|
include_config_list(tool_class, core_config)
|
|
113
117
|
tool_class.read_only
|
|
@@ -115,184 +119,21 @@ module ActiveScaffold
|
|
|
115
119
|
end
|
|
116
120
|
end
|
|
117
121
|
|
|
118
|
-
def add_column_filter_properties(tool_class, core_config, settings, mappings)
|
|
119
|
-
configured_filter_columns(core_config, settings).each do |column_name, operators|
|
|
120
|
-
column = core_config._columns_hash[column_name.to_s]
|
|
121
|
-
next unless column
|
|
122
|
-
|
|
123
|
-
Array(operators).each do |operator|
|
|
124
|
-
add_operator_properties(tool_class, column_name, column, normalize_operator(operator), mappings)
|
|
125
|
-
end
|
|
126
|
-
end
|
|
127
|
-
end
|
|
128
|
-
|
|
129
|
-
def configured_filter_columns(core_config, settings)
|
|
130
|
-
result = case settings[:filter_columns]
|
|
131
|
-
when Hash
|
|
132
|
-
settings[:filter_columns].transform_keys(&:to_sym)
|
|
133
|
-
when nil
|
|
134
|
-
{}
|
|
135
|
-
else
|
|
136
|
-
Array(settings[:filter_columns]).to_h { |name| [name.to_sym, :equal] }
|
|
137
|
-
end
|
|
138
|
-
|
|
139
|
-
settings.fetch(:filter_column_sets, {}).each do |set_name, operators|
|
|
140
|
-
columns_for_set(core_config, set_name).each { |name| result[name] ||= operators }
|
|
141
|
-
end
|
|
142
|
-
add_polymorphic_type_columns(core_config, result)
|
|
143
|
-
result
|
|
144
|
-
end
|
|
145
|
-
|
|
146
|
-
def add_polymorphic_type_columns(core_config, filter_columns)
|
|
147
|
-
model = core_config.model
|
|
148
|
-
return unless model.respond_to?(:reflect_on_all_associations)
|
|
149
|
-
|
|
150
|
-
model.reflect_on_all_associations(:belongs_to).select(&:polymorphic?).each do |association|
|
|
151
|
-
foreign_key = association.foreign_key.to_sym
|
|
152
|
-
foreign_type = association.foreign_type.to_sym
|
|
153
|
-
next unless filter_columns.key?(foreign_key)
|
|
154
|
-
next unless core_config._columns_hash.key?(foreign_type.to_s)
|
|
155
|
-
|
|
156
|
-
filter_columns[foreign_type] ||= filter_columns[foreign_key]
|
|
157
|
-
end
|
|
158
|
-
end
|
|
159
|
-
|
|
160
|
-
def columns_for_set(core_config, set_name)
|
|
161
|
-
columns = core_config._columns_hash.values
|
|
162
|
-
selected_columns = case set_name.to_sym
|
|
163
|
-
when :foreign_keys
|
|
164
|
-
columns.select { |column| column.name.end_with?("_id") }
|
|
165
|
-
when :dates
|
|
166
|
-
columns.select { |column| column.type.to_sym == :date }
|
|
167
|
-
when :timestamps
|
|
168
|
-
columns.select { |column| %i[datetime timestamp timestamptz].include?(column.type.to_sym) }
|
|
169
|
-
else
|
|
170
|
-
raise ArgumentError, "unknown MCP filter column set: #{set_name}"
|
|
171
|
-
end
|
|
172
|
-
selected_columns.map { |column| column.name.to_sym }
|
|
173
|
-
end
|
|
174
|
-
|
|
175
|
-
def normalize_operator(operator)
|
|
176
|
-
operator = operator.to_sym
|
|
177
|
-
OPERATOR_ALIASES.fetch(operator, operator)
|
|
178
|
-
end
|
|
179
|
-
|
|
180
|
-
def add_operator_properties(tool_class, column_name, column, operator, mappings)
|
|
181
|
-
case operator
|
|
182
|
-
when :equal, :not_equal
|
|
183
|
-
property_name = operator == :equal ? column_name : :"#{column_name}_not"
|
|
184
|
-
add_scalar_property(tool_class, property_name, column, "Filter by #{column_name}")
|
|
185
|
-
mappings[property_name.to_sym] = {target: column_name.to_sym, operator: operator}
|
|
186
|
-
when :in, :not_in
|
|
187
|
-
property_name = operator == :in ? :"#{column_name}_in" : :"#{column_name}_not_in"
|
|
188
|
-
tool_class.collection property_name,
|
|
189
|
-
type: json_type(column),
|
|
190
|
-
description: "Filter #{column_name} by a list of values"
|
|
191
|
-
mappings[property_name] = {target: column_name.to_sym, operator: operator}
|
|
192
|
-
when :between, :not_between
|
|
193
|
-
prefix = operator == :between ? column_name : :"#{column_name}_not"
|
|
194
|
-
%i[from to].each do |endpoint|
|
|
195
|
-
property_name = :"#{prefix}_#{endpoint}"
|
|
196
|
-
add_scalar_property(tool_class, property_name, column, "#{endpoint.to_s.humanize} value for #{column_name}")
|
|
197
|
-
mappings[property_name] = {
|
|
198
|
-
target: column_name.to_sym,
|
|
199
|
-
operator: :"#{'not_' if operator == :not_between}between_#{endpoint}"
|
|
200
|
-
}
|
|
201
|
-
end
|
|
202
|
-
when :null, :not_null
|
|
203
|
-
property_name = operator == :null ? :"#{column_name}_null" : :"#{column_name}_not_null"
|
|
204
|
-
tool_class.property property_name,
|
|
205
|
-
type: "boolean",
|
|
206
|
-
description: "Match records where #{column_name} is #{'not ' if operator == :not_null}null"
|
|
207
|
-
mappings[property_name] = {target: column_name.to_sym, operator: operator}
|
|
208
|
-
else
|
|
209
|
-
raise ArgumentError, "unsupported MCP filter operator for #{column_name}: #{operator}"
|
|
210
|
-
end
|
|
211
|
-
end
|
|
212
|
-
|
|
213
|
-
def add_scalar_property(tool_class, property_name, column, description)
|
|
214
|
-
tool_class.property(property_name, **schema_options(column), description: description)
|
|
215
|
-
end
|
|
216
|
-
|
|
217
|
-
def add_named_filter_properties(tool_class, action_config, settings, mappings)
|
|
218
|
-
selected_filters(action_config, settings[:filters]).each do |filter|
|
|
219
|
-
property_name = filter.name.to_sym
|
|
220
|
-
if mappings.key?(property_name)
|
|
221
|
-
raise ArgumentError, "named list filter #{filter.name} conflicts with an MCP column filter"
|
|
222
|
-
end
|
|
223
|
-
|
|
224
|
-
option_names = filter.map { |option| option.name.to_s }
|
|
225
|
-
tool_class.property property_name,
|
|
226
|
-
type: "string",
|
|
227
|
-
description: named_filter_description(filter),
|
|
228
|
-
default: filter.default_option&.to_s,
|
|
229
|
-
enum: option_names
|
|
230
|
-
mappings[property_name] = {target: filter.name, operator: :equal, source: :named_filter}
|
|
231
|
-
end
|
|
232
|
-
end
|
|
233
|
-
|
|
234
|
-
def named_filter_description(filter)
|
|
235
|
-
sentences = [filter.description || "Apply the #{filter.name} list filter"]
|
|
236
|
-
sentences << "When omitted, the server uses the default option '#{filter.default_option}'"
|
|
237
|
-
if filter.security_method_set?
|
|
238
|
-
sentences << "This filter may be disallowed in the current context"
|
|
239
|
-
else
|
|
240
|
-
secured_options = filter.select(&:security_method_set?).map { |option| option.name.to_s }
|
|
241
|
-
if secured_options.any?
|
|
242
|
-
sentences << "These options may be disallowed in the current context: #{secured_options.to_sentence}"
|
|
243
|
-
end
|
|
244
|
-
end
|
|
245
|
-
sentences.join(". ")
|
|
246
|
-
end
|
|
247
|
-
|
|
248
|
-
def selected_filters(action_config, configured_filters)
|
|
249
|
-
return [] if configured_filters.nil? || !action_config.respond_to?(:filters)
|
|
250
|
-
return action_config.filters.to_a if configured_filters == :all
|
|
251
|
-
|
|
252
|
-
Array(configured_filters).filter_map { |name| action_config.filters[name] }
|
|
253
|
-
end
|
|
254
|
-
|
|
255
|
-
def add_search_property(tool_class, core_config, settings, mappings)
|
|
256
|
-
return unless search_enabled?(core_config, settings)
|
|
257
|
-
|
|
258
|
-
description = search_property_description(core_config)
|
|
259
|
-
tool_class.include ActiveScaffold::Actions::Search
|
|
260
|
-
tool_class.property :search,
|
|
261
|
-
type: "string",
|
|
262
|
-
description: description
|
|
263
|
-
mappings[:search] = {target: :search, operator: :equal, source: :search, description: description}
|
|
264
|
-
end
|
|
265
|
-
|
|
266
|
-
def search_property_description(core_config)
|
|
267
|
-
columns = core_config.search.columns.visible_columns(skip_authorization: true, flatten: true)
|
|
268
|
-
labels = columns.map { |column| column.label.presence || column.name.to_s.humanize }
|
|
269
|
-
return "Search text across the configured ActiveScaffold search columns" if labels.empty?
|
|
270
|
-
|
|
271
|
-
displayed_labels = labels.first(12)
|
|
272
|
-
remaining_count = labels.length - displayed_labels.length
|
|
273
|
-
description = "Search text across #{displayed_labels.to_sentence}"
|
|
274
|
-
description += " and #{remaining_count} more" if remaining_count.positive?
|
|
275
|
-
description
|
|
276
|
-
end
|
|
277
|
-
|
|
278
|
-
def search_enabled?(core_config, settings)
|
|
279
|
-
settings.fetch(:search, true) && core_config.actions.include?(:search)
|
|
280
|
-
end
|
|
281
|
-
|
|
282
122
|
def add_sorting_properties(tool_class, core_config, action_config)
|
|
283
123
|
columns = core_config.columns.select(&:sortable?)
|
|
284
124
|
column_names = columns.map { |column| column.name.to_s }
|
|
285
125
|
return [] if column_names.empty?
|
|
286
126
|
|
|
127
|
+
description = "Column to sort by. Allowed columns: #{formatted_column_names(column_names)}. " \
|
|
128
|
+
"#{default_sorting_description(action_config)}."
|
|
287
129
|
tool_class.property :sort,
|
|
288
|
-
type:
|
|
289
|
-
description:
|
|
290
|
-
"#{default_sorting_description(action_config)}.",
|
|
130
|
+
type: 'string',
|
|
131
|
+
description: description,
|
|
291
132
|
enum: column_names
|
|
292
133
|
tool_class.property :sort_direction,
|
|
293
|
-
type:
|
|
294
|
-
description:
|
|
295
|
-
default:
|
|
134
|
+
type: 'string',
|
|
135
|
+
description: 'Sort direction used when sort is provided; defaults to asc',
|
|
136
|
+
default: 'asc',
|
|
296
137
|
enum: %w[asc desc]
|
|
297
138
|
column_names
|
|
298
139
|
end
|
|
@@ -309,7 +150,7 @@ module ActiveScaffold
|
|
|
309
150
|
if clauses.any?
|
|
310
151
|
"When sort is omitted, the ActiveScaffold default sorting is #{clauses.to_sentence}"
|
|
311
152
|
else
|
|
312
|
-
|
|
153
|
+
'When sort is omitted, the configured ActiveScaffold default sorting is used'
|
|
313
154
|
end
|
|
314
155
|
end
|
|
315
156
|
|
|
@@ -321,18 +162,17 @@ module ActiveScaffold
|
|
|
321
162
|
ActiveScaffold::MCP::ListTool::DEFAULT_LIMIT
|
|
322
163
|
end
|
|
323
164
|
tool_class.property :limit,
|
|
324
|
-
type:
|
|
325
|
-
description:
|
|
165
|
+
type: 'integer',
|
|
166
|
+
description: 'Maximum records to return',
|
|
326
167
|
default: default_limit,
|
|
327
168
|
minimum: 1,
|
|
328
169
|
maximum: ActiveScaffold::MCP::ListTool::MAX_LIMIT
|
|
329
170
|
tool_class.property :page,
|
|
330
|
-
type:
|
|
331
|
-
description:
|
|
171
|
+
type: 'integer',
|
|
172
|
+
description: 'Page number',
|
|
332
173
|
default: 1,
|
|
333
174
|
minimum: 1
|
|
334
175
|
end
|
|
335
|
-
|
|
336
176
|
end
|
|
337
177
|
end
|
|
338
178
|
end
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require
|
|
4
|
-
require "set"
|
|
3
|
+
require 'json'
|
|
5
4
|
|
|
6
5
|
module ActiveScaffold
|
|
7
6
|
module MCP
|
|
@@ -21,7 +20,7 @@ module ActiveScaffold
|
|
|
21
20
|
{
|
|
22
21
|
label: view.name.to_s,
|
|
23
22
|
value: view.name.to_s,
|
|
24
|
-
source:
|
|
23
|
+
source: 'configured'
|
|
25
24
|
}.tap do |result|
|
|
26
25
|
result[:description] = view.tooltip.to_s if view.respond_to?(:tooltip) && view.tooltip.present?
|
|
27
26
|
end
|
|
@@ -35,7 +34,7 @@ module ActiveScaffold
|
|
|
35
34
|
{
|
|
36
35
|
label: label.to_s,
|
|
37
36
|
value: value.to_s,
|
|
38
|
-
source:
|
|
37
|
+
source: 'saved'
|
|
39
38
|
}
|
|
40
39
|
end
|
|
41
40
|
configured_views + saved_views
|
|
@@ -10,20 +10,18 @@ module ActiveScaffold
|
|
|
10
10
|
end
|
|
11
11
|
|
|
12
12
|
def presentable_column?(column)
|
|
13
|
-
return true if column.association ||
|
|
13
|
+
return true if column.association || presentation_method_name(column)
|
|
14
14
|
return false unless model.public_method_defined?(column.name)
|
|
15
15
|
|
|
16
|
-
model.instance_method(column.name).parameters.none? { |type, _|
|
|
16
|
+
model.instance_method(column.name).parameters.none? { |type, _| %i[req keyreq].include?(type) }
|
|
17
17
|
end
|
|
18
18
|
|
|
19
19
|
def record(record, columns, counts = nil)
|
|
20
|
-
columns.
|
|
21
|
-
|
|
20
|
+
columns.to_h do |column|
|
|
21
|
+
[column.name, column_value(record, column, counts)]
|
|
22
22
|
end
|
|
23
23
|
end
|
|
24
24
|
|
|
25
|
-
protected
|
|
26
|
-
|
|
27
25
|
delegate :active_scaffold_config, :current_user, :params, to: :tool
|
|
28
26
|
|
|
29
27
|
private
|
|
@@ -32,14 +30,113 @@ module ActiveScaffold
|
|
|
32
30
|
:"#{column.name}_column"
|
|
33
31
|
end
|
|
34
32
|
|
|
35
|
-
def
|
|
33
|
+
def ui_method_names(column)
|
|
34
|
+
action_name = tool.action_name
|
|
35
|
+
return [] unless %w[list show].include?(action_name)
|
|
36
|
+
|
|
37
|
+
ui_method = :"#{action_name}_ui"
|
|
38
|
+
ui = column.public_send(ui_method)
|
|
39
|
+
return [] unless ui
|
|
40
|
+
|
|
41
|
+
methods = [:"mcp_#{action_name}_#{ui}"]
|
|
42
|
+
methods << :"mcp_list_#{ui}" if action_name == 'show'
|
|
43
|
+
methods.uniq
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def ui_options(column)
|
|
47
|
+
column.public_send(:"#{tool.action_name}_ui_options") || column.options
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def presentation_method_name(column)
|
|
51
|
+
@presentation_method_names ||= {}.compare_by_identity
|
|
52
|
+
@presentation_method_names.fetch(column) do
|
|
53
|
+
@presentation_method_names[column] = resolve_presentation_method_name(column)
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def resolve_presentation_method_name(column)
|
|
36
58
|
column_method = column_method_name(column)
|
|
37
|
-
return
|
|
38
|
-
|
|
59
|
+
return column_method if respond_to?(column_method, true)
|
|
60
|
+
|
|
61
|
+
ui_method_names(column).find { |method_name| respond_to?(method_name, true) }
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def column_value(record, column, counts)
|
|
65
|
+
presentation_method = presentation_method_name(column)
|
|
66
|
+
if presentation_method == column_method_name(column)
|
|
67
|
+
return send(presentation_method, record, column)
|
|
68
|
+
elsif presentation_method
|
|
69
|
+
return send(
|
|
70
|
+
presentation_method,
|
|
71
|
+
record,
|
|
72
|
+
column,
|
|
73
|
+
ui_options: ui_options(column)
|
|
74
|
+
)
|
|
75
|
+
end
|
|
76
|
+
if column.association
|
|
77
|
+
return association_value(record, record.public_send(column.association.name), column,
|
|
78
|
+
counts)
|
|
79
|
+
end
|
|
39
80
|
|
|
40
81
|
record.public_send(column.name)
|
|
41
82
|
end
|
|
42
83
|
|
|
84
|
+
def mcp_list_active_storage_has_one(record, column, ui_options:)
|
|
85
|
+
attached = record.public_send(column.name)
|
|
86
|
+
return unless attached.attached?
|
|
87
|
+
|
|
88
|
+
active_storage_attachment_metadata(attached.attachment)
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def mcp_list_active_storage_has_many(record, column, ui_options:)
|
|
92
|
+
attached = record.public_send(column.name)
|
|
93
|
+
return unless attached.attached?
|
|
94
|
+
|
|
95
|
+
attached.attachments.map { |attachment| active_storage_attachment_metadata(attachment) }
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def mcp_list_paperclip(record, column, ui_options:)
|
|
99
|
+
attachment = record.public_send(column.name)
|
|
100
|
+
return unless attachment.file?
|
|
101
|
+
|
|
102
|
+
attachment_metadata(
|
|
103
|
+
filename: attachment.original_filename,
|
|
104
|
+
content_type: attachment.content_type,
|
|
105
|
+
byte_size: attachment.size,
|
|
106
|
+
updated_at: metadata_value(attachment, :updated_at)
|
|
107
|
+
)
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
def mcp_list_carrierwave(record, column, ui_options:)
|
|
111
|
+
uploader = record.public_send(column.name)
|
|
112
|
+
file = uploader.file
|
|
113
|
+
return if file.blank?
|
|
114
|
+
|
|
115
|
+
attachment_metadata(
|
|
116
|
+
filename: metadata_value(uploader, :identifier) || metadata_value(file, :filename),
|
|
117
|
+
content_type: metadata_value(file, :content_type),
|
|
118
|
+
byte_size: metadata_value(file, :size)
|
|
119
|
+
)
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
def mcp_list_dragonfly(record, column, ui_options:)
|
|
123
|
+
attachment = record.public_send(column.name)
|
|
124
|
+
return if attachment.blank?
|
|
125
|
+
|
|
126
|
+
attachment_metadata(
|
|
127
|
+
filename: metadata_value(attachment, :name),
|
|
128
|
+
content_type: metadata_value(attachment, :mime_type, :content_type),
|
|
129
|
+
byte_size: metadata_value(attachment, :size)
|
|
130
|
+
)
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
def mcp_list_download_link(record, column, ui_options:)
|
|
134
|
+
file_column_attachment_metadata(record, column)
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
alias mcp_list_download_link_with_filename mcp_list_download_link
|
|
138
|
+
alias mcp_list_thumbnail mcp_list_download_link
|
|
139
|
+
|
|
43
140
|
def association_value(record, value, column, counts)
|
|
44
141
|
if column.association.collection?
|
|
45
142
|
collection_association_value(record, value, column, counts)
|
|
@@ -59,36 +156,39 @@ module ActiveScaffold
|
|
|
59
156
|
count = association_size(record, value, column, counts) if associated_number
|
|
60
157
|
return associated_number ? count : nil if associated_limit.zero?
|
|
61
158
|
|
|
159
|
+
limited_collection_association_value(value, associated_limit, associated_number, count)
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
def limited_collection_association_value(value, associated_limit, associated_number, count)
|
|
62
163
|
loaded_limit = associated_number ? associated_limit : associated_limit + 1
|
|
63
164
|
associated_records = limited_associated_records(value, loaded_limit)
|
|
64
165
|
more_records = associated_number ? count > associated_limit : associated_records.size > associated_limit
|
|
65
166
|
associated_records = associated_records.first(associated_limit)
|
|
66
167
|
records = associated_records.map { |associated_record| associated_record_hash(associated_record) }
|
|
67
|
-
displayed_count =
|
|
68
|
-
if associated_number
|
|
69
|
-
count
|
|
70
|
-
elsif more_records
|
|
71
|
-
">#{associated_limit}"
|
|
72
|
-
else
|
|
73
|
-
records.size
|
|
74
|
-
end
|
|
75
168
|
|
|
76
169
|
{
|
|
77
170
|
(more_records ? :first_records : :records) => records,
|
|
78
|
-
count:
|
|
171
|
+
count: displayed_association_count(records, associated_limit, associated_number, count, more_records)
|
|
79
172
|
}
|
|
80
173
|
end
|
|
81
174
|
|
|
82
|
-
def
|
|
83
|
-
if
|
|
84
|
-
|
|
85
|
-
elsif
|
|
86
|
-
|
|
175
|
+
def displayed_association_count(records, associated_limit, associated_number, count, more_records)
|
|
176
|
+
if associated_number
|
|
177
|
+
count
|
|
178
|
+
elsif more_records
|
|
179
|
+
">#{associated_limit}"
|
|
87
180
|
else
|
|
88
|
-
|
|
181
|
+
records.size
|
|
89
182
|
end
|
|
90
183
|
end
|
|
91
184
|
|
|
185
|
+
def limited_associated_records(value, limit)
|
|
186
|
+
return value.first(limit) if value.respond_to?(:loaded?) && value.loaded?
|
|
187
|
+
return value.limit(limit).to_a if value.respond_to?(:limit)
|
|
188
|
+
|
|
189
|
+
value.first(limit)
|
|
190
|
+
end
|
|
191
|
+
|
|
92
192
|
def association_size(_record, value, column, counts)
|
|
93
193
|
return counts[column.name] if counts&.key?(column.name)
|
|
94
194
|
|
|
@@ -104,6 +204,37 @@ module ActiveScaffold
|
|
|
104
204
|
def model
|
|
105
205
|
active_scaffold_config.model
|
|
106
206
|
end
|
|
207
|
+
|
|
208
|
+
def active_storage_attachment_metadata(attachment)
|
|
209
|
+
blob = attachment.blob
|
|
210
|
+
attachment_metadata(
|
|
211
|
+
filename: blob.filename,
|
|
212
|
+
content_type: blob.content_type,
|
|
213
|
+
byte_size: blob.byte_size
|
|
214
|
+
)
|
|
215
|
+
end
|
|
216
|
+
|
|
217
|
+
def file_column_attachment_metadata(record, column)
|
|
218
|
+
value = record.public_send(column.name)
|
|
219
|
+
return unless value
|
|
220
|
+
|
|
221
|
+
attachment_metadata(filename: File.basename(value.to_s))
|
|
222
|
+
end
|
|
223
|
+
|
|
224
|
+
def attachment_metadata(filename:, content_type: nil, byte_size: nil, updated_at: nil)
|
|
225
|
+
updated_at = updated_at.iso8601 if updated_at.respond_to?(:iso8601)
|
|
226
|
+
{
|
|
227
|
+
filename: filename&.to_s,
|
|
228
|
+
content_type: content_type,
|
|
229
|
+
byte_size: byte_size,
|
|
230
|
+
updated_at: updated_at
|
|
231
|
+
}.compact
|
|
232
|
+
end
|
|
233
|
+
|
|
234
|
+
def metadata_value(object, *methods)
|
|
235
|
+
method = methods.find { |method_name| object.respond_to?(method_name) }
|
|
236
|
+
object.public_send(method) if method
|
|
237
|
+
end
|
|
107
238
|
end
|
|
108
239
|
end
|
|
109
240
|
end
|
|
@@ -8,9 +8,9 @@ module ActiveScaffold
|
|
|
8
8
|
module_function
|
|
9
9
|
|
|
10
10
|
def resolve(reference)
|
|
11
|
-
class_name, separator, id = reference.to_s.rpartition(
|
|
11
|
+
class_name, separator, id = reference.to_s.rpartition('#')
|
|
12
12
|
if separator.empty? || class_name.empty? || id.empty?
|
|
13
|
-
raise InvalidReference,
|
|
13
|
+
raise InvalidReference, 'MCP_USER must use CLASS#ID, for example: User#201'
|
|
14
14
|
end
|
|
15
15
|
|
|
16
16
|
user_class = class_name.safe_constantize
|
|
@@ -37,7 +37,7 @@ module ActiveScaffold
|
|
|
37
37
|
when :description, :label, :name
|
|
38
38
|
(value.is_a?(String) || value.is_a?(Symbol)) && value.to_s.present?
|
|
39
39
|
when :modules, :presenter_modules
|
|
40
|
-
Array(value).all?
|
|
40
|
+
Array(value).all?(Module)
|
|
41
41
|
when :presenter
|
|
42
42
|
value.is_a?(Class)
|
|
43
43
|
end
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require
|
|
3
|
+
require 'json'
|
|
4
4
|
|
|
5
5
|
module ActiveScaffold
|
|
6
6
|
module MCP
|
|
@@ -48,21 +48,22 @@ module ActiveScaffold
|
|
|
48
48
|
[:id, *self.class.mcp_lookup_column_names]
|
|
49
49
|
end
|
|
50
50
|
|
|
51
|
-
def resolve_lookup_id
|
|
51
|
+
def resolve_lookup_id # rubocop:disable Naming/PredicateMethod
|
|
52
52
|
return true if mcp_argument_provided?(:id)
|
|
53
53
|
|
|
54
54
|
column_name = self.class.mcp_lookup_column_names.find { |name| mcp_argument_provided?(name) }
|
|
55
55
|
# Resolve exact identifiers within the base scope, without list filters
|
|
56
56
|
# (including defaults) hiding an otherwise showable record.
|
|
57
57
|
ids = beginning_of_chain.where(column_name => public_send(column_name))
|
|
58
|
-
|
|
59
|
-
|
|
58
|
+
.limit(2)
|
|
59
|
+
.pluck(self.class.mcp_primary_key_name)
|
|
60
60
|
if ids.empty?
|
|
61
|
-
report_error
|
|
61
|
+
report_error "No record found for #{column_name} #{public_send(column_name).inspect}"
|
|
62
62
|
return false
|
|
63
63
|
end
|
|
64
64
|
if ids.many?
|
|
65
|
-
report_error
|
|
65
|
+
report_error "More than one record found for #{column_name} #{public_send(column_name).inspect}; " \
|
|
66
|
+
'use id instead'
|
|
66
67
|
return false
|
|
67
68
|
end
|
|
68
69
|
|