95 行
4.1 KiB
Ruby
95 行
4.1 KiB
Ruby
# Only confirmed combinations belong here; all others retain the existing behaviour.
|
|
class CountsDefaults
|
|
POST_COLUMNS = ['resource_id', 'version_no', 'latest', 'title', 'url',
|
|
'thumbnail_base', 'tags', 'parents', 'original_created_from',
|
|
'original_created_before', 'event_type', 'user', 'created_at'].freeze
|
|
|
|
def self.entries(resource, locale: 'ja')
|
|
result = [{ definition: 'history', mode: 'summary', params: {
|
|
'rows' => 'created_at:month',
|
|
'cols' => 'event_type',
|
|
'sort' => 'a0:desc', 'conditions' => '' } }]
|
|
result << { definition: 'latest', mode: 'summary', params: {
|
|
'rows' => '', 'cols' => '', 'conditions' => '', 'tags' => '' } }
|
|
details = detail_params(resource)
|
|
if details
|
|
details = details.merge('sort' => 'resource_id:asc version_no:asc')
|
|
language = Locale.find_by(code: locale)&.language_code || locale
|
|
name = CountsResourceDefinition.name_key(language)
|
|
aliases = CountsResourceDefinition.aliases_key(language)
|
|
details = details.transform_values do |value|
|
|
value.split(' ').flat_map do |token|
|
|
if resource == 'tags' && ['name', 'parents.name'].include?(token)
|
|
token == 'name' ? name : ["parents.#{ name }", "parents.#{ aliases }"]
|
|
elsif resource == 'tags' && token == 'aliases'
|
|
aliases
|
|
elsif resource == 'tags' && token.start_with?('aliases.')
|
|
[]
|
|
elsif resource == 'posts' && token == 'tags.name'
|
|
"tags.#{ name }"
|
|
else token
|
|
end
|
|
end.join(' ')
|
|
end
|
|
if ['posts', 'tags'].include?(resource)
|
|
fields = CountsResourceDefinition.new(resource).output_fields
|
|
selected = details.fetch('columns').split(' ')
|
|
details['columns'] = fields.map { _1[:key] }.select { selected.include?(_1) }.join(' ')
|
|
order = lambda do |items, prefix|
|
|
items.flat_map do |field|
|
|
path = [prefix, field[:key]].compact.join('.')
|
|
[path, *order.call(field.fetch(:children, []), path)]
|
|
end
|
|
end
|
|
details['order'] = order.call(fields, nil).join(' ')
|
|
end
|
|
['latest', 'history'].each do |definition|
|
|
result << { definition:, mode: 'details', params: details }
|
|
end
|
|
end
|
|
result
|
|
end
|
|
|
|
def self.detail_params(resource)
|
|
if resource == 'posts'
|
|
return { 'columns' => POST_COLUMNS.join(' '), 'order' => POST_COLUMNS.join(' '),
|
|
'children' => 'tags.name parents.id', 'nested_json' => '0',
|
|
'conditions' => '', 'tags' => '' }
|
|
end
|
|
unless resource == 'tags'
|
|
return { 'columns' => CountsExportProjection.default_columns(resource).join(' '),
|
|
'children' => CountsExportProjection.default_children(resource).join(' ') }
|
|
end
|
|
|
|
columns = ['resource_id', 'version_no', 'latest', 'name', 'category',
|
|
'aliases', 'parents', 'deprecated', 'event_type', 'user', 'created_at']
|
|
{ 'columns' => columns.join(' '), 'order' => columns.join(' '),
|
|
'children' => 'aliases.name parents.name', 'nested_json' => '0' }
|
|
end
|
|
|
|
def self.for(params)
|
|
defaults = entries(params.fetch('resource', 'posts'), locale: params.fetch('locale', 'ja'))
|
|
settings = defaults.find do |entry|
|
|
entry[:definition] == params.fetch('definition', 'latest') &&
|
|
entry[:mode] == params.fetch('mode', 'summary')
|
|
end&.fetch(:params) || {}
|
|
if settings.key?('sort') && params.key?('rows') && params.fetch('mode', 'summary') == 'summary'
|
|
actual = QueryState.axes(params['rows'])
|
|
expected = QueryState.axes(settings.fetch('rows'))
|
|
compatible = actual.is_a?(Array) && actual.first.is_a?(Hash) &&
|
|
actual.first['field'] == expected.first['field'] &&
|
|
actual.first.fetch('grain', 'month') == expected.first['grain']
|
|
unless compatible
|
|
settings = settings.except('sort')
|
|
end
|
|
end
|
|
settings
|
|
end
|
|
|
|
def self.metadata(resource, locale: 'ja')
|
|
entries(resource, locale:).map do |entry|
|
|
entry.merge(params: entry[:params].map { |key, value| { key:, value: } })
|
|
end
|
|
end
|
|
end
|