diff --git a/app/controllers/short_code_resolver_controller.rb b/app/controllers/short_code_resolver_controller.rb new file mode 100644 index 00000000..fa12a299 --- /dev/null +++ b/app/controllers/short_code_resolver_controller.rb @@ -0,0 +1,13 @@ +class ShortCodeResolverController < ApplicationController + skip_before_action :authenticate_activated_user! + + def resolve + # "short_code_resolver/resolve/:collection_code/:work_code" + raise ActiveRecord::RecordNotFound.new("not found") if params[:collection_code].blank? + + collection = Collection.find_by_unique_short_code!(params[:collection_code]) + work = collection.works_including_child_works.find_by_stock_number!(params[:work_code]) + + redirect_to collection_work_path(collection, work) + end +end diff --git a/app/models/collection.rb b/app/models/collection.rb index c9e3be40..2f79f513 100644 --- a/app/models/collection.rb +++ b/app/models/collection.rb @@ -25,9 +25,14 @@ # show_library :boolean # sort_works_by :string # supported_languages :text default(["\"nl\""]), is an Array +# unique_short_code :string # created_at :datetime not null # updated_at :datetime not null -# parent_collection_id :bigint default(7) +# parent_collection_id :bigint default(1) +# +# Indexes +# +# index_collections_on_unique_short_code (unique_short_code) UNIQUE # class Collection < ApplicationRecord diff --git a/config/routes.rb b/config/routes.rb index 637eaab2..1c73ff31 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -5,6 +5,7 @@ Sidekiq::Web.set :sessions, false Rails.application.routes.draw do + get "short_code_resolver/resolve/:collection_code/:work_code" => "short_code_resolver#resolve" get "application_status" => "status#application_status" authenticate :user, lambda { |u| u.admin? } do diff --git a/db/migrate/20231011205842_add_unique_short_code_to_collections.rb b/db/migrate/20231011205842_add_unique_short_code_to_collections.rb new file mode 100644 index 00000000..42a1145a --- /dev/null +++ b/db/migrate/20231011205842_add_unique_short_code_to_collections.rb @@ -0,0 +1,6 @@ +class AddUniqueShortCodeToCollections < ActiveRecord::Migration[7.0] + def change + add_column :collections, :unique_short_code, :string + add_index :collections, :unique_short_code, unique: true + end +end diff --git a/db/schema.rb b/db/schema.rb index 14c89682..2fc91bfb 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2023_07_14_154754) do +ActiveRecord::Schema[7.0].define(version: 2023_10_11_205842) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" enable_extension "tablefunc" @@ -175,6 +175,8 @@ t.text "supported_languages", default: ["nl"], array: true t.text "default_collection_attributes_for_artists", default: ["website", "email", "telephone_number", "description"], array: true t.text "default_collection_attributes_for_works", default: [], array: true + t.string "unique_short_code" + t.index ["unique_short_code"], name: "index_collections_on_unique_short_code", unique: true end create_table "collections_geoname_summaries", force: :cascade do |t| diff --git a/spec/fixtures/collections.yml b/spec/fixtures/collections.yml index 47644e3b..966d2f55 100644 --- a/spec/fixtures/collections.yml +++ b/spec/fixtures/collections.yml @@ -23,9 +23,14 @@ # show_library :boolean # sort_works_by :string # supported_languages :text default(["\"nl\""]), is an Array +# unique_short_code :string # created_at :datetime not null # updated_at :datetime not null -# parent_collection_id :bigint default(7) +# parent_collection_id :bigint default(1) +# +# Indexes +# +# index_collections_on_unique_short_code (unique_short_code) UNIQUE # root_collection: name: "-" @@ -61,6 +66,7 @@ collection_with_works_child: collection3: name: Collection 3 + unique_short_code: COL4 parent_collection: :root_collection collection_name_extended_cache: "\"Collection 3\"" diff --git a/spec/models/collection_spec.rb b/spec/models/collection_spec.rb index 3b499899..9088daf7 100644 --- a/spec/models/collection_spec.rb +++ b/spec/models/collection_spec.rb @@ -25,9 +25,14 @@ # show_library :boolean # sort_works_by :string # supported_languages :text default(["\"nl\""]), is an Array +# unique_short_code :string # created_at :datetime not null # updated_at :datetime not null -# parent_collection_id :bigint default(7) +# parent_collection_id :bigint default(1) +# +# Indexes +# +# index_collections_on_unique_short_code (unique_short_code) UNIQUE # require "rails_helper" diff --git a/spec/requests/short_code_resolver_spec.rb b/spec/requests/short_code_resolver_spec.rb new file mode 100644 index 00000000..afa3cf0a --- /dev/null +++ b/spec/requests/short_code_resolver_spec.rb @@ -0,0 +1,24 @@ +require "rails_helper" + +RSpec.describe "ShortCodeResolvers", type: :request do + describe "GET /resolve" do + let(:work) { works(:work_diptych_2) } + + it "returns http not found for unknown collection code" do + expect { + get "/short_code_resolver/resolve/ABC/123" + }.to raise_error(ActiveRecord::RecordNotFound) + end + + it "returns http not found for unknown work code" do + expect { + get "/short_code_resolver/resolve/COL4/123" + }.to raise_error(ActiveRecord::RecordNotFound) + end + + it "redirects when known combi is given" do + get "/short_code_resolver/resolve/#{work.collection.unique_short_code}/#{work.stock_number}" + expect(response).to redirect_to(collection_work_path(work.collection, work)) + end + end +end