Skip to content

Commit

Permalink
short code resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
murb committed Oct 11, 2023
1 parent 6ac1765 commit 1e7c5d9
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 4 deletions.
13 changes: 13 additions & 0 deletions app/controllers/short_code_resolver_controller.rb
Original file line number Diff line number Diff line change
@@ -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
7 changes: 6 additions & 1 deletion app/models/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
4 changes: 3 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[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"
Expand Down Expand Up @@ -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|
Expand Down
8 changes: 7 additions & 1 deletion spec/fixtures/collections.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: "-"
Expand Down Expand Up @@ -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\""

Expand Down
7 changes: 6 additions & 1 deletion spec/models/collection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
24 changes: 24 additions & 0 deletions spec/requests/short_code_resolver_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 1e7c5d9

Please sign in to comment.