47 行
1.9 KiB
Ruby
47 行
1.9 KiB
Ruby
# Only confirmed combinations belong here; all others retain the existing behaviour.
|
|
class CountsDefaults
|
|
POST_COLUMNS = ['resource_id', 'version_no', 'is_latest', 'title', 'url',
|
|
'thumbnail_base', 'tags', 'parents', 'original_created_from',
|
|
'original_created_before', 'event_type', 'user', 'created_at'].freeze
|
|
|
|
def self.entries(resource)
|
|
result = [{ definition: 'history', mode: 'summary', params: {
|
|
'rows' => [{ field: 'created_at', grain: 'month' }].to_json,
|
|
'cols' => [{ field: 'event_type' }].to_json,
|
|
'sort' => 'a0:desc', 'conditions' => '[]' } }]
|
|
if resource == 'posts'
|
|
result += [{ definition: 'latest', mode: 'summary', params: {
|
|
'rows' => '[]', 'cols' => '[]', 'conditions' => '[]', 'tags' => '' } },
|
|
{ definition: 'latest', mode: 'details', params: {
|
|
'columns' => POST_COLUMNS.join(','), 'order' => POST_COLUMNS.join(','),
|
|
'children' => 'tags.name,parents.id', 'nested_json' => '0',
|
|
'conditions' => '[]', 'tags' => '' } }]
|
|
end
|
|
result
|
|
end
|
|
|
|
def self.for(params)
|
|
settings = entries(params.fetch('resource', 'posts')).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')
|
|
actual = JSON.parse(params['rows'])
|
|
expected = JSON.parse(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)
|
|
entries(resource).map do |entry|
|
|
entry.merge(params: entry[:params].map { |key, value| { key:, value: } })
|
|
end
|
|
end
|
|
end
|