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
4 changes: 3 additions & 1 deletion app/models/search/tag_indexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ def self.mapping
},
tag_type: { type: "keyword" },
sortable_name: { type: "keyword" },
canonized_at: { type: "date" },
decanonized_at: { type: "date" },
uses: { type: "integer" },
unwrangled: { type: "boolean" }
}
Expand Down Expand Up @@ -72,7 +74,7 @@ def document(object)
root: false,
only: [
:id, :name, :sortable_name, :merger_id, :canonical, :created_at,
:unwrangleable
:canonized_at, :decanonized_at, :unwrangleable
]
).merge(
has_posted_works: object.has_posted_works?,
Expand Down
10 changes: 7 additions & 3 deletions app/models/search/tag_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,19 @@ def sort
when "created_at"
column = "created_at"
direction ||= "desc"
when "canonized_at"
column = "canonized_at"
direction ||= "desc"
when "decanonized_at"
column = "decanonized_at"
direction ||= "desc"
else
column = "name.keyword"
direction ||= "asc"
end
sort_hash = { column => { order: direction } }

if column == "created_at"
sort_hash[column][:unmapped_type] = "date"
end
sort_hash[column][:unmapped_type] = "date" if %w[created_at canonized_at decanonized_at].include?(column)

sort_by_id = { id: { order: direction } }

Expand Down
4 changes: 3 additions & 1 deletion app/models/search/tag_search_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,13 @@ def sort_options
[
%w[Name name],
["Date Created", "created_at"],
["Date Canonized", "canonized_at"],
["Date Decanonized", "decanonized_at"],
%w[Uses uses]
]
end

def default_sort_direction
%w[created_at uses].include?(sort_column) ? "desc" : "asc"
%w[created_at canonized_at decanonized_at uses].include?(sort_column) ? "desc" : "asc"
end
end
9 changes: 9 additions & 0 deletions app/models/tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,15 @@ def update_wrangler(tag)
end
end

before_save :set_canonization_date, if: :will_save_change_to_canonical?
def set_canonization_date
if canonical?
self.canonized_at = Time.current
else
self.decanonized_at = Time.current
end
end

after_save :check_type_changes, if: :saved_change_to_type?
def check_type_changes
return if type_before_last_save.nil?
Expand Down
10 changes: 10 additions & 0 deletions app/views/tags/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@

<% if logged_in_as_admin? %>
<p class="notes"><%= ts("Last updated by %{wrangler} on %{date}", wrangler: @tag.last_wrangler.try(:login) || '---', date: @tag.updated_at) %></p>
<% if @tag.canonized_at || @tag.decanonized_at %>
<p class="notes">
<% if @tag.canonized_at %>
<%= t(".last_canonized", date: @tag.canonized_at.utc.strftime("%Y-%m-%d %H:%M:%S")) %>

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need strftime here? I get the desired format just by using utc (although, to be fair, I tried it on @tag.updated_at since that was what I had handy)

<% end %>
<% if @tag.decanonized_at %>
<%= t(".last_decanonized", date: @tag.decanonized_at.utc.strftime("%Y-%m-%d %H:%M:%S")) %>
<% end %>
</p>
<% end %>
<% end %>

<%= form_for @tag, as: :tag, url: { action: "update", id: @tag}, html: { method: :put } do |f| %>
Expand Down
2 changes: 2 additions & 0 deletions config/locales/views/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3078,6 +3078,8 @@ en:
wrangling_tools: Wrangling Tools
tags:
edit:
last_canonized: Last canonized on %{date} UTC.
last_decanonized: Last decanonized on %{date} UTC.
save_changes: Save changes
submit_legend: Submit
index:
Expand Down
10 changes: 10 additions & 0 deletions db/migrate/20260725153049_add_canonization_dates_to_tags.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class AddCanonizationDatesToTags < ActiveRecord::Migration[8.1]
uses_departure! if Rails.env.staging? || Rails.env.production?

def change
change_table :tags, bulk: true do |t|
t.datetime :canonized_at, default: nil, null: true
t.datetime :decanonized_at, default: nil, null: true
end
end
end
30 changes: 30 additions & 0 deletions features/tags_and_wrangling/tag_wrangling_admin.feature
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,36 @@ Feature: Tag wrangling
Then I should not see "Amelie"
And I should see "Amélie"

Scenario: Admin can see when a tag was last canonized and decanonized

Given I am logged in as a "tag_wrangling" admin
And a fandom exists with name: "Amelie", canonical: false
When I edit the tag "Amelie"
Then I should not see "Last canonized on"
And I should not see "Last decanonized on"
When I check "Canonical"
And I press "Save changes"
And I edit the tag "Amelie"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once you save changes, you should be redirected to the tag edit page, so this step would ideally be unnecessary... Did you try it without? (I can understand if you did and it failed due to the test running too quickly, but I wanted to ask just the same.)

Then I should see "Last canonized on"
And I should not see "Last decanonized on"
When I uncheck "Canonical"
And I press "Save changes"
And I edit the tag "Amelie"
Then I should see "Last canonized on"
And I should see "Last decanonized on"

Scenario: Tag wrangler cannot see when a tag was last canonized or decanonized

Given I am logged in as a "tag_wrangling" admin
And a fandom exists with name: "Amelie", canonical: true
When I edit the tag "Amelie"
And I uncheck "Canonical"
And I press "Save changes"
And I am logged in as a tag wrangler
And I edit the tag "Amelie"
Then I should not see "Last canonized on"
And I should not see "Last decanonized on"

Scenario: Admin can rename a tag using Eastern characters

Given I am logged in as a "tag_wrangling" admin
Expand Down
10 changes: 10 additions & 0 deletions spec/models/search/tag_query_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,16 @@
expect(q.generated_query[:sort]).to eq([{ "created_at" => { order: "asc", unmapped_type: "date" } }, { id: { order: "asc" } }])
end

it "allows you to sort by Date Canonized" do
q = TagQuery.new(sort_column: "canonized_at")
expect(q.generated_query[:sort]).to eq([{ "canonized_at" => { order: "desc", unmapped_type: "date" } }, { id: { order: "desc" } }])
end

it "allows you to sort by Date Decanonized" do
q = TagQuery.new(sort_column: "decanonized_at")
expect(q.generated_query[:sort]).to eq([{ "decanonized_at" => { order: "desc", unmapped_type: "date" } }, { id: { order: "desc" } }])
end

it "allows you to sort by Uses" do
q = TagQuery.new(sort_column: "uses")
expect(q.generated_query[:sort]).to eq([{ "uses" => { order: "desc" } }, { "name.keyword" => { order: "asc" } }, { id: { order: "desc" } }])
Expand Down
43 changes: 43 additions & 0 deletions spec/models/tag_wrangling_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,42 @@
end.to(add_to_reindex_queue(work, :background) &
not_add_to_reindex_queue(work, :main))
end

it "sets canonized_at" do
freeze_time do
fandom.update!(canonical: true)
expect(fandom.canonized_at).to eq(Time.current)
expect(fandom.decanonized_at).to be_nil
end
end
end

context "when a tag is created as canonical" do
it "sets canonized_at" do
freeze_time do
fandom = create(:canonical_fandom)
expect(fandom.canonized_at).to eq(Time.current)
expect(fandom.decanonized_at).to be_nil
end
end
end

context "when a tag is created as non-canonical" do
it "does not set canonized_at or decanonized_at" do
fandom = create(:fandom)
expect(fandom.canonized_at).to be_nil
expect(fandom.decanonized_at).to be_nil
end
end

context "when canonical does not change" do
it "does not change canonized_at or decanonized_at" do
fandom = create(:canonical_fandom)
expect do
fandom.update!(unwrangleable: false)
end.to avoid_changing { fandom.reload.canonized_at } &
avoid_changing { fandom.reload.decanonized_at }
end
end

context "when canonical becomes false" do
Expand All @@ -37,6 +73,13 @@
not_add_to_reindex_queue(work, :main))
end

it "sets decanonized_at" do
freeze_time do
fandom.update!(canonical: false)
expect(fandom.decanonized_at).to eq(Time.current)
end
end

it "removes favorite tags" do
user = create(:user)
user.favorite_tags.create(tag: fandom)
Expand Down
Loading