mirror of
https://github.com/mastodon/mastodon.git
synced 2025-07-12 15:33:14 +00:00
Compare commits
4 Commits
b95dfd2f34
...
9ebf94550f
Author | SHA1 | Date | |
---|---|---|---|
![]() |
9ebf94550f | ||
![]() |
6fb6b9fa4b | ||
![]() |
bfdaa9789a | ||
![]() |
f400360cf9 |
|
@ -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
|
||||||
|
|
40
spec/presenters/tag_relationships_presenter_spec.rb
Normal file
40
spec/presenters/tag_relationships_presenter_spec.rb
Normal 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
|
Loading…
Reference in New Issue
Block a user