Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions app/controllers/tags_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,19 +72,16 @@ def show
end

def feed
begin
@tag = Tag.find(params[:id])
rescue ActiveRecord::RecordNotFound
raise ActiveRecord::RecordNotFound, "Couldn't find tag with id '#{params[:id]}'"
end
@tag = Tag.find(params[:id])
@tag = @tag.merger if !@tag.canonical? && @tag.merger
# Temp for testing
Comment thread
brianjaustin marked this conversation as resolved.
if %w(Fandom Character Relationship).include?(@tag.type.to_s) || @tag.name == 'F/F'
if @tag.canonical?
@works = @tag.filtered_works.visible_to_all.order('created_at DESC').limit(25)
else
@works = @tag.works.visible_to_all.order('created_at DESC').limit(25)
end
@works = if @tag.canonical?
@tag.filtered_works
else
@tag.works
end
@works = @works.visible_to_all.order(created_at: :desc).limit(25).includes(:tags, :language, :series)
else
redirect_to(tag_works_path(tag_id: @tag.to_param)) && return
end
Expand Down
2 changes: 1 addition & 1 deletion app/helpers/works_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def feed_summary(work)
text = +"<p>by #{byline(work, { visibility: 'public', only_path: false })}</p>"
text << work.summary if work.summary
text << "<p>Words: #{work.word_count}, Chapters: #{chapter_total_display(work)}, Language: #{work.language ? work.language.name : 'English'}</p>"
text << "<p>Series: #{series_list_with_work_position(work)}</p>" unless work.series.count.zero?
text << "<p>Series: #{series_list_with_work_position(work)}</p>" unless work.series.size.zero?
# Create list of tags
text << "<ul>"
%w(Fandom Rating ArchiveWarning Category Character Relationship Freeform).each do |type|
Expand Down
5 changes: 3 additions & 2 deletions app/views/tags/feed.atom.builder
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
atom_feed do |feed|
feed.title "AO3 works tagged '#{@tag.name}'"
feed.updated @works.first.created_at if @works.respond_to?(:first) && @works.first.present?

@works.each do |work|
@works.each_with_index do |work, index|
next if work.unrevealed?

feed.updated work.created_at if index.zero?

feed.entry work do |entry|
entry.title work.title
entry.summary feed_summary(work), type: "html"
Expand Down
4 changes: 4 additions & 0 deletions features/other_b/subscriptions_fandoms.feature
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@ Feature: Subscriptions
Scenario: Subscribe to a test fandom when there are works in it

When I am logged in as "author"
And time is frozen at 2026-04-11 16:00
And I post a work "My Work Title" with category "F/F"
And time is frozen at 2026-04-11 17:00
And I post a work "Silly Games" with category "F/F"
When I am logged in as "reader"
And I view the "F/F" works index
Then I should see "RSS Feed"
When I follow "RSS Feed"
Then I should see "My Work Title"
And I should see "Stargate SG-1"
And the feed updated date should be should be the created date of "Silly Games"

Scenario: Subscribe to a non-test fandom

Expand Down
5 changes: 5 additions & 0 deletions features/step_definitions/subscription_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@
expect(page).to have_selector(:xpath, "//author").exactly(int)
end

Then "the feed updated date should be should be the created date of {string}" do |work|
w = Work.find_by(title: work)
expect(find(:xpath, "//feed/updated")).to have_content(w.created_at.xmlschema)
end

# rubocop:disable Cucumber/RegexStepName
Then /^the (\d+)(?:st|nd|rd|th) feed author should contain "([^"]*)"$/ do |n, text|
within(:xpath, "//author[#{n}]") do
Expand Down
47 changes: 47 additions & 0 deletions spec/requests/tags_n_plus_one_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
require "spec_helper"

describe "n+1 queries in the tags controller" do
describe "#feed", n_plus_one: true do
context "when creating a tag's feed" do
let!(:tag) { create(:canonical_fandom, name: "Hermitcraft SMP") }

subject do
proc do
get feed_tag_path({ id: tag.id, format: :atom })
end
end

context "when all bylines are cached" do
populate do |n|
create_list(:work, n, fandom_string: "Hermitcraft SMP")
subject.call # this caches the bylines
end

it "produces a constant number of queries" do
expect do
subject.call
expect(response.body.scan("<author>").size).to eq(current_scale.to_i)
end.to perform_constant_number_of_queries
end
end

context "when nothing is cached" do
populate do |n|
create_list(:work, n, fandom_string: "Hermitcraft SMP")
end

it "performs around 4 queries per work" do
# TODO: Ideally, we'd like the uncached tag feed to also have a
# constant number of queries, instead of the linear number of queries
# we're checking for here. But we also don't want to put too much
# unnecessary load on the databases when we have a bunch of cache hits,
# so this requires some care & tuning.
expect do
subject.call
expect(response.body.scan("<author>").size).to eq(current_scale.to_i)
end.to perform_linear_number_of_queries(slope: 4).with_warming_up
end
end
end
end
end
Loading