Skip to content

Commit

Permalink
WIP change dropdown to textbox
Browse files Browse the repository at this point in the history
  • Loading branch information
jenshenny committed Feb 7, 2022
1 parent d59cd2a commit 8fbbfa6
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 10 deletions.
34 changes: 33 additions & 1 deletion app/controllers/api_keys_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ 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 @@ -37,6 +43,12 @@ 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 @@ -67,8 +79,28 @@ 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_id)
params.require(:api_key).permit(:name, *ApiKey::API_SCOPES, :mfa, :rubygem_name)
end

def redirect_to_verify
Expand Down
11 changes: 11 additions & 0 deletions app/models/api_key.rb
Original file line number Diff line number Diff line change
@@ -1,6 +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
belongs_to :user
has_one :api_keys_rubygems, dependent: :destroy
has_one :rubygem, through: :api_keys_rubygems
Expand All @@ -10,6 +11,8 @@ class ApiKey < ApplicationRecord
validates :name, length: { maximum: Gemcutter::MAX_FIELD_LENGTH }
validate :gem_ownership

attr_writer :rubygem_name

def enabled_scopes
API_SCOPES.filter_map { |scope| scope if send(scope) }
end
Expand Down Expand Up @@ -50,6 +53,14 @@ def scope_presence
errors.add :base, "Please enable at least one scope" unless enabled_scopes.any?
end

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

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

def gem_ownership
return true unless rubygem
return true if rubygem.owned_by?(user)
Expand Down
4 changes: 2 additions & 2 deletions app/views/api_keys/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
</div>

<div class="form__group">
<%= label_tag :rubygem_id, t(".rubygem_scope"), class: "form__label" %>
<%= label_tag :rubygem_name, t(".rubygem_scope"), class: "form__label" %>
<p><%= t(".rubygem_scope_info") %></p>
<%= f.collection_select :rubygem_id, current_user.rubygems.by_name, :id, :name, { include_blank: t(".all_gems") }, selected: :rubygem_id, class: "form__input form__select" %>
<%= f.text_field :rubygem_name, value: @api_key.rubygem&.name, placeholder: t(".all_gems"), class: "form__input" %>
</div>

<% unless current_user.mfa_disabled? || current_user.mfa_ui_and_api? %>
Expand Down
4 changes: 2 additions & 2 deletions app/views/api_keys/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
</div>

<div class="form__group">
<%= label_tag :rubygem_id, t(".rubygem_scope"), class: "form__label" %>
<%= label_tag :rubygem, t(".rubygem_scope"), class: "form__label" %>
<p><%= t(".rubygem_scope_info") %></p>
<%= f.collection_select :rubygem_id, current_user.rubygems.by_name, :id, :name, { include_blank: t(".all_gems"), selected: :rubygem }, class: "form__input form__select" %>
<%= f.text_field :rubygem_name, value: @api_key.rubygem&.name, placeholder: t(".all_gems"), class: "form__input" %>
</div>

<% unless current_user.mfa_disabled? || current_user.mfa_ui_and_api? %>
Expand Down
8 changes: 3 additions & 5 deletions test/integration/api_keys_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ class ApiKeysTest < SystemTest

fill_in "api_key[name]", with: "test"
check "api_key[index_rubygems]"
assert page.has_select? "api_key_rubygem_id", selected: nil
page.select @ownership.rubygem.name
fill_in "api_key_rubygem_name", with: @ownership.rubygem.name
click_button "Create"

assert page.has_content? "Note that we won't be able to show the key to you again. New API key:"
Expand Down Expand Up @@ -86,8 +85,8 @@ class ApiKeysTest < SystemTest
click_button "Edit"

assert page.has_content? "Edit API key"
assert page.has_select? "api_key_rubygem_id", selected: @ownership.rubygem.name
page.select "All Gems"
assert page.has_field? "api_key_rubygem_name", with: @ownership.rubygem.name
fill_in "api_key_rubygem_name", with: ""
click_button "Update"

assert_nil api_key.reload.rubygem
Expand Down Expand Up @@ -158,7 +157,6 @@ class ApiKeysTest < SystemTest
assert page.has_content? "(ownership removed)"

click_button "Edit"
assert page.has_select? "api_key_rubygem_id", selected: nil
assert page.has_css? ".flash"
end

Expand Down

0 comments on commit 8fbbfa6

Please sign in to comment.