Skip to content

Commit

Permalink
Find rubygem based on name be in the model
Browse files Browse the repository at this point in the history
  • Loading branch information
jenshenny committed Feb 7, 2022
1 parent 33af15d commit b8bf7da
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 35 deletions.
32 changes: 0 additions & 32 deletions app/controllers/api_keys_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@ def create
key = generate_unique_rubygems_key
@api_key = current_user.api_keys.build(api_key_params.merge(hashed_key: hashed_key(key)))

# unless set_rubygem_from_params
# flash[:error] = @api_key.errors.full_messages.to_sentence
# render :new
# return
# end

if @api_key.save
Mailer.delay.api_key_created(@api_key.id)

Expand All @@ -43,12 +37,6 @@ def edit
def update
@api_key = current_user.api_keys.find(params.require(:id))

# unless set_rubygem_from_params
# flash[:error] = @api_key.errors.full_messages.to_sentence
# render :edit
# return
# end

if @api_key.update(api_key_params)
redirect_to profile_api_keys_path, flash: { notice: t(".success") }
else
Expand Down Expand Up @@ -79,26 +67,6 @@ def reset

private

# def set_rubygem_from_params
# rubygem_name = api_key_params.dig(:rubygem)
# unless rubygem_name.present?
# @api_key.rubygem = nil
# return true
# end

# rubygem = Rubygem.name_is(rubygem_name).first
# if rubygem
# @api_key.rubygem = rubygem
# else
# @api_key.errors[:rubygem] << "#{rubygem_name} cannot be found."
# false
# end
# end

# def api_key_build_params
# api_key_params.except(:rubygem)
# end

def api_key_params
params.require(:api_key).permit(:name, *ApiKey::API_SCOPES, :mfa, :rubygem_name)
end
Expand Down
6 changes: 3 additions & 3 deletions app/models/api_key.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class ApiKey < ApplicationRecord
API_SCOPES = %i[index_rubygems push_rubygem yank_rubygem add_owner remove_owner access_webhooks show_dashboard].freeze

before_validation :set_rubygem_from_name, on: :save
before_validation :set_rubygem_from_name, on: %i[create update]
belongs_to :user
has_one :api_keys_rubygems, dependent: :destroy
has_one :rubygem, through: :api_keys_rubygems
Expand All @@ -11,7 +11,7 @@ class ApiKey < ApplicationRecord
validates :name, length: { maximum: Gemcutter::MAX_FIELD_LENGTH }
validate :gem_ownership

attr_writer :rubygem_name
attr_accessor :rubygem_name

def enabled_scopes
API_SCOPES.filter_map { |scope| scope if send(scope) }
Expand Down Expand Up @@ -55,7 +55,7 @@ def scope_presence

def set_rubygem_from_name
return if rubygem_name.nil?
return self.rubygem = nil unless rubygem_name.present?
return self.rubygem = nil if rubygem_name.blank?

self.rubygem = Rubygem.name_is(rubygem_name).first
errors.add :rubygem, "#{rubygem_name} cannot be found." unless rubygem
Expand Down

0 comments on commit b8bf7da

Please sign in to comment.