Files

87 lines
2.7 KiB
Ruby

class EntriesController < ApplicationController
before_action :set_entry, only: [ :show, :edit, :update ]
def index
@language_code = validate_language_code(params[:language].presence)
@category = params[:category].presence
@query = params[:q].to_s.strip
@starts_with = params[:starts_with].presence
entries_scope = Entry.active_entries
entries_scope = entries_scope.with_category(@category)
entries_scope = entries_scope.search(@query, language_code: @language_code)
entries_scope = entries_scope.starts_with(@starts_with, language_code: @language_code) if @starts_with.present?
entries_scope = entries_scope.alphabetical_for(@language_code) if @query.blank? && @starts_with.blank? && @language_code.present?
entries_scope = entries_scope.order(created_at: :desc) if entries_scope.order_values.empty?
@pagy, @entries = pagy(entries_scope, items: 25)
@total_entries = @pagy.count
@entry_count = Entry.active_entries.count
@requested_count = Entry.requested.count
@verified_count = Entry.active_entries.where(verified: true).count
@needs_review_count = @entry_count - @verified_count
@complete_entries_count = supported_languages.reduce(Entry.active_entries) do |scope, language|
scope.where.not(language.code => [ nil, "" ])
end.count
@missing_entries_count = @entry_count - @complete_entries_count
@language_completion = supported_languages.index_with do |language|
next 0 if @entry_count.zero?
(Entry.active_entries.where.not(language.code => [ nil, "" ]).count * 100.0 / @entry_count).round
end
if @language_code.present?
primary_language, other_languages = supported_languages.partition { |language| language.code == @language_code }
@display_languages = primary_language + other_languages
else
@display_languages = supported_languages
end
respond_to do |format|
format.html
format.turbo_stream
end
end
def show
end
def edit
end
def update
if @entry.update(entry_params)
redirect_to entry_path(@entry), notice: "Entry updated."
else
render :edit, status: :unprocessable_entity
end
end
def download
@entries = Entry.active_entries.order(:id)
respond_to do |format|
format.xlsx do
filename = "sanasto-entries-#{Time.zone.today}.xlsx"
response.headers["Content-Disposition"] = "attachment; filename=\"#{filename}\""
end
end
end
private
def set_entry
@entry = Entry.find(params[:id])
end
def entry_params
params.require(:entry).permit(:category, :fi, :en, :sv, :no, :ru, :de, :notes)
end
def validate_language_code(code)
return nil if code.blank?
SupportedLanguage.valid_codes.include?(code) ? code : nil
end
end