Skip to content

Commit

Permalink
generate random repository id. #335
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Jan 11, 2020
1 parent 0b1f2ef commit bfe0d77
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 26 deletions.
2 changes: 1 addition & 1 deletion app/controllers/providers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
7 changes: 6 additions & 1 deletion app/controllers/repositories_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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}

Expand Down
4 changes: 4 additions & 0 deletions app/models/concerns/helpable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down
34 changes: 11 additions & 23 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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"
Expand All @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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"
Expand All @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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"
Expand Down
8 changes: 7 additions & 1 deletion spec/concerns/helpable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit bfe0d77

Please sign in to comment.