Skip to content

Commit

Permalink
feat(alchemy_form_for): Allow to disable remote
Browse files Browse the repository at this point in the history
In order to use `alchemy_form_for` in `turbo_frame_tag`s,
we need to be able to disable the Ajax remote form feature
from `rails-ujs`.
  • Loading branch information
tvdeyen committed Sep 25, 2024
1 parent 4c20717 commit 885ede5
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/helpers/alchemy/admin/form_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module FormHelper
def alchemy_form_for(object, *args, &block)
options = args.extract_options!
options[:builder] = Alchemy::Forms::Builder
options[:remote] = request.xhr?
options.key?(:remote) || options[:remote] = request.xhr?
options[:html] = {
id: options.delete(:id),
class: ["alchemy", options.delete(:class)].compact.join(" ")
Expand Down
46 changes: 46 additions & 0 deletions spec/helpers/alchemy/admin/form_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
require "rails_helper"

RSpec.describe Alchemy::Admin::FormHelper do
describe "#alchemy_form_for" do
subject { helper.alchemy_form_for(resource) {} }

let(:resource) do
[alchemy, :admin, Alchemy::Element.new(name: "article")]
end

it "returns a form with alchemy class" do
expect(subject).to have_css(".alchemy")
end

context "if options[:remote] is given" do
context "and set to true" do
subject { helper.alchemy_form_for(resource, remote: true) {} }

it "makes the form remote" do
expect(subject).to have_css("form[data-remote]")
end
end

context "and set to false" do
subject { helper.alchemy_form_for(resource, remote: false) {} }

it "makes the form non-remote" do
expect(subject).to have_css("form")
expect(subject).to_not have_css("form[data-remote]")
end
end
end

context "if options[:remote] is not given" do
context "and request is xhr" do
before do
allow(helper).to receive(:request).and_return(double(xhr?: true))
end

it "makes the form remote" do
expect(subject).to have_css("form[data-remote]")
end
end
end
end
end

0 comments on commit 885ede5

Please sign in to comment.