can_assets_examples.rb 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
  2. RSpec.shared_examples 'ApplicationModel::CanAssets' do |associations: [], selectors: [], own_attributes: true|
  3. subject { create(described_class.name.underscore, updated_by_id: admin.id) }
  4. let(:admin) { create(:admin) }
  5. describe '#assets (for supplying model data to front-end framework)' do
  6. shared_examples 'own asset attributes' do
  7. it 'returns a hash with own asset attributes' do
  8. expect(subject.assets({})[described_class.to_app_model])
  9. .to include(subject.id => hash_including(subject.attributes_with_association_ids))
  10. end
  11. end
  12. include_examples 'own asset attributes' if own_attributes
  13. describe 'for created_by & updated_by users' do
  14. let(:users) { User.where(id: subject.attributes.slice('created_by_id', 'updated_by_id').values) }
  15. let(:users_assets) { users.reduce({}) { |assets_hash, user| user.assets(assets_hash) } }
  16. it 'returns a hash with their asset attributes' do
  17. expect(subject.assets({})[:User]).to include(users_assets[:User])
  18. end
  19. end
  20. context 'when given a non-empty hash' do
  21. let(:hash) { { described_class.to_app_model => { foo: 'bar' } } }
  22. it 'deep-merges assets into it, in place' do
  23. expect { subject.assets(hash) }
  24. .to change { hash }
  25. .to(hash.deep_merge(subject.assets({})))
  26. end
  27. end
  28. Array(associations).each do |association|
  29. describe "for ##{association} association" do
  30. let(:reflection) { described_class.reflect_on_association(association) }
  31. shared_examples 'single association' do
  32. subject { create(described_class.name.underscore, association => single) }
  33. let(:single) { create(reflection.class_name.underscore) }
  34. it 'returns a hash with its asset attributes' do
  35. expect(subject.assets({})).to include(single.assets({}))
  36. end
  37. context 'after association has been modified' do
  38. it 'does not use a cached value' do
  39. subject.assets({})
  40. single.update(updated_by_id: User.last.id)
  41. expect(subject.assets({}).dig(reflection.klass.to_app_model, single.id))
  42. .to include(single.attributes_with_association_ids)
  43. end
  44. end
  45. end
  46. shared_examples 'collection association' do
  47. subject { create(described_class.name.underscore, association => collection) }
  48. let(:collection) { create_list(reflection.class_name.underscore, 5) }
  49. let(:collection_assets) { collection.reduce({}) { |assets_hash, single| single.assets(assets_hash) } }
  50. it 'returns a hash with their asset attributes' do
  51. expect(subject.assets({})).to include(collection_assets)
  52. end
  53. context 'after association has been modified' do
  54. it 'does not use a cached value' do
  55. subject.assets({})
  56. collection.first.update(updated_by_id: User.last.id)
  57. expect(subject.assets({}).dig(reflection.klass.to_app_model, collection.first.id))
  58. .to include(collection.first.attributes_with_association_ids)
  59. end
  60. end
  61. end
  62. if described_class.reflect_on_association(association).macro.in?(%i[has_one belongs_to])
  63. include_examples 'single association'
  64. else
  65. include_examples 'collection association'
  66. end
  67. end
  68. end
  69. Array(selectors).each do |s|
  70. subject { create(described_class.name.underscore, s => selector) }
  71. let(:selector) { { 'ticket.priority_id' => { operator: 'is', value: [1, 2] } } }
  72. let(:priorities_assets) { Ticket::Priority.first(2).reduce({}) { |asset_hash, priority| priority.assets(asset_hash) } }
  73. describe "for objects referenced in ##{s}" do
  74. it 'returns a hash with their asset attributes' do
  75. expect(subject.assets({})).to include(priorities_assets)
  76. end
  77. end
  78. end
  79. end
  80. end