agnes the alien!

Creating Side Pairings for OTW-Archive

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>

https://treasurechest.alien.town/agnes/otwarchive-side-pairings/src/commit/4f52f457d7f494047b6365ef5a11f4ebf3b6d28a/app/views/works/_work_form_tags.rb#L95

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,

https://treasurechest.alien.town/agnes/otwarchive-side-pairings/src/commit/96b40c01a530890fb214ab06446d86c3d7c58ce0/app/controllers/works_controller.rb#L910

And work tag params

https://treasurechest.alien.town/agnes/otwarchive-side-pairings/src/commit/96b40c01a530890fb214ab06446d86c3d7c58ce0/app/controllers/works_controller.rb#L935

EDIT app/models/work.rb, alongside the other virtual attributes add

https://treasurechest.alien.town/agnes/otwarchive-side-pairings/src/commit/a08edd69b9f18efacba6810ad235fdbfffa4d9e3/app/models/work.rb#L76

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:

https://treasurechest.alien.town/agnes/otwarchive-side-pairings/src/commit/a08edd69b9f18efacba6810ad235fdbfffa4d9e3/app/models/work.rb#L16

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

https://treasurechest.alien.town/agnes/otwarchive-side-pairings/src/commit/5d9149606305054234cfca5b9efa57300baffa4b/app/models/work.rb#L1025

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

https://treasurechest.alien.town/agnes/otwarchive-side-pairings/commit/57187457a3937b295b1f6e23d34b1db3265124dd

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'

tutorial, tech, sunset

⬅ Previous post
I'm Agnes, and I'll be your Twitter Lolcow for the week!

Next post ➡
Agnes the Alien