Skip to content

Commit

Permalink
Create ApiKeysRubygems association
Browse files Browse the repository at this point in the history
  • Loading branch information
jenshenny committed Feb 7, 2022
1 parent 33c05d6 commit d59cd2a
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 13 deletions.
9 changes: 5 additions & 4 deletions app/models/api_key.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ class ApiKey < ApplicationRecord
API_SCOPES = %i[index_rubygems push_rubygem yank_rubygem add_owner remove_owner access_webhooks show_dashboard].freeze

belongs_to :user
belongs_to :rubygem, optional: true
has_one :api_keys_rubygems, dependent: :destroy
has_one :rubygem, through: :api_keys_rubygems
validates :user, :name, :hashed_key, presence: true
validate :exclusive_show_dashboard_scope, if: :can_show_dashboard?
validate :scope_presence
Expand Down Expand Up @@ -50,9 +51,9 @@ def scope_presence
end

def gem_ownership
return true unless rubygem_id
return true if user.rubygems.find_by_id(rubygem_id)
errors.add :rubygem, "#{Rubygem.find_by_id(rubygem_id)&.name} cannot be scoped to this API key." \
return true unless rubygem
return true if rubygem.owned_by?(user)
errors.add :rubygem, "#{rubygem.name} cannot be scoped to this API key." \
" Please change the scope to a gem that you own."
false
end
Expand Down
7 changes: 7 additions & 0 deletions app/models/api_keys_rubygems.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class ApiKeysRubygems < ApplicationRecord
belongs_to :api_key
belongs_to :rubygem

validates :rubygem_id, uniqueness: { scope: :api_key_id }
validates :api_key, :rubygem, presence: true
end
1 change: 0 additions & 1 deletion app/models/rubygem.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ class Rubygem < ApplicationRecord
has_one :gem_download, -> { where(version_id: 0) }, inverse_of: :rubygem
has_many :ownership_calls, -> { opened }, dependent: :destroy, inverse_of: :rubygem
has_many :ownership_requests, -> { opened }, dependent: :destroy, inverse_of: :rubygem
has_many :api_keys

validate :ensure_name_format, if: :needs_name_validation?
validates :name,
Expand Down
5 changes: 0 additions & 5 deletions db/migrate/20220127003654_add_rubygem_to_api_keys.rb

This file was deleted.

12 changes: 12 additions & 0 deletions db/migrate/20220204145456_create_join_table_api_keys_rubygems.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class CreateJoinTableApiKeysRubygems < ActiveRecord::Migration[6.1]
def self.up
create_table :api_keys_rubygems do |t|
t.references :api_key
t.references :rubygem, index: false
end
end

def self.down
drop_table :api_keys_rubygems
end
end
10 changes: 7 additions & 3 deletions 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.define(version: 2022_01_27_003654) do
ActiveRecord::Schema.define(version: 2022_02_04_145456) do

# These are extensions that must be enabled in order to support this database
enable_extension "hstore"
Expand All @@ -31,12 +31,16 @@
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.boolean "mfa", default: false, null: false
t.bigint "rubygem_id"
t.index ["hashed_key"], name: "index_api_keys_on_hashed_key", unique: true
t.index ["rubygem_id"], name: "index_api_keys_on_rubygem_id"
t.index ["user_id"], name: "index_api_keys_on_user_id"
end

create_table "api_keys_rubygems", force: :cascade do |t|
t.integer "api_key_id"
t.integer "rubygem_id"
t.index ["api_key_id"], name: "index_api_keys_rubygems_on_api_key_id"
end

create_table "delayed_jobs", id: :serial, force: :cascade do |t|
t.integer "priority", default: 0
t.integer "attempts", default: 0
Expand Down
9 changes: 9 additions & 0 deletions test/unit/api_keys_rubygems_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require "test_helper"

class ApiKeysRubygemsTest < ActiveSupport::TestCase
should belong_to :api_key
should belong_to :rubygem
should validate_uniqueness_of(:rubygem_id).scoped_to(:api_key_id)
should validate_presence_of(:rubygem)
should validate_presence_of(:api_key)
end

0 comments on commit d59cd2a

Please sign in to comment.