Skip to content

Commit

Permalink
Fix 'Save and add another', 'Save and edit', 'Cancel' buttons didn't …
Browse files Browse the repository at this point in the history
…work right

Fixes #3468
  • Loading branch information
mshibuya committed Feb 15, 2022
1 parent d96def6 commit ac0a563
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 18 deletions.
27 changes: 16 additions & 11 deletions spec/integration/actions/edit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -688,18 +688,21 @@ class HelpTest < Tableless

context 'on cancel' do
before do
@ball = FactoryBot.create :ball
visit '/admin/ball?sort=color'
@player = FactoryBot.create :player
visit '/admin/player'
click_link 'Edit'
end

it "shows cancel button with 'novalidate' attribute" do
expect(page).to have_css '[type="submit"][name="_continue"][formnovalidate]'
it 'sends back to previous URL', js: true do
find_button('Cancel').trigger('click')
is_expected.to have_text 'No actions were taken'
expect(page.current_path).to eq('/admin/player')
end

it 'sends back to previous URL' do
click_button 'Cancel'
expect(page.current_url).to eq('http://www.example.com/admin/ball?sort=color')
it 'allows submit even if client-side validation is not satisfied', js: true do
fill_in 'player[name]', with: ''
find_button('Cancel').trigger('click')
is_expected.to have_text 'No actions were taken'
end
end

Expand Down Expand Up @@ -738,7 +741,7 @@ class HelpTest < Tableless
end
end

describe 'update and edit' do
describe 'update and edit', js: true do
before do
@player = FactoryBot.create :player

Expand All @@ -747,12 +750,14 @@ class HelpTest < Tableless
fill_in 'player[name]', with: 'Jackie Robinson'
fill_in 'player[number]', with: '42'
fill_in 'player[position]', with: 'Second baseman'
click_button 'Save and edit'

@player.reload
find_button('Save and edit').trigger('click')
end

it 'updates an object with correct attributes' do
is_expected.to have_text 'Player successfully updated'
expect(page.current_path).to eq("/admin/player/#{@player.id}/edit")

@player.reload
expect(@player.name).to eq('Jackie Robinson')
expect(@player.number).to eq(42)
expect(@player.position).to eq('Second baseman')
Expand Down
10 changes: 6 additions & 4 deletions spec/integration/actions/new_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,19 +121,21 @@
end
end

context 'on create and add another' do
context 'on create and add another', js: true do
before do
visit new_path(model_name: 'player')

fill_in 'player[name]', with: 'Jackie Robinson'
fill_in 'player[number]', with: '42'
fill_in 'player[position]', with: 'Second baseman'
click_button 'Save and add another'

@player = RailsAdmin::AbstractModel.new('Player').first
find_button('Save and add another').trigger('click')
end

it 'creates an object with correct attributes' do
is_expected.to have_text 'Player successfully created'
expect(page.current_path).to eq('/admin/player/new')

@player = RailsAdmin::AbstractModel.new('Player').first
expect(@player.name).to eq('Jackie Robinson')
expect(@player.number).to eq(42)
expect(@player.position).to eq('Second baseman')
Expand Down
18 changes: 15 additions & 3 deletions src/rails_admin/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,21 @@ import I18n from "./i18n";
.each(function () {
$(this).siblings(".control-group").hide();
});
$("[formnovalidate]").on("click", function () {
$(this).closest("form").attr("novalidate", true);
});
$(".form-actions .extra_buttons button")
.attr("type", "button")
.on("click", function () {
var form = $(this).closest("form");
form.append(
$("<input />")
.attr("type", "hidden")
.attr("name", $(this).attr("name"))
.attr("value", true)
);
if ($(this).is("[formnovalidate]")) {
form.attr("novalidate", true);
}
form.trigger("submit");
});
$.each($("#filters_box").data("options"), function () {
$.filters.append(this);
});
Expand Down

0 comments on commit ac0a563

Please sign in to comment.