Compare commits

...

4 Commits

Author SHA1 Message Date
Matt Jankowski
9ebf94550f
Merge 6fb6b9fa4b into c442589593 2025-07-10 08:06:41 +00:00
Matt Jankowski
6fb6b9fa4b Use private methods 2025-07-03 18:28:38 -04:00
Matt Jankowski
bfdaa9789a Remove unused options arg 2025-07-03 18:28:38 -04:00
Matt Jankowski
f400360cf9 Add coverage for TagRelationshipsPresenter class 2025-07-03 18:28:38 -04:00
2 changed files with 59 additions and 3 deletions

View File

@ -3,13 +3,29 @@
class TagRelationshipsPresenter class TagRelationshipsPresenter
attr_reader :following_map, :featuring_map attr_reader :following_map, :featuring_map
def initialize(tags, current_account_id = nil, **options) def initialize(tags, current_account_id = nil)
if current_account_id.nil? if current_account_id.nil?
@following_map = {} @following_map = {}
@featuring_map = {} @featuring_map = {}
else else
@following_map = TagFollow.select(:tag_id).where(tag_id: tags.map(&:id), account_id: current_account_id).each_with_object({}) { |f, h| h[f.tag_id] = true }.merge(options[:following_map] || {}) @following_map = mapped_tag_follows(tags, current_account_id)
@featuring_map = FeaturedTag.select(:tag_id).where(tag_id: tags.map(&:id), account_id: current_account_id).each_with_object({}) { |f, h| h[f.tag_id] = true }.merge(options[:featuring_map] || {}) @featuring_map = mapped_featured_tags(tags, current_account_id)
end end
end end
private
def mapped_tag_follows(tags, account_id)
TagFollow
.where(tag_id: tags.map(&:id), account_id: account_id)
.pluck(:tag_id)
.index_with(true)
end
def mapped_featured_tags(tags, account_id)
FeaturedTag
.where(tag_id: tags.map(&:id), account_id: account_id)
.pluck(:tag_id)
.index_with(true)
end
end end

View File

@ -0,0 +1,40 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe TagRelationshipsPresenter do
context 'without an account' do
subject { described_class.new(tags, nil) }
let(:tags) { Fabricate.times 2, :tag }
it 'includes empty hashes for maps' do
expect(subject)
.to have_attributes(
following_map: eq({}),
featuring_map: eq({})
)
end
end
context 'with an account and following and featured tags' do
subject { described_class.new(Tag.all, account.id) }
let(:account) { Fabricate :account }
let(:tag_to_feature) { Fabricate :tag }
let(:tag_to_follow) { Fabricate :tag }
before do
Fabricate :featured_tag, account: account, tag: tag_to_feature
Fabricate :tag_follow, account: account, tag: tag_to_follow
end
it 'includes map with relevant id values' do
expect(subject)
.to have_attributes(
featuring_map: eq(tag_to_feature.id => true),
following_map: eq(tag_to_follow.id => true)
)
end
end
end