Skip to content
This repository was archived by the owner on Jul 25, 2025. It is now read-only.

Commit f5b4d45

Browse files
Shpigfordclaude
andcommitted
Fix: Properly handle article author updates in Avo
- Add create_model_record to handle author assignment on new articles - Extract author assignment logic to private method - Add virtual author_id attribute to Article model for Avo compatibility - Specify use_resource for Author to ensure proper resource handling - Add handler for both create and update operations The author association now properly saves when creating or updating articles. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent d4dd12c commit f5b4d45

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

app/avo/resources/article.rb

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def fields
1515
field :content, as: :easy_mde
1616
field :publish_at, as: :date_time
1717
field :author_name, as: :text, hide_on: [ :forms ]
18-
field :author, as: :belongs_to, searchable: true, display_with_value: -> { record.name }
18+
field :author, as: :belongs_to, searchable: true, display_with_value: -> { record.name }, use_resource: Avo::Resources::Author
1919

2020
tabs do
2121
tab "Author Details" do
@@ -24,11 +24,29 @@ def fields
2424
end
2525
end
2626

27+
def create_model_record(model, params, **args)
28+
author_id = params.delete(:author_id)
29+
30+
super(model, params, **args)
31+
32+
handle_author_assignment(model, author_id)
33+
34+
model
35+
end
36+
2737
def update_model_record(model, params, **args)
2838
author_id = params.delete(:author_id)
2939

3040
super(model, params, **args)
3141

42+
handle_author_assignment(model, author_id)
43+
44+
model
45+
end
46+
47+
private
48+
49+
def handle_author_assignment(model, author_id)
3250
if author_id.present?
3351
# Remove existing authorship if present
3452
model.authorship&.destroy
@@ -39,7 +57,5 @@ def update_model_record(model, params, **args)
3957
# If author_id is blank, remove the authorship
4058
model.authorship&.destroy
4159
end
42-
43-
model
4460
end
4561
end

app/models/article.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@ class Article < ApplicationRecord
2020
scope :published, -> { where.not(publish_at: nil).where("publish_at <= ?", Time.current) }
2121
scope :latest, -> { order(publish_at: :desc) }
2222

23+
# Virtual attribute for Avo
24+
def author_id
25+
author&.id
26+
end
27+
28+
def author_id=(id)
29+
# This is handled in the Avo resource
30+
end
31+
2332
include MetaImage
2433

2534
def self.random_sample(count, exclude:)

app/views/authors/show.html.erb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<%
22
title @author.name
33
description @author.bio || "Read articles and content by #{@author.name}"
4-
4+
55
content_for :schema_data do
66
author_schema = {
77
"@context": "https://schema.org",
@@ -18,7 +18,7 @@
1818
"url": "https://maybefinance.com"
1919
}
2020
}.compact
21-
21+
2222
concat(tag.script(author_schema.to_json.html_safe, type: "application/ld+json"))
2323
end
2424
%>

0 commit comments

Comments
 (0)