Skip to content

Commit a501b76

Browse files
zhephynPaul-Bob
andauthored
Fix disabled and readonly on trix field (#3939)
* Initial approach * Fix Layout/TrailingEmptyLines lint CI failure * Add tests for the bug fix * Fix Style/StringLiterals lint CI failure * Refactor to use component_for_view method * Fix Lint CI Failure * Fix failing system and feature tests * tweaks * view inquirer --------- Co-authored-by: Paul Bob <[email protected]> Co-authored-by: Paul Bob <[email protected]>
1 parent d4622d4 commit a501b76

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

lib/avo/fields/trix_field.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@ def attachments_disabled
3737
# Disable attachments if attachment_key is not present
3838
@attachment_key.blank?
3939
end
40+
41+
def component_for_view(view)
42+
view = Avo::ViewInquirer.new(view)
43+
44+
if view.edit? && (is_readonly? || is_disabled?)
45+
Avo::Fields::TrixField::ShowComponent
46+
else
47+
super
48+
end
49+
end
4050
end
4151
end
4252
end

spec/system/avo/trix_field_spec.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,44 @@
125125
end
126126
end
127127
end
128+
129+
describe "trix field with disabled/readonly set to true" do
130+
let!(:body) { "This is test content for disabled state" }
131+
let!(:post) { create :post, body: body }
132+
133+
context "when disabled is set to true" do
134+
it "displays show page content when disabled is set to true for a trix field" do
135+
Avo::Resources::Post.with_temporary_items do
136+
field :body, as: :trix, disabled: true
137+
end
138+
139+
visit "/admin/resources/posts/#{post.id}/edit"
140+
141+
expect(page).not_to have_selector("trix-editor")
142+
143+
expect(page).to have_selector(".trix-content")
144+
expect(page).to have_text(body)
145+
146+
Avo::Resources::Post.restore_items_from_backup
147+
end
148+
end
149+
context "when readonly is set to true" do
150+
it "displays show page content when readonly is set to true for a trix field" do
151+
Avo::Resources::Post.with_temporary_items do
152+
field :body, as: :trix, readonly: true
153+
end
154+
155+
visit "/admin/resources/posts/#{post.id}/edit"
156+
157+
expect(page).not_to have_selector("trix-editor")
158+
159+
expect(page).to have_selector(".trix-content")
160+
expect(page).to have_text(body)
161+
162+
Avo::Resources::Post.restore_items_from_backup
163+
end
164+
end
165+
end
128166
end
129167

130168
def fill_in_trix_editor(id, with:)

0 commit comments

Comments
 (0)