From bfe0d770ff09297e53d2f00c49b70b686a51d88d Mon Sep 17 00:00:00 2001 From: Martin Fenner Date: Sat, 11 Jan 2020 10:51:14 +0100 Subject: [PATCH] generate random repository id. #335 --- app/controllers/providers_controller.rb | 2 +- app/controllers/repositories_controller.rb | 7 ++++- app/models/concerns/helpable.rb | 4 +++ config/routes.rb | 1 + db/schema.rb | 34 +++++++--------------- spec/concerns/helpable_spec.rb | 8 ++++- 6 files changed, 30 insertions(+), 26 deletions(-) diff --git a/app/controllers/providers_controller.rb b/app/controllers/providers_controller.rb index bdecb2434..eecf117b3 100644 --- a/app/controllers/providers_controller.rb +++ b/app/controllers/providers_controller.rb @@ -5,7 +5,7 @@ class ProvidersController < ApplicationController prepend_before_action :authenticate_user! before_action :set_provider, only: [:show, :update, :destroy] before_action :set_include - load_and_authorize_resource :except => [:totals, :random] + load_and_authorize_resource :except => [:index, :show, :totals, :random] def index sort = case params[:sort] diff --git a/app/controllers/repositories_controller.rb b/app/controllers/repositories_controller.rb index 77c4b6bc8..c930e5348 100644 --- a/app/controllers/repositories_controller.rb +++ b/app/controllers/repositories_controller.rb @@ -5,7 +5,7 @@ class RepositoriesController < ApplicationController before_action :set_repository, only: [:show, :update, :destroy] before_action :authenticate_user! before_action :set_include - load_and_authorize_resource :client, :parent => false, :except => [:index, :show, :totals] + load_and_authorize_resource :client, :parent => false, :except => [:index, :show, :totals, :random] def index sort = case params[:sort] @@ -177,6 +177,11 @@ def destroy end end + def random + symbol = generate_random_repository_symbol + render json: { symbol: symbol }.to_json + end + def totals page = { size: 0, number: 1} diff --git a/app/models/concerns/helpable.rb b/app/models/concerns/helpable.rb index 4ff42a27d..cddee8f88 100644 --- a/app/models/concerns/helpable.rb +++ b/app/models/concerns/helpable.rb @@ -81,6 +81,10 @@ def generate_random_provider_symbol "4:X".gen end + def generate_random_repository_symbol + "6:X".gen + end + def generate_random_dois(str, options={}) prefix = validate_prefix(str) fail IdentifierError, "No valid prefix found" unless prefix.present? diff --git a/config/routes.rb b/config/routes.rb index c251e1d26..f48c9436c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -40,6 +40,7 @@ get '/dois/text/x-bibliography', :to => 'dois#index', defaults: { format: :citation } get '/providers/text/csv', :to => 'providers#index', defaults: { format: :csv } get 'providers/random', :to => 'providers#random' + get 'repositories/random', :to => 'repositories#random' get '/organizations/text/csv', :to => 'organizations#index', defaults: { format: :csv } get '/repositories/text/csv', :to => 'repositories#index', defaults: { format: :csv } diff --git a/db/schema.rb b/db/schema.rb index e4197710c..93e45161f 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -12,7 +12,7 @@ ActiveRecord::Schema.define(version: 2019_08_07_002912) do - create_table "active_storage_attachments", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin", force: :cascade do |t| + create_table "active_storage_attachments", options: "ENGINE=InnoDB DEFAULT CHARSET=latin1", force: :cascade do |t| t.string "name", limit: 191, null: false t.string "record_type", null: false t.bigint "record_id", null: false @@ -22,7 +22,7 @@ t.index ["record_type", "record_id", "name", "blob_id"], name: "index_active_storage_attachments_uniqueness", unique: true end - create_table "active_storage_blobs", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin", force: :cascade do |t| + create_table "active_storage_blobs", options: "ENGINE=InnoDB DEFAULT CHARSET=latin1", force: :cascade do |t| t.string "key", limit: 191, null: false t.string "filename", limit: 191, null: false t.string "content_type", limit: 191 @@ -33,7 +33,7 @@ t.index ["key"], name: "index_active_storage_blobs_on_key", unique: true end - create_table "allocator", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT", force: :cascade do |t| + create_table "allocator", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| t.string "system_email", null: false t.datetime "created" t.integer "doi_quota_allowed", null: false @@ -75,7 +75,7 @@ t.index ["symbol"], name: "symbol", unique: true end - create_table "allocator_prefixes", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT", force: :cascade do |t| + create_table "allocator_prefixes", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| t.bigint "allocator", null: false t.bigint "prefixes", null: false t.datetime "created_at" @@ -85,7 +85,7 @@ t.index ["prefixes"], name: "FKE7FBD674AF86A1C7" end - create_table "audits", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin", force: :cascade do |t| + create_table "audits", options: "ENGINE=InnoDB DEFAULT CHARSET=latin1", force: :cascade do |t| t.integer "auditable_id" t.string "auditable_type" t.integer "associated_id" @@ -107,18 +107,7 @@ t.index ["user_id", "user_type"], name: "user_index" end - create_table "contacts", options: "ENGINE=InnoDB DEFAULT CHARSET=latin1", force: :cascade do |t| - t.bigint "allocator" - t.string "email" - t.string "given_name" - t.string "family_name" - t.string "role" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - t.index ["allocator"], name: "fk_rails_5c598567a8" - end - - create_table "datacentre", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT", force: :cascade do |t| + create_table "datacentre", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| t.text "comments", limit: 4294967295 t.string "system_email", null: false t.datetime "created" @@ -154,7 +143,7 @@ t.index ["url"], name: "index_datacentre_on_url", length: 100 end - create_table "datacentre_prefixes", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT", force: :cascade do |t| + create_table "datacentre_prefixes", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| t.bigint "datacentre", null: false t.bigint "prefixes", null: false t.datetime "created_at" @@ -166,7 +155,7 @@ t.index ["prefixes"], name: "FK13A1B3BAAF86A1C7" end - create_table "dataset", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT", force: :cascade do |t| + create_table "dataset", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| t.datetime "created" t.string "doi", null: false t.binary "is_active", limit: 1, null: false @@ -248,7 +237,7 @@ t.index ["uuid"], name: "index_events_on_uuid", unique: true, length: 36 end - create_table "media", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT", force: :cascade do |t| + create_table "media", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| t.datetime "created" t.string "media_type", limit: 80 t.datetime "updated" @@ -260,7 +249,7 @@ t.index ["url"], name: "index_media_on_url", length: 100 end - create_table "metadata", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT", force: :cascade do |t| + create_table "metadata", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| t.datetime "created" t.integer "metadata_version" t.integer "version" @@ -272,7 +261,7 @@ t.index ["dataset"], name: "FKE52D7B2F4D3D6B1B" end - create_table "prefix", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT", force: :cascade do |t| + create_table "prefix", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| t.datetime "created" t.string "prefix", limit: 80, null: false t.integer "version" @@ -311,7 +300,6 @@ add_foreign_key "allocator_prefixes", "allocator", column: "allocator", name: "FKE7FBD67446EBD781" add_foreign_key "allocator_prefixes", "prefix", column: "prefixes", name: "FKE7FBD674AF86A1C7" - add_foreign_key "contacts", "allocator", column: "allocator" add_foreign_key "datacentre", "allocator", column: "allocator", name: "FK6695D60546EBD781" add_foreign_key "datacentre_prefixes", "datacentre", column: "datacentre", name: "FK13A1B3BA47B5F5FF" add_foreign_key "datacentre_prefixes", "prefix", column: "prefixes", name: "FK13A1B3BAAF86A1C7" diff --git a/spec/concerns/helpable_spec.rb b/spec/concerns/helpable_spec.rb index 2f668cbb9..b0c0c834e 100644 --- a/spec/concerns/helpable_spec.rb +++ b/spec/concerns/helpable_spec.rb @@ -3,12 +3,18 @@ describe Doi, vcr: true do subject { create(:doi) } - context "generate_random_symbol" do + context "generate_random_provider_symbol" do it 'should generate' do expect(subject.generate_random_provider_symbol).to match(/\A[A-Z]{4}\Z/) end end + context "generate_random_repository_symbol" do + it 'should generate' do + expect(subject.generate_random_repository_symbol).to match(/\A[A-Z]{6}\Z/) + end + end + context "validate_prefix" do it 'should validate' do str = "10.14454"