Creating Side Pairings for OTW-Archive
| 5 min read
I remember in 2016 when my friend emailed AO3 and asked for a side pairings feature and they told them it wasn't being planned because it'd be difficult. Well, me and another dev made it in a week, LOL. This may be hacky and crazy, but according to Symphony it works for purposes of working and I'll be deploying to Sunset soon. Just if you implement this on your own site, know this software isn't really designed for that and even though we did this it was incredibly hard, so don't yell at me if it breaks stuff.
Heavy heavy thanks to Jordan, Paintedflowers, for making 95% of this code. I just put together the commands that make it work basically, as well as a few code tweaks to make it smoother. Also note that THIS ONLY WORKS FOR DOCKER <33 I do not know how to adapt it to bare metal.
HERE WE GO.
Ok so first, db migration
db/migrate/TIMESTAMP_create_work_side_pairings.rb
class CreateWorkSidePairings < ActiveRecord::Migration[7.0]
def change
create_table :work_side_pairings do |t|
t.references :work,
null: false,
type: :integer,
foreign_key: true
t.references :relationship,
null: false,
type: :integer,
foreign_key: { to_table: :tags }
t.timestamps
end
add_index :work_side_pairings,
[:work_id, :relationship_id],
unique: true,
name: "index_work_side_pairings_unique"
end
end
now you have to go add to your dockerfile
RUN apt-get update && \
apt-get install -y \
calibre \
default-mysql-client \
libvips \
shared-mime-info \
# ->>>>>> ADD THIS LINE percona-toolkit \ # ADD THIS LINE <------------------------ BEFORE ZIP
zip
then rebuild web containers docker compose up -d web --force-recreate --build
then run
docker compose run web bundle exec rake db:migrate
CREATE:
app/models/work_side_pairing.rb
class WorkSidePairing < ApplicationRecord
belongs_to :work
belongs_to :relationship,
class_name: "Relationship"
validates :relationship_id,
uniqueness: { scope: :work_id }
after_create :update_filters
after_destroy :update_filters
after_create :update_relationship
after_destroy :update_relationship
def update_filters
return unless work && !work.destroyed?
work.update_filters
end
def update_relationship
return unless relationship && !relationship.destroyed?
relationship.update_tag_cache
relationship.enqueue_to_index if relationship.taggings_count < ArchiveConfig.TAGGINGS_COUNT_REINDEX_LIMIT
end
end
EDIT:
app/views/works/_work_form_tags.rb
<dt class="side-pairing">
<label for="work_side_pairing">Side/Background Pairings</label>
</dt>
<dd class="side-pairing">
<%= text_field_tag(
"work[side_pairing_names]",
include_blank ? "" : @work.side_pairing_names,
autocomplete_options(
"relationship_in_fandom",
id: "work_side_pairing",
title: "Side/Background Pairings",
data: { autocomplete_live_params: "fandom=work_fandom" }
)
) %>
</dd>
edit this file to add where indicated in the line (below
data: { autocomplete_live_params: "fandom=work_fandom" }) %>
</dd>
)
EDIT app/controllers/works_controller.rb
ADD to params of work_params side_pairing_names like so: :rating_string, :fandom_string, :relationship_string, :side_pairing_names, :character_string,
And work tag params
EDIT app/models/work.rb, alongside the other virtual attributes add
attr_accessor :side_pairing_names
def side_pairing_names
@side_pairing_names ||=
side_pairings.map(&:name).join(
ArchiveConfig.DELIMITER_FOR_OUTPUT
)
end
def side_pairing_names=(value)
@side_pairing_names = value
names = value.to_s
.gsub(/\uff0c|\u3001/, ",")
.split(",")
.map(&:squish)
.reject(&:blank?)
.uniq
self.side_pairings = names.map do |name|
Relationship.find_or_create_by_name(name)
end
end
Also at the top under associations:
has_many :work_side_pairings,
dependent: :destroy
has_many :side_pairings,
through: :work_side_pairings,
source: :relationship
CREATE app/views/works/_side_pairings.html.erb
<% if @work.side_pairings.any? %>
<dl class="side-pairings">
<dt>Side Pairings:</dt>
<dd>
<% @work.side_pairings.each do |pairing| %>
<%= link_to pairing.name,
side_pairing_path(pairing) %>
<% end %>
</dd>
</dl>
<% end %>
EDIT app/views/works/show.html.erb
three people working together couldn't figure out how to edit the part that says fandom, relationship, etc at the top of a specific work, so just add <%= render "side_pairings", work: @work %> after <!--/descriptions--> and it'll show under the summary
Back to app/models/work.rb
Go down to scope underneath public
scope :with_side_pairing, ->(relationship_id) {
joins(:work_side_pairings)
.where(
work_side_pairings: {
relationship_id: relationship_id
}
)
.distinct
}
CREATE app/controllers/side_pairings_controller.rb
class SidePairingsController < ApplicationController
def show
@relationship = Relationship.find(params[:id])
@works = Work.visible_to_user(current_user)
.with_side_pairing(@relationship.id)
.order(revised_at: :desc)
end
end
Add this to config/routes.rb
I have it under errors in the repo and it works but maybe dont put it there
get "side_pairings/:id",
to: "side_pairings#show",
as: :side_pairing
CREATE app/views/side_pairings
app/views/side_pairings/show.html.erb
<h2>Works featuring this side pairing</h2>
<h3><%= @relationship.name %></h3>
<% @works.each do |work| %>
<%= render "works/work_blurb", work: work %>
<% end %>
edit filter updater app/models/filter_updater.rb i think to this
def load_direct_filters
taggings = Tagging.where(taggable_type: type, taggable_id: @valid_item_ids)
filter_relations = [
Tag.canonical.joins(:taggings),
Tag.canonical.joins(mergers: :taggings)
]
pairs = filter_relations.flat_map do |filters|
filters.merge(taggings).pluck("taggings.taggable_id", "tags.id")
end
if type == "Work"
side_pairing_pairs = WorkSidePairing
.where(work_id: @valid_item_ids)
.joins(:relationship)
.pluck(:work_id, "tags.id")
pairs.concat(side_pairing_pairs)
end
but here's the full file: https://treasurechest.alien.town/agnes/otwarchive-side-pairings/src/branch/main/app/models/filter_updater.rb
add to app/views/works/_work_module.rb right above summary
<% if work.side_pairings.any? %>
<h6 class="heading">Side/Background Pairings</h6>
<ul class="tags commas side-pairings">
<% work.side_pairings.each do |pairing| %>
<li><%= link_to_tag_works(pairing) %></li>
<% end %>
</ul>
<% end %>
EDIT app/models/relationship.rb add
def works
main_work_ids = Tagging.where(
tagger_type: "Tag",
tagger_id: id,
taggable_type: "Work"
).select(:taggable_id)
side_work_ids = WorkSidePairing.where(
relationship_id: id
).select(:work_id)
Work.where(id: main_work_ids).or(
Work.where(id: side_work_ids)
)
end
def taggings_count
main_work_ids = Tagging.where(
tagger_type: "Tag",
tagger_id: id,
taggable_type: "Work"
).select(:taggable_id)
side_pairing_count = WorkSidePairing.where(
relationship_id: id
).where.not(work_id: main_work_ids).count
super + side_pairing_count
end
make it show up on wrangling
EDIT app/models/search/tag_indexer.rb
change
uses: object.taggings_count_cache
to
uses: object.taggings_count
Add to skin css:
.side-pairings {
margin: 12px 0;
padding: 10px 12px;
background: #f3eff7;
border-left: 4px solid #80649b;
border-radius: 3px;
}
.side-pairings dt {
font-weight: bold;
color: #59416f;
margin-bottom: 6px;
}
.side-pairings dd {
margin: 0;
}
.side-pairings a {
display: inline-block;
padding: 3px 7px;
margin: 2px 3px 2px 0;
color: #604878;
background: #e6dcef;
border: 1px solid #c5b1d7;
border-radius: 3px;
text-decoration: none;
}
.side-pairings a:hover {
background: #d8c8e7;
text-decoration: underline;
}
.side-pairing {
margin-top: 12px;
}
run the following commands I can't remember which ones of the ones I tried are necessary to get it to work so we'll just do both
sudo docker compose run --rm web bundle exec rails runner '
total = Work.count
processed = 0
puts "fucking with #{total} works..."
Work.update_filters(reindex_queue: :main) do
processed += ArchiveConfig.FILTER_UPDATE_BATCH_SIZE
puts "fucked with approximately #{[processed, total].min}/#{total} works"
end
puts "finished reindexing"
'
sudo docker compose run --rm web bundle exec rails runner 'p IndexQueue.all'