class GekanatorQuestionsController < ApplicationController def index return head :not_found unless current_user&.admin? questions = GekanatorQuestion.accepted.order(priority_weight: :desc, id: :asc) render json: { questions: questions.map { |question| question_json(question) } } end private def question_json question { id: question_id_for(question), text: question.text, kind: question.kind, condition: condition_json(question.condition), source: question.source, priority_weight: question.priority_weight } end def question_id_for question condition = condition_json(question.condition).deep_symbolize_keys case condition[:type] when 'tag' "tag:#{ condition[:key] }" when 'source' "source:#{ condition[:host] }" when 'original-year' "original-year:#{ condition[:year] }" when 'original-month' "original-month:#{ condition[:month] }" when 'original-month-day' "original-month-day:#{ condition[:monthDay] || condition[:month_day] }" when 'title-length-greater-than' "title:length-greater-than:#{ condition[:length] }" when 'title-has-ascii' 'title:ascii' else "catalog:#{ question.id }" end end def condition_json condition json = condition.deep_dup.as_json if json['type'] == 'original-month-day' && json['monthDay'].blank? json['monthDay'] = json.delete('month_day') end json end end