Commit 1106bdff authored by Justin Ho's avatar Justin Ho

Move backend logic from model to a serializer

As suggested in maintainer review of a previous MR, the logic to
display a JSON format used by the frontend should not be put in the
model. Instead, using a serializer is better separation of concerns.
parent 96825378
......@@ -171,20 +171,6 @@ class Service < ApplicationRecord
fields
end
def global_fields_json
global_fields.map do |field|
value = send(field[:name]) # rubocop:disable GitlabSecurity/PublicSend
public_value =
if field[:type] == 'password' && value.present?
'true'
else
value
end
field.merge(value: public_value)
end.to_json
end
def configurable_events
events = self.class.supported_events
......
# frozen_string_literal: true
class ServiceFieldEntity < Grape::Entity
include RequestAwareEntity
expose :type, :name, :title, :placeholder, :required, :choices, :help
expose :value do |field|
value = service.send(field[:name]) # rubocop:disable GitlabSecurity/PublicSend
if field[:type] == 'password' && value.present?
'true'
else
value
end
end
private
alias_method :field, :object
def service
request.service
end
end
# frozen_string_literal: true
class ServiceFieldSerializer < BaseSerializer
entity ServiceFieldEntity
end
= form_errors(@service)
- trigger_events = Feature.enabled?(:integration_form_refactor) ? ServiceEventSerializer.new(service: @service).represent(@service.configurable_events).to_json : []
- fields = Feature.enabled?(:integration_form_refactor) ? @service.global_fields_json : []
- fields = Feature.enabled?(:integration_form_refactor) ? ServiceFieldSerializer.new(service: @service).represent(@service.global_fields).to_json : []
- if lookup_context.template_exists?('help', "projects/services/#{@service.to_param}", true)
= render "projects/services/#{@service.to_param}/help", subject: @service
......
# frozen_string_literal: true
require 'spec_helper'
describe ServiceFieldEntity do
let(:service) { create(:jira_service) }
let(:request) { double('request') }
subject { described_class.new(field, request: request, service: service).as_json }
before do
allow(request).to receive(:service).and_return(service)
end
describe '#as_json' do
context 'field with type text' do
let(:field) { service.global_fields.find { |field| field[:name] == 'username' } }
it 'exposes correct attributes' do
expect(subject[:type]).to eq('text')
expect(subject[:name]).to eq('username')
expect(subject[:title]).to eq('Username or Email')
expect(subject[:placeholder]).to eq('Use a username for server version and an email for cloud version')
expect(subject[:required]).to eq(true)
expect(subject[:choices]).to eq(nil)
expect(subject[:help]).to eq(nil)
expect(subject[:value]).to eq('jira_username')
end
end
context 'field with type password' do
let(:field) { service.global_fields.find { |field| field[:name] == 'password' } }
it 'exposes correct attributes but hides password' do
expect(subject[:type]).to eq('password')
expect(subject[:name]).to eq('password')
expect(subject[:title]).to eq('Password or API token')
expect(subject[:placeholder]).to eq('Use a password for server version and an API token for cloud version')
expect(subject[:required]).to eq(true)
expect(subject[:choices]).to eq(nil)
expect(subject[:help]).to eq(nil)
expect(subject[:value]).to eq('true')
end
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment