Skip to content

Commit

Permalink
Use content_id for matching list items
Browse files Browse the repository at this point in the history
  • Loading branch information
unoduetre committed Jul 26, 2024
1 parent a280838 commit f2dfdc5
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 24 deletions.
5 changes: 3 additions & 2 deletions app/controllers/lists_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,11 @@ def edit_list_items_params
def save_list_items
@list.errors.add(:list_items, "Select a link to add to the list") and return false if edit_list_items_params.blank?

edit_list_items_params.each do |base_path|
linked_item = @available_list_items.select { |item| item.base_path == base_path }.first
edit_list_items_params.each do |content_id|
linked_item = @available_list_items.select { |item| item.content_id == content_id }.first
@list.list_items.create!(
base_path: linked_item.base_path,
content_id: linked_item.content_id,
title: linked_item.title,
index: @list.list_items.length + 1,
)
Expand Down
10 changes: 5 additions & 5 deletions app/models/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def tagged_list_items

def list_items_with_tagging_status
@list_items_with_tagging_status ||= list_items.order(:index).map do |list_item|
list_item.tagged = tagged_base_paths.include?(list_item.base_path)
list_item.tagged = tagged_content_ids.include?(list_item.content_id)
list_item
end
end
Expand All @@ -24,14 +24,14 @@ def available_list_items
.documents
.reject do |link|
list_items
.map(&:base_path)
.include?(link["base_path"])
.map(&:content_id)
.include?(link["content_id"])
end
end

private

def tagged_base_paths
@tagged_base_paths ||= tag.tagged_documents.map(&:base_path)
def tagged_content_ids
@tagged_content_ids ||= tag.tagged_documents.map(&:content_id)
end
end
4 changes: 0 additions & 4 deletions app/models/list_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,4 @@ class ListItem < ApplicationRecord
def display_title
list.tag.tagged_document_for_base_path(base_path).try(:title) || title
end

def content_id
list.tag.tagged_document_for_base_path(base_path).content_id
end
end
2 changes: 1 addition & 1 deletion app/views/lists/edit_list_items.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
items: @list.available_list_items.map do |list_item|
{
label: list_item.title,
value: list_item.base_path,
value: list_item.content_id,
}
end,
} %>
Expand Down
4 changes: 2 additions & 2 deletions spec/controllers/list_items_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@
let!(:list_item2) { create(:list_item, list: list1, index: 2) }
let(:tagged_documents) do
[
TaggedDocuments::Document.new(list_item1.title, list_item1.base_path, "123"),
TaggedDocuments::Document.new(list_item2.title, list_item2.base_path, "456"),
TaggedDocuments::Document.new(list_item1.title, "/some-path-1", list_item1.content_id),
TaggedDocuments::Document.new(list_item2.title, "/some-path-2", list_item2.content_id),
TaggedDocuments::Document.new("New list", "/new-list", "789"),
]
end
Expand Down
12 changes: 7 additions & 5 deletions spec/controllers/lists_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@
let!(:list_item2) { create(:list_item, list:, index: 2) }
let(:tagged_documents) do
[
TaggedDocuments::Document.new(list_item1.title, list_item1.base_path, "123"),
TaggedDocuments::Document.new(list_item2.title, list_item2.base_path, "456"),
TaggedDocuments::Document.new(list_item1.title, "/some-path-1", list_item1.content_id),
TaggedDocuments::Document.new(list_item2.title, "/some-path-2", list_item2.content_id),
TaggedDocuments::Document.new("New list", "/new-list", "789"),
TaggedDocuments::Document.new("Newer list", "/newer-list", "012"),
]
Expand All @@ -84,17 +84,19 @@
patch :update_list_items, params: {
tag_id: tag.content_id,
id: list.id,
list: { list_items: ["/new-list", "/newer-list"] },
list: { list_items: %w[789 012] },
}

new_list_item1, new_list_item2 = list.list_items.last(2)

expect(list.list_items.count).to eq 4
expect(new_list_item1.title).to eq "New list"
expect(new_list_item1.base_path).to eq "/new-list"
expect(new_list_item1.content_id).to eq "789"
expect(new_list_item1.index).to eq 3
expect(new_list_item2.title).to eq "Newer list"
expect(new_list_item2.base_path).to eq "/newer-list"
expect(new_list_item2.content_id).to eq "012"
expect(new_list_item2.index).to eq 4
expect(response.status).to eq(302)
expect(response).to redirect_to(tag_list_path(tag, list))
Expand Down Expand Up @@ -176,8 +178,8 @@
let!(:list_item2) { create(:list_item, list:, index: 2) }
let(:tagged_documents) do
[
TaggedDocuments::Document.new(list_item1.title, list_item1.base_path, "123"),
TaggedDocuments::Document.new(list_item2.title, list_item2.base_path, "456"),
TaggedDocuments::Document.new(list_item1.title, "/some-path-1", list_item1.content_id),
TaggedDocuments::Document.new(list_item2.title, "/some-path-2", list_item2.content_id),
TaggedDocuments::Document.new("New list", "/new-list", "789"),
]
end
Expand Down
1 change: 1 addition & 0 deletions spec/factories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@
factory :list_item do
sequence(:title) { |n| "List item #{n}" }
sequence(:base_path) { |n| "/base-path-#{n}" }
content_id { SecureRandom.uuid }
sequence(:index)
end

Expand Down
10 changes: 5 additions & 5 deletions spec/models/list_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
describe "#list_items_with_tagging_status" do
it "returns the list items with tagged set to true if they're tagged" do
list = create(:list, tag: create(:tag, slug: "subtag"))
create(:list_item, list:, base_path: "/tagged-item")
create(:list_item, list:, base_path: "/untagged-item")
publishing_api_has_linked_items(list.tag.content_id, items: [{ base_path: "/tagged-item" }])
create(:list_item, list:, content_id: "123")
create(:list_item, list:, content_id: "456")
publishing_api_has_linked_items(list.tag.content_id, items: [{ content_id: "123" }])

list_item = list.list_items_with_tagging_status.first

Expand All @@ -40,7 +40,7 @@

it "returns the list items with tagged set to false if they're not tagged" do
list = create(:list, tag: create(:tag, slug: "subtag"))
create(:list_item, list:, base_path: "/untagged-item")
create(:list_item, list:, content_id: "456")
publishing_api_has_no_linked_items

list_item = list.list_items_with_tagging_status.first
Expand All @@ -52,7 +52,7 @@
describe "#available_list_items" do
it "returns tagged list items from the publishing api that are in the list" do
list = create(:list)
create(:list_item, list:, base_path: "/item-in-list")
create(:list_item, list:, content_id: "123")
publishing_api_has_linked_items(
list.tag.content_id,
items: [
Expand Down

0 comments on commit f2dfdc5

Please sign in to comment.