Fixes #2793 - CSV-Export contains Tags twice.

This commit is contained in:
Mantas Masalskis 2020-12-08 15:06:41 +01:00 committed by Thorsten Eckel
parent a0dc877e33
commit 7bfe6dac31
4 changed files with 101 additions and 17 deletions

View file

@ -0,0 +1,35 @@
require 'active_record/relation/calculations'
module ActiveRecord
module Calculations
# plucks attributes and create a hash instead of returning an array
#
# @see ActiveRecord::Calculations#pluck
#
# @param [<String, Symbol, SqlLiteral>] attributes to fetch
#
# @return [<Hash<String=>Any>]
#
# @example
# Ticket.all.pluck_as_hash(:title) # [{title: 'A'}, {title: 'B'}]
# Ticket.all.pluck_as_hash(:title, :owner_id) # [{title: 'A', owner_id: 1}, {title: 'B', owner_id: 2}]
def pluck_as_hash(*column_names)
column_names.flatten! # flatten args in case array was given
klass.enforce_raw_sql_whitelist(column_names) # keep up deprection as in #pluck to ensure smooth Rails 6 transition
pluck(*arel_columns(column_names))
.map { |elem| pluck_as_hash_map(column_names, elem) }
end
private
def pluck_as_hash_map(keys, values)
if keys.one?
Hash[keys.first, values]
else
keys.zip(values).to_h
end
end
end
end

View file

@ -48,23 +48,19 @@ class ExcelSheet::Ticket < ExcelSheet
header.concat(@additional_attributes_header) if @additional_attributes_header header.concat(@additional_attributes_header) if @additional_attributes_header
# ObjectManager attributes # ObjectManager attributes
objects = ObjectManager::Attribute.where(active: true, ObjectManager::Attribute
to_create: false, .where(
object_lookup_id: ObjectLookup.lookup(name: 'Ticket').id) active: true,
.pluck(:name, :display, :data_type, :data_option) to_create: false,
.map { |name, display, data_type, data_option| { name: name, display: display, data_type: data_type, data_option: data_option, width: 20 } } object_lookup: ObjectLookup.lookup(name: 'Ticket')
objects.each do |object| )
already_exists = false .where.not(
header.each do |local_header| name: header.pluck(:name),
next if local_header[:name] != object[:name] display: header.pluck(:display)
)
already_exists = true .pluck_as_hash(:name, :display, :data_type, :data_option)
break .each { |elem| elem[:width] = 20 }
end .then { |objects| header.concat(objects) }
next if already_exists
header.push object
end
header.concat([ header.concat([
{ display: 'Created At', name: 'created_at', width: 18, data_type: 'datetime' }, { display: 'Created At', name: 'created_at', width: 18, data_type: 'datetime' },

View file

@ -0,0 +1,24 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe ActiveRecord::Calculations do
describe '#pluck_as_hash' do
let(:ticket) { create(:ticket) }
it 'returns array with the hash' do
result = Ticket.where(id: ticket.id).pluck_as_hash(:title)
expect(result).to eq [{ title: ticket.title }]
end
it 'works given multiple attributes' do
result = Ticket.where(id: ticket.id).pluck_as_hash(:title, :id)
expect(result).to eq [{ title: ticket.title, id: ticket.id }]
end
it 'works given array' do
result = Ticket.where(id: ticket.id).pluck_as_hash(%i[title id])
expect(result).to eq [{ title: ticket.title, id: ticket.id }]
end
end
end

View file

@ -0,0 +1,29 @@
require 'rails_helper'
RSpec.describe ExcelSheet::Ticket do
let(:ticket) { create(:ticket) }
let(:tag_name) { 'foo' }
let(:instance) { described_class.new(title: 'some title', ticket_ids: [ticket.id], timezone: 'Europe/Berlin', locale: 'de-de') }
before do
Tag.tag_add(object: 'Ticket', item: tag_name, o_id: 1, created_by_id: 1)
end
describe '#ticket_header' do
it 'has Tags once' do
tags_count = instance.ticket_header.count { |elem| elem[:display] == 'Tags' }
expect(tags_count).to eq 1
end
it 'has 31 column in default configuration' do
tags_count = instance.ticket_header.count
expect(tags_count).to eq 31
end
it 'all elements have width attribute' do
expect(instance.ticket_header).to be_all(have_key(:width))
end
end
end