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

Commit 41d9c91

Browse files
Shpigfordclaude
andcommitted
Fix: Move author assignment logic to Article model
- Remove custom create/update methods from Avo resource - Implement author_id= method in Article model to handle authorship - Simplify Avo resource configuration - Handle author assignment through model's virtual attribute This approach is cleaner and follows Rails conventions better. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 4b8aaf3 commit 41d9c91

2 files changed

Lines changed: 10 additions & 50 deletions

File tree

app/avo/resources/article.rb

Lines changed: 1 addition & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -15,60 +15,12 @@ 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 }, use_resource: Avo::Resources::Author
18+
field :author, as: :belongs_to, searchable: true
1919

2020
tabs do
2121
tab "Author Details" do
2222
field :authorship, as: :has_one
2323
end
2424
end
2525
end
26-
27-
def create_model_record(model, params, **args)
28-
Rails.logger.debug "=== Article Create Params: #{params.inspect}"
29-
30-
# Try different possible param keys
31-
author_id = params.delete(:author_id) || params.delete("author_id") || params.dig(:author, :id) || params.dig("author", "id")
32-
Rails.logger.debug "=== Author ID extracted: #{author_id.inspect}"
33-
34-
super(model, params, **args)
35-
36-
handle_author_assignment(model, author_id)
37-
38-
model
39-
end
40-
41-
def update_model_record(model, params, **args)
42-
Rails.logger.debug "=== Article Update Params: #{params.inspect}"
43-
44-
# Try different possible param keys
45-
author_id = params.delete(:author_id) || params.delete("author_id") || params.dig(:author, :id) || params.dig("author", "id")
46-
Rails.logger.debug "=== Author ID extracted: #{author_id.inspect}"
47-
48-
super(model, params, **args)
49-
50-
handle_author_assignment(model, author_id)
51-
52-
model
53-
end
54-
55-
private
56-
57-
def handle_author_assignment(model, author_id)
58-
Rails.logger.debug "=== Handling author assignment with ID: #{author_id.inspect}"
59-
60-
if author_id.present?
61-
# Remove existing authorship if present
62-
model.authorship&.destroy
63-
Rails.logger.debug "=== Destroyed existing authorship"
64-
65-
# Create new authorship with the selected author
66-
authorship = model.create_authorship(author_id: author_id, role: "primary")
67-
Rails.logger.debug "=== Created new authorship: #{authorship.inspect}, valid: #{authorship.valid?}, errors: #{authorship.errors.full_messages}"
68-
elsif author_id == ""
69-
# If author_id is blank, remove the authorship
70-
model.authorship&.destroy
71-
Rails.logger.debug "=== Removed authorship (blank author_id)"
72-
end
73-
end
7426
end

app/models/article.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,15 @@ def author_id
2626
end
2727

2828
def author_id=(id)
29-
# This is handled in the Avo resource
29+
if id.blank?
30+
self.authorship&.destroy
31+
else
32+
if self.authorship
33+
self.authorship.update(author_id: id)
34+
else
35+
self.create_authorship(author_id: id, role: "primary")
36+
end
37+
end
3038
end
3139

3240
include MetaImage

0 commit comments

Comments
 (0)