Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/assets/builds/tailwind.css

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions app/avo/actions/archive_location.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Avo::Actions::ArchiveLocation < Avo::BaseAction
self.name = -> { I18n.t("admin.locations.archive") }
self.message = -> { I18n.t("admin.locations.archive_message") }
self.authorize = -> { AdminPolicy.new(current_user).update? }

def handle(query:, current_user:, **)
archived = query.reject(&:archived?).select { |location| AdminPolicy.new(current_user, location).update? }
archived.each(&:archive!)
succeed I18n.t("admin.locations.archived", count: archived.size)
end
end
10 changes: 10 additions & 0 deletions app/avo/actions/restore_location.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Avo::Actions::RestoreLocation < Avo::BaseAction
self.name = -> { I18n.t("admin.locations.restore") }
self.authorize = -> { AdminPolicy.new(current_user).update? }

def handle(query:, current_user:, **)
restored = query.select(&:archived?).select { |location| AdminPolicy.new(current_user, location).update? }
restored.each(&:restore!)
succeed I18n.t("admin.locations.restored", count: restored.size)
end
end
19 changes: 19 additions & 0 deletions app/avo/filters/location_archived.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Avo::Filters::LocationArchived < Avo::Filters::SelectFilter
self.name = -> { I18n.t("admin.locations.status") }

def apply(_request, query, value)
case value
when "archived" then query.archived
when "all" then query
else query.not_archived
end
end

def default
"active"
end

def options
%w[active archived all].index_with { |status| I18n.t("admin.locations.statuses.#{status}") }
end
end
11 changes: 11 additions & 0 deletions app/avo/filters/location_city.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Avo::Filters::LocationCity < Avo::Filters::SelectFilter
self.name = -> { I18n.t("admin.locations.city") }

def apply(_request, query, value)
value.present? ? query.where(city: value) : query
end

def options
Location.where.not(city: [ nil, "" ]).distinct.order(:city).pluck(:city).index_with(&:itself)
end
end
20 changes: 20 additions & 0 deletions app/avo/filters/location_photos.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Places that still need photos, the old curator area's "needs photos" list.
class Avo::Filters::LocationPhotos < Avo::Filters::SelectFilter
self.name = -> { I18n.t("admin.locations.photos") }

def apply(_request, query, value)
return query if value.blank?

counts = ActiveStorage::Attachment.where(record_type: "Location", name: "photos")
.group(:record_id).select(:record_id)
case value
when "none" then query.where.not(id: counts)
when "few" then query.where.not(id: counts.having("COUNT(*) >= 3"))
else query
end
end

def options
%w[none few].index_with { |bucket| I18n.t("admin.locations.photo_buckets.#{bucket}") }
end
end
63 changes: 63 additions & 0 deletions app/avo/resources/location.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
class Avo::Resources::Location < Avo::BaseResource
self.icon = "tabler/outline/map-pin"
self.title = :name
self.includes = [ :translations ]
self.attachments = [ :photos ]
# URLs carry the public uuid (Identifiable#to_param), never the database id.
self.find_record_method = -> { id.is_a?(Array) ? query.where(uuid: id) : query.find_by!(uuid: id) }
self.search = {
query: -> { query.where("locations.name ILIKE :q OR locations.city ILIKE :q", q: "%#{params[:q]}%") }
}

TRANSLATED = %i[name description historical_context].freeze

def fields
main_panel
field :photos, as: :files, is_image: true, hide_on: :index, name: I18n.t("admin.locations.photos")
translations_tabs
end

def filters
filter Avo::Filters::LocationCity
filter Avo::Filters::LocationArchived
filter Avo::Filters::LocationPhotos
end

def actions
action Avo::Actions::ArchiveLocation
action Avo::Actions::RestoreLocation
end

private

def main_panel
field :name, as: :text, required: true, sortable: true, link_to_record: true
field :city, as: :text, sortable: true
field :lat, as: :number, step: 0.000001, hide_on: :index, help: I18n.t("admin.locations.coordinates_help")
field :lng, as: :number, step: 0.000001, hide_on: :index
field :budget, as: :select, enum: ::Location.budgets, hide_on: :index
field :short_description, as: :textarea, hide_on: :index
field :phone, as: :text, hide_on: :index
field :email, as: :text, hide_on: :index
field :website, as: :text, hide_on: :index
field :video_url, as: :text, hide_on: :index
field :photo_count, as: :number, name: I18n.t("admin.locations.photo_count"), only_on: :index,
format_using: -> { record.photos.size }
field :archived_at, as: :date_time, readonly: true, hide_on: :forms
field :ai_generated, as: :boolean, readonly: true, hide_on: :forms
field :average_rating, as: :number, readonly: true, hide_on: :forms
end

def translations_tabs
tabs do
Translation::SUPPORTED_LOCALES.each do |locale|
tab title: locale.upcase do
TRANSLATED.each do |attribute|
field :"#{attribute}_#{locale}", as: (attribute == :name ? :text : :textarea),
name: "#{I18n.t("admin.locations.fields.#{attribute}")} (#{locale})", hide_on: :index
end
end
end
end
end
end
3 changes: 3 additions & 0 deletions app/controllers/avo/locations_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Avo::LocationsController < Avo::ResourcesController
include AdminResource
end
2 changes: 1 addition & 1 deletion app/controllers/avo/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
class Avo::UsersController < Avo::ResourcesController
include AdminPolicyGate
include AdminResource
end
16 changes: 0 additions & 16 deletions app/controllers/concerns/admin_policy_gate.rb

This file was deleted.

21 changes: 21 additions & 0 deletions app/controllers/concerns/admin_resource.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# What every Avo resource controller shares. The resource's policy is asked
# before each request, after Avo has loaded the record, and whatever the policy
# does not allow is refused. The signed-in user is recorded as the editor, so
# models can tell a person's change in the admin from the AI pipeline's.
module AdminResource
extend ActiveSupport::Concern

included do
# Prepended, because Avo copies the form onto the record in a before_action
# of its own, and the models need to know the editor while that happens.
prepend_before_action { Current.editor = _current_user }
before_action :enforce_admin_policy
end

private

def enforce_admin_policy
policy = AdminPolicy.for(@resource.model_class).new(_current_user, @record)
raise Avo::NotAuthorizedError unless policy.allows?(action_name)
end
end
44 changes: 36 additions & 8 deletions app/models/concerns/translatable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# location.set_translation(:name, "Stari Most", :hr)
# location.translate(:name, :hr) # => "Stari Most"
# location.name_hr # => "Stari Most"
# location.name_hr = "Stari Most" # Sets translation
# location.name_hr = "Stari Most" # Staged; saved with location.save
#
module Translatable
extend ActiveSupport::Concern
Expand All @@ -34,7 +34,7 @@ def self.request_locale_chain
end

included do
has_many :translations, as: :translatable, dependent: :destroy
has_many :translations, as: :translatable, dependent: :destroy, autosave: true

# Preloading `translations` pulls every locale of every field — sixteen
# locales' worth to render one card. This one holds only what the current
Expand All @@ -54,17 +54,17 @@ def translates(*fields)
self.translatable_fields = fields.map(&:to_sym)

fields.each do |field|
# Define getter methods for each locale
# e.g., name_hr, name_de, etc.
# Per-language accessors (name_hr, name_de, ...) read and write exactly
# one language, with no fallback, which is what an edit form needs: an
# empty German field must not show the English text and then save it
# as German. Reading with fallbacks is what `translate` is for.
Translation::SUPPORTED_LOCALES.each do |locale|
# Getter: location.name_hr
define_method("#{field}_#{locale}") do
translate(field, locale)
stored_translation(field, locale)&.value
end

# Setter: location.name_hr = "value"
define_method("#{field}_#{locale}=") do |value|
set_translation(field, value, locale)
write_translation(field, locale, value)
end
end
end
Expand Down Expand Up @@ -131,6 +131,34 @@ def set_translation(field, value, locale = I18n.locale)
translation
end

# Stages one language of one field on the record, saved with it (autosave),
# so a record that fails validation leaves its translations untouched too.
# An unchanged value is left alone, a blank one removes the translation, and a
# change made by a person in the admin (Current.editor) is marked as theirs.
def write_translation(field, locale, value)
field = field.to_s
locale = locale.to_s
existing = stored_translation(field, locale)

if value.blank?
existing&.mark_for_destruction
return
end
return if existing && existing.value == value

translation = existing || translations.build(field_name: field, locale: locale)
translation.value = value
translation.human_edited_at = Time.current if Current.editor
end

def stored_translation(field, locale)
field = field.to_s
locale = locale.to_s
translations.detect do |translation|
translation.field_name == field && translation.locale == locale && !translation.marked_for_destruction?
end
end

# Set multiple translations at once
#
# @param translations_hash [Hash] hash with field names as keys and values
Expand Down
6 changes: 6 additions & 0 deletions app/models/current.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Who is making a change in this request, when it matters to the models: an
# edit made by a person in the admin is marked as theirs (a hand-edited
# translation, later an admin event).
class Current < ActiveSupport::CurrentAttributes
attribute :editor
end
26 changes: 26 additions & 0 deletions config/locales/bs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,32 @@ bs:
one: Jedan nalog je odblokiran.
other: "Odblokirano naloga: %{count}."
refused: "Nije promijenjeno: %{names}. Administrator ne može mijenjati svoj nalog, a odblokirati se mogu samo blokirani nalozi."
locations:
photos: Fotografije
photo_count: Fotografije
coordinates_help: Geografska širina i dužina. Mjesto u području sumnjivom na mine ne može se sačuvati.
fields:
name: Naziv
description: Opis
historical_context: Historijski kontekst
city: Grad
status: Status
statuses:
active: Aktivna
archived: Arhivirana
all: Sva
photo_buckets:
none: Bez fotografija
few: Manje od tri fotografije
archive: Arhiviraj
archive_message: Arhivirano mjesto nestaje sa stranice i iz pretrage, ali putnici zadržavaju svoje posjete i uspomene. Može se vratiti.
archived:
one: Jedno mjesto je arhivirano.
other: "Arhivirano mjesta: %{count}."
restore: Vrati
restored:
one: Jedno mjesto je vraćeno.
other: "Vraćeno mjesta: %{count}."
auth:
register_title: Registracija - Usput
register_heading: Kreiraj nalog
Expand Down
26 changes: 26 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -834,6 +834,32 @@ en:
one: One account unblocked.
other: "%{count} accounts unblocked."
refused: "Left unchanged: %{names}. An admin cannot act on their own account, and only blocked accounts can be unblocked."
locations:
photos: Photos
photo_count: Photos
coordinates_help: Latitude and longitude. A place inside a mine-suspected area cannot be saved.
fields:
name: Name
description: Description
historical_context: Historical context
city: City
status: Status
statuses:
active: Active
archived: Archived
all: All
photo_buckets:
none: No photos
few: Fewer than three photos
archive: Archive
archive_message: An archived place leaves the site and search, but travellers keep their check-ins and moments. It can be restored.
archived:
one: One place archived.
other: "%{count} places archived."
restore: Restore
restored:
one: One place restored.
other: "%{count} places restored."
auth:
register_title: Register - Usput
register_heading: Create account
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddHumanEditedAtToTranslations < ActiveRecord::Migration[8.1]
def change
add_column :translations, :human_edited_at, :datetime
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[8.1].define(version: 2026_09_30_150000) do
ActiveRecord::Schema[8.1].define(version: 2026_09_30_160000) do
# These are extensions that must be enabled in order to support this database
enable_extension "pg_catalog.plpgsql"

Expand Down Expand Up @@ -580,6 +580,7 @@
create_table "translations", force: :cascade do |t|
t.datetime "created_at", null: false
t.string "field_name", limit: 50, null: false
t.datetime "human_edited_at"
t.string "locale", limit: 10, null: false
t.bigint "translatable_id", null: false
t.string "translatable_type", null: false
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
title: Editing place translations in the admin, from the operator
date: 2026-09-30
via: conversation with the agent
---
# Place translations in Avo, 2026-09-30

Before building the places page in the admin, the agent asked two questions,
one at a time.

1. Which languages curators may edit by hand in the Avo places page. The
options were Bosnian and English with the rest read-only, Bosnian only,
or all 16. Answer: "All 16 languages".
2. What a later AI retranslation should do with a translation a curator
edited by hand. The options were to keep hand edits, to let the AI
overwrite them, or to ask each time. Answer: "Ask each time": the
retranslate action lists the hand-edited languages and the curator
chooses which to overwrite.
Loading
Loading