1
0
Fork 0

Use interactor validation

This commit is contained in:
Alex Kotov 2019-09-15 17:52:00 +05:00
parent ec1d9c216c
commit 557e892e44
Signed by: kotovalexarian
GPG Key ID: 553C0EBBEB5D5F08
2 changed files with 5 additions and 19 deletions

View File

@ -32,10 +32,13 @@ class AsymmetricKeysController < ApplicationController
@asymmetric_key_form = AsymmetricKeyForm.new asymmetric_key_form_params
authorize @asymmetric_key_form
return render :new unless @asymmetric_key_form.valid?
result = ImportAsymmetricKey.call @asymmetric_key_form.attributes
if result.failure?
@asymmetric_key_form.errors.add :public_key_pem
return render :new
end
redirect_to after_create_url result.asymmetric_key
end

View File

@ -1,14 +1,8 @@
# frozen_string_literal: true
class AsymmetricKeyForm < ApplicationForm
attr_reader :public_key_openssl_pkey
attribute :public_key_pem, :string
before_validation :set_public_key_openssl_pkey
validates :public_key_pem, presence: true
def self.model_name
ActiveModel::Name.new(self, nil, AsymmetricKey.name)
end
@ -16,15 +10,4 @@ class AsymmetricKeyForm < ApplicationForm
def self.policy_class
'AsymmetricKeyPolicy'
end
private
def set_public_key_openssl_pkey
return @public_key_openssl_pkey = nil if public_key_pem.blank?
@public_key_openssl_pkey = OpenSSL::PKey.read public_key_pem
rescue OpenSSL::OpenSSLError
@public_key_openssl_pkey = nil
errors.add :public_key_pem
end
end