diff --git a/app/models/application_model/can_assets.rb b/app/models/application_model/can_assets.rb index b5dee446a..95d2226c7 100644 --- a/app/models/application_model/can_assets.rb +++ b/app/models/application_model/can_assets.rb @@ -73,28 +73,35 @@ get assets and record_ids of selector logger.error "Unable to get asset for '#{attribute[0]}': #{e.inspect}" next end - reflection = attribute[1].sub(/_id$/, '') - #reflection = reflection.to_sym - next if !models[attribute_class] - next if !models[attribute_class][:reflections] - next if !models[attribute_class][:reflections][reflection] - next if !models[attribute_class][:reflections][reflection].klass - attribute_ref_class = models[attribute_class][:reflections][reflection].klass - if content['value'].instance_of?(Array) - content['value'].each do |item_id| - next if item_id.blank? + if attribute_class == ::Notification + next if content['recipient'].blank? - attribute_object = attribute_ref_class.lookup(id: item_id) - next if !attribute_object + attribute_ref_class = ::User + item_ids = [] + Array(content['recipient']).each do |identifier| + next if identifier !~ /\Auserid_(\d+)\z/ - assets = attribute_object.assets(assets) - end - elsif content['value'].present? - attribute_object = attribute_ref_class.find_by(id: content['value']) - if attribute_object - assets = attribute_object.assets(assets) + item_ids.push($1) end + else + reflection = attribute[1].sub(/_id$/, '') + next if !models[attribute_class] + next if !models[attribute_class][:reflections] + next if !models[attribute_class][:reflections][reflection] + next if !models[attribute_class][:reflections][reflection].klass + + attribute_ref_class = models[attribute_class][:reflections][reflection].klass + item_ids = Array(content['value']) + end + + item_ids.each do |item_id| + next if item_id.blank? + + attribute_object = attribute_ref_class.lookup(id: item_id) + next if !attribute_object + + assets = attribute_object.assets(assets) end end assets diff --git a/spec/models/trigger_spec.rb b/spec/models/trigger_spec.rb index 5f01c9823..aa0f482b4 100644 --- a/spec/models/trigger_spec.rb +++ b/spec/models/trigger_spec.rb @@ -157,6 +157,21 @@ RSpec.describe Trigger, type: :model do it 'contains all recipients' do expect(ticket.articles.last.to).to eq("#{recipient1.email}, #{recipient2.email}, #{recipient3.email}") end + + context 'assets' do + it 'resolves Users from recipient list' do + expect(trigger.assets({})[:User].keys).to include(recipient1.id, recipient2.id, recipient3.id) + end + + context 'single entry' do + + let(:recipient) { "userid_#{recipient1.id}" } + + it 'resolves User from recipient list' do + expect(trigger.assets({})[:User].keys).to include(recipient1.id) + end + end + end end context 'recipient group keyword only' do