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
15 changes: 15 additions & 0 deletions app/avo/filters/plan_ownership.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Avo::Filters::PlanOwnership < Avo::Filters::SelectFilter
self.name = -> { I18n.t("admin.plans.ownership") }

def apply(_request, query, value)
case value
when "curated" then query.where(user_id: nil)
when "travellers" then query.where.not(user_id: nil)
else query
end
end

def options
%w[curated travellers].index_with { |kind| I18n.t("admin.plans.ownerships.#{kind}") }
end
end
28 changes: 28 additions & 0 deletions app/avo/resources/experience.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class Avo::Resources::Experience < Avo::BaseResource
include Avo::TranslatedFields

self.icon = "tabler/outline/route"
self.title = :title
self.includes = [ :translations ]
self.find_record_method = -> { id.is_a?(Array) ? query.where(uuid: id) : query.find_by!(uuid: id) }
self.search = {
query: -> { query.where("experiences.title ILIKE :q", q: "%#{params[:q]}%") }
}

def fields
field :title, as: :text, required: true, sortable: true, link_to_record: true
field :experience_category_id, as: :select, name: I18n.t("admin.experiences.category"), include_blank: true,
options: -> { ExperienceCategory.order(:name).map { |category| [ category.name, category.id ] }.to_h }
field :estimated_duration, as: :number, name: I18n.t("admin.experiences.duration"), hide_on: :index
field :cover_photo, as: :file, is_image: true, hide_on: :index
field :contact_name, as: :text, hide_on: :index
field :contact_phone, as: :text, hide_on: :index
field :contact_email, as: :text, hide_on: :index
field :contact_website, as: :text, hide_on: :index
field :ai_generated, as: :boolean, readonly: true, hide_on: :forms
field :average_rating, as: :number, readonly: true, hide_on: :forms
translation_tabs :title, :description, long: %i[description]
field :locations, as: :has_many, through: :experience_locations, name: I18n.t("admin.experiences.places"),
attach_fields: -> { field :position, as: :number, default: 0 }
end
end
19 changes: 3 additions & 16 deletions app/avo/resources/location.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class Avo::Resources::Location < Avo::BaseResource
include Avo::TranslatedFields

self.icon = "tabler/outline/map-pin"
self.title = :name
self.includes = [ :translations ]
Expand All @@ -9,12 +11,10 @@ class Avo::Resources::Location < Avo::BaseResource
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
translation_tabs :name, :description, :historical_context, long: %i[description historical_context]
end

def filters
Expand Down Expand Up @@ -47,17 +47,4 @@ def main_panel
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
42 changes: 42 additions & 0 deletions app/avo/resources/plan.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
class Avo::Resources::Plan < Avo::BaseResource
include Avo::TranslatedFields

self.icon = "tabler/outline/map"
self.title = :title
self.includes = [ :translations, :user ]
# Curated plans and travellers' public plans only; a private plan is not
# found even by its link (PlanPolicy.visible).
self.index_query = -> { PlanPolicy.visible(query) }
self.find_record_method = -> {
scope = PlanPolicy.visible(query)
id.is_a?(Array) ? scope.where(uuid: id) : scope.find_by!(uuid: id)
}
self.search = {
query: -> { PlanPolicy.visible(query).where("plans.title ILIKE :q OR plans.city_name ILIKE :q", q: "%#{params[:q]}%") }
}

def fields
field :title, as: :text, required: true, sortable: true, link_to_record: true
field :city_name, as: :text, name: I18n.t("admin.plans.city"), sortable: true
field :visibility, as: :select, enum: ::Plan.visibilities, hide_on: :index
field :owner, as: :text, name: I18n.t("admin.plans.owner"), readonly: true, hide_on: :forms,
format_using: -> { record.user&.username || I18n.t("admin.plans.curated") }
field :ai_generated, as: :boolean, readonly: true, hide_on: :forms
translation_tabs :title, :notes, long: %i[notes]
field :experiences, as: :has_many, through: :plan_experiences, name: I18n.t("admin.plans.experiences"),
attach_fields: -> {
field :day_number, as: :number, default: 1, name: I18n.t("admin.plans.day")
field :position, as: :number, default: 0
}
field :location_items, as: :has_many, through: :plan_locations, name: I18n.t("admin.plans.places"),
use_resource: Avo::Resources::Location,
attach_fields: -> {
field :day_number, as: :number, default: 1, name: I18n.t("admin.plans.day")
field :position, as: :number, default: 0
}
end

def filters
filter Avo::Filters::PlanOwnership
end
end
16 changes: 16 additions & 0 deletions app/avo/translated_fields.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# One tab per supported language, with the resource's translated attributes,
# each read and written exactly in that language (Translatable accessors).
module Avo::TranslatedFields
def translation_tabs(*attributes, long: [])
tabs do
Translation::SUPPORTED_LOCALES.each do |locale|
tab title: locale.upcase do
attributes.each do |attribute|
field :"#{attribute}_#{locale}", as: (long.include?(attribute) ? :textarea : :text),
name: "#{I18n.t("admin.fields.#{attribute}")} (#{locale})", hide_on: :index
end
end
end
end
end
end
3 changes: 3 additions & 0 deletions app/controllers/avo/experiences_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Avo::ExperiencesController < Avo::ResourcesController
include AdminResource
end
3 changes: 3 additions & 0 deletions app/controllers/avo/plans_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Avo::PlansController < Avo::ResourcesController
include AdminResource
end
18 changes: 18 additions & 0 deletions app/controllers/concerns/admin_association_gate.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Avo attaches and detaches related records (a plan's experiences, an
# experience's places) through its own associations controller. Linking or
# unlinking counts as editing the parent, so the parent's policy decides.
module AdminAssociationGate
extend ActiveSupport::Concern

included do
before_action :enforce_parent_policy
end

private

def enforce_parent_policy
policy = AdminPolicy.for(@resource.model_class).new(_current_user, @record)
allowed = action_name.in?(%w[index show]) ? policy.show? : policy.update?
raise Avo::NotAuthorizedError unless allowed
end
end
17 changes: 17 additions & 0 deletions app/policies/plan_policy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Curated plans (no traveller owns them) are the admin's to create and edit.
# Travellers' public plans can be looked at, so a curator can spot a problem,
# but never changed there; travellers' private plans never reach the admin.
class PlanPolicy < AdminPolicy
def self.visible(query)
query.where(user_id: nil).or(query.visibility_public_plan)
end

def show? = curator? && visible_record?
def update? = curator? && curated?
def destroy? = admin? && curated?

private

def curated? = record.nil? || record.user_id.nil?
def visible_record? = record.nil? || record.user_id.nil? || record.visibility_public_plan?
end
4 changes: 4 additions & 0 deletions config/initializers/avo_associations.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# The policy gate on Avo's own associations controller (see AdminAssociationGate).
Rails.application.config.to_prepare do
Avo::AssociationsController.include(AdminAssociationGate) unless Avo::AssociationsController < AdminAssociationGate
end
25 changes: 21 additions & 4 deletions config/locales/bs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,12 @@ bs:
published: Uspomena podijeljena — curator će je pregledati prije nego postane javna
unpublished: Uspomena je ponovo privatna
admin:
fields:
name: Naziv
title: Naslov
description: Opis
historical_context: Historijski kontekst
notes: Bilješke
welcome:
title: Dobrodošli
greeting: Zdravo, %{name}
Expand Down Expand Up @@ -642,10 +648,6 @@ bs:
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:
Expand All @@ -664,6 +666,21 @@ bs:
restored:
one: Jedno mjesto je vraćeno.
other: "Vraćeno mjesta: %{count}."
experiences:
category: Kategorija
duration: Trajanje (minute)
places: Mjesta
plans:
city: Grad
owner: Vlasnik
curated: Uredio Usput
experiences: Iskustva
places: Mjesta
day: Dan
ownership: Autor
ownerships:
curated: Usput
travellers: Putnici (javni)
auth:
register_title: Registracija - Usput
register_heading: Kreiraj nalog
Expand Down
25 changes: 21 additions & 4 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,12 @@ en:
updated: Experience was successfully updated
deleted: Experience was successfully deleted
admin:
fields:
name: Name
title: Title
description: Description
historical_context: Historical context
notes: Notes
welcome:
title: Welcome
greeting: Hello, %{name}
Expand Down Expand Up @@ -838,10 +844,6 @@ en:
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:
Expand All @@ -860,6 +862,21 @@ en:
restored:
one: One place restored.
other: "%{count} places restored."
experiences:
category: Category
duration: Duration (minutes)
places: Places
plans:
city: City
owner: Owner
curated: Curated by usput
experiences: Experiences
places: Places
day: Day
ownership: Made by
ownerships:
curated: Usput
travellers: Travellers (public)
auth:
register_title: Register - Usput
register_heading: Create account
Expand Down
11 changes: 11 additions & 0 deletions sources/conversations/2026-09-30--usput--plans-in-avo.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
title: Which plans the admin shows, from the operator
date: 2026-09-30
via: conversation with the agent
---
# Plans in Avo, 2026-09-30

Before building the plans page in the admin, the agent asked which plans it
should list. The options were curated plans only (the ones no traveller owns),
curated plans plus travellers' public plans read-only, or every plan including
private ones. Answer: "Curated + travellers' public".
82 changes: 82 additions & 0 deletions test/integration/admin_experiences_plans_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# frozen_string_literal: true

require "test_helper"

class AdminExperiencesPlansTest < ActionDispatch::IntegrationTest
setup do
@curator = person(:curator)
@traveller = User.create!(username: "aep_traveller", email: "aep_traveller@example.com", password: "password123")
@place = Location.create!(name: "Stari most", city: "Mostar", lat: 43.3372, lng: 17.8150)
@experience = Experience.create!(title: "Mostar walk")
@curated = Plan.create!(title: "Curated weekend", city_name: "Mostar")
@shared = Plan.create!(title: "Shared trip", city_name: "Mostar", user: @traveller, visibility: :public_plan)
@private = Plan.create!(title: "Secret trip", city_name: "Mostar", user: @traveller)
sign_in @curator
end

test "a curator edits an experience and its translations" do
patch "/admin/resources/experiences/#{@experience.to_param}", params: { experience: { title: "Mostar old town walk", title_de: "Altstadtspaziergang" } }

@experience.reload
assert_equal "Mostar old town walk", @experience.title
assert_equal "Altstadtspaziergang", @experience.title_de
end

test "a curator adds a place to an experience at a position" do
post "/admin/resources/experiences/#{@experience.to_param}/locations",
params: { fields: { related_id: @place.to_param, position: 2 } }

assert_equal [ [ @place.id, 2 ] ], @experience.experience_locations.pluck(:location_id, :position)
end

test "the plans list holds curated and public plans, never private ones" do
get "/admin/resources/plans"

assert_response :success
assert_includes response.body, "Curated weekend"
assert_includes response.body, "Shared trip"
assert_not_includes response.body, "Secret trip"
end

test "a private plan is not found even by its link" do
get "/admin/resources/plans/#{@private.to_param}"

assert_response :not_found
end

test "a curator edits a curated plan but not a traveller's" do
patch "/admin/resources/plans/#{@curated.to_param}", params: { plan: { title: "Curated long weekend" } }
assert_equal "Curated long weekend", @curated.reload.title

patch "/admin/resources/plans/#{@shared.to_param}", params: { plan: { title: "Hijacked" } }
assert_equal "Shared trip", @shared.reload.title
end

test "a curator adds an experience to a curated plan on a given day" do
post "/admin/resources/plans/#{@curated.to_param}/experiences",
params: { fields: { related_id: @experience.to_param, day_number: 2, position: 1 } }

assert_equal [ [ @experience.id, 2, 1 ] ], @curated.plan_experiences.pluck(:experience_id, :day_number, :position)
end

test "nothing can be attached to a traveller's plan" do
post "/admin/resources/plans/#{@shared.to_param}/experiences",
params: { fields: { related_id: @experience.to_param, day_number: 1, position: 1 } }

assert_empty @shared.plan_experiences
end

test "a curator cannot delete a plan or an experience" do
delete "/admin/resources/plans/#{@curated.to_param}"
delete "/admin/resources/experiences/#{@experience.to_param}"

assert Plan.exists?(@curated.id)
assert Experience.exists?(@experience.id)
end

private

def person(role)
User.create!(username: "aep_#{role}", email: "aep_#{role}@example.com", password: "password123", user_type: role).tap(&:confirm)
end
end
Loading
Loading