Skip to content

Commit fd97e15

Browse files
committed
Preserver the page parameter when editing #47
1 parent 1d9028f commit fd97e15

File tree

9 files changed

+79
-17
lines changed

9 files changed

+79
-17
lines changed

app/controllers/tolaria/resource_controller.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ def index
66
@search = @managed_class.klass.ransack(ransack_params)
77
@resources = @search.result
88
if @managed_class.paginated?
9-
@resources = @resources.page(params[:page]).per(Tolaria.config.page_size)
9+
@resources = @resources.page(page_param).per(Tolaria.config.page_size)
1010
end
1111
unless currently_sorting?
1212
@resources = @resources.order(@managed_class.default_order)
@@ -92,7 +92,7 @@ def random_blingword
9292
# Handles route forbidding cases.
9393
def form_completion_redirect_path(managed_class, resource = nil)
9494
if managed_class.allows?(:index) && params[:save_and_review].blank?
95-
url_for(action:"index", q:ransack_params)
95+
url_for(action:"index", q:ransack_params, p:page_param)
9696
elsif managed_class.allows?(:show) && resource.present?
9797
url_for(action:"show", id:resource.id)
9898
elsif managed_class.allows?(:edit) && resource.present?
@@ -128,6 +128,14 @@ def log_validation_errors!
128128
end
129129
end
130130

131+
# Returns the current `params[:p]` as an Integer if it is valid.
132+
# Returns `nil` otherwise.
133+
def page_param
134+
integer_page = params[:p].to_i
135+
return integer_page if integer_page > 0
136+
return nil
137+
end
138+
131139
# Returns params[:q] as a hash if it can be converted.
132140
# Ransack expects this generic hash and has its own internal
133141
# logic for handing the many possible keys of the hash.
@@ -153,6 +161,7 @@ def currently_filtering?
153161
end
154162
end
155163

164+
helper_method :page_param
156165
helper_method :ransack_params
157166
helper_method :currently_sorting?
158167
helper_method :currently_filtering?

app/helpers/admin/table_helper.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,9 @@ def index_td(resource, method_or_content = {}, options = {}, &block)
137137
end
138138

139139
if @managed_class.allows? :edit
140-
link = url_for(action:"edit", id:resource.id, q:ransack_params)
140+
link = url_for(action:"edit", id:resource.id, q:ransack_params, p:page_param)
141141
elsif @managed_class.allows? :show
142-
link = url_for(action:"show", id:resource.id, q:ransack_params)
142+
link = url_for(action:"show", id:resource.id, q:ransack_params, p:page_param)
143143
else
144144
link = "#" # Guh, painted ourseves into a corner here
145145
end
@@ -162,15 +162,15 @@ def actions_td(resource)
162162
links = []
163163

164164
if @managed_class.allows?(:edit)
165-
links << link_to("Edit", url_for(action:"edit", id:resource.id, q:ransack_params), class:"button -small")
165+
links << link_to("Edit", url_for(action:"edit", id:resource.id, q:ransack_params, p:page_param), class:"button -small -edit")
166166
end
167167

168168
if @managed_class.allows?(:show)
169-
links << link_to("Inspect", url_for(action:"show", id:resource.id, q:ransack_params), class:"button -small")
169+
links << link_to("Inspect", url_for(action:"show", id:resource.id, q:ransack_params, p:page_param), class:"button -small -inspect")
170170
end
171171

172172
if @managed_class.allows?(:destroy)
173-
links << link_to("Delete", url_for(action:"destroy", id:resource.id, q:ransack_params), class: "button -small", method: :delete, :'data-confirm' => deletion_warning(resource))
173+
links << link_to("Delete", url_for(action:"destroy", id:resource.id, q:ransack_params, p:page_param), class: "button -small -delete", method: :delete, :'data-confirm' => deletion_warning(resource))
174174
end
175175

176176
return content_tag(:td, links.join("").html_safe, class:"actions-td")

app/views/admin/tolaria_resource/_form_buttons.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<% if @managed_class.allows? :index %>
2-
<%= link_to url_for(action:"index", q:ransack_params), class:"button -cancel" do %>
2+
<%= link_to url_for(action:"index", q:ransack_params, p:page_param), class:"button -cancel" do %>
33
<%= fontawesome_icon :close %>
44
Cancel
55
<% end %>

app/views/admin/tolaria_resource/_index_table.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,6 @@
7070
<p class="pagination-desc"><%= page_entries_info @resources %></p>
7171
<% end %>
7272
73-
<%= paginate @resources, theme:"admin", window:2 %>
73+
<%= paginate @resources, theme:"admin", window:2, param_name:"p" %>
7474
7575
<% end %>

app/views/admin/tolaria_resource/edit.html.erb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,28 @@
66

77
<%= form_for [:admin, @resource], url:contextual_form_url, builder:Admin::FormBuilder, html:{class:"resource-form"} do |form_builder| %>
88

9+
<% if page_param.present? %>
10+
11+
<% end %>
12+
913
<% if ransack_params.present? %>
1014
<% ransack_params.each do |key, value| %>
1115
<% next if value.respond_to?(:keys) %>
1216
<%= hidden_field_tag "q[#{key}]", value %>
1317
<% end %>
1418
<% end %>
1519

20+
<% if page_param.present? %>
21+
<%= hidden_field_tag :p, page_param %>
22+
<% end %>
23+
1624
<div class="main-controls">
1725
<div class="main-controls-left">
1826
<h1>
1927
<span class="crumb">
2028
<%= fontawesome_icon @managed_class.icon %>
2129
<% if @managed_class.allows? :index %>
22-
<%= link_to @managed_class.model_name.human.pluralize.titleize, url_for(action:"index", controller:@managed_class.plural, q:ransack_params) %>
30+
<%= link_to @managed_class.model_name.human.pluralize.titleize, url_for(action:"index", controller:@managed_class.plural, q:ransack_params, p:page_param) %>
2331
<% else %>
2432
<%= @managed_class.model_name.human.pluralize.titleize %>
2533
<% end %>

app/views/admin/tolaria_resource/index.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
<% end %>
2727

2828
<% if @managed_class.allows?(:new) %>
29-
<%= link_to url_for(action:"new", q:ransack_params), class:"button -primary" do %>
29+
<%= link_to url_for(action:"new", q:ransack_params, p:page_param), class:"button -primary" do %>
3030
<%= fontawesome_icon :plus %>
3131
New <%= @managed_class.model_name.human.titleize %>
3232
<% end %>

app/views/admin/tolaria_resource/show.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<span class="crumb">
88
<%= fontawesome_icon @managed_class.icon %>
99
<% if @managed_class.allows? :index %>
10-
<%= link_to @managed_class.model_name.human.pluralize.titleize, url_for(action:"index", controller:@managed_class.plural, q:ransack_params) %>
10+
<%= link_to @managed_class.model_name.human.pluralize.titleize, url_for(action:"index", controller:@managed_class.plural, q:ransack_params, p:page_param) %>
1111
<% else %>
1212
<%= @managed_class.model_name.human.pluralize.titleize %>
1313
<% end %>

test/integration/filter_preservation_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def teardown
2525
find_link("Organization").click
2626
find_link("Nintendo").click
2727
first(".button.-cancel").click
28-
assert page.current_url.include?("q[s]=organization+asc"), "filter not retained"
28+
assert page.current_url.include?("q[s]=organization+asc"), "filter should be retained"
2929
end
3030

3131
test "after filtering index, should retain filter on edit and save" do

test/integration/pagination_test.rb

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
class PaginationTest < ActionDispatch::IntegrationTest
44

5-
test "shows pagination and its usable" do
6-
5+
def setup
6+
BlogPost.destroy_all
77
50.times do
88
BlogPost.create!({
99
title: SecureRandom.uuid,
@@ -12,20 +12,65 @@ class PaginationTest < ActionDispatch::IntegrationTest
1212
published_at: Time.current,
1313
})
1414
end
15+
end
1516

17+
def teardown
18+
BlogPost.destroy_all
19+
end
20+
21+
test "shows pagination and its usable" do
1622
sign_in_dummy_administrator!
1723
visit("/admin/blog_posts")
1824
assert page.has_content?("Next")
1925
assert page.has_content?("Last")
2026
assert page.has_content?("3")
21-
visit("/admin/blog_posts?page=3")
27+
visit("/admin/blog_posts?p=3")
2228
assert page.has_content?("Next")
2329
assert page.has_content?("Last")
2430
assert page.has_content?("First")
2531
assert page.has_content?("Prev")
32+
end
2633

27-
BlogPost.destroy_all
34+
test "after paginating index, should retain page on edit and back with crumb" do
35+
sign_in_dummy_administrator!
36+
visit("/admin/blog_posts?p=3")
37+
first(".button.-edit").click # Open the editor form for a random blog post
38+
first(".crumb a").click
39+
assert page.current_url.include?("p=3"), "page should be retained"
40+
end
41+
42+
test "after paginating index, should retain page on edit and back with button" do
43+
sign_in_dummy_administrator!
44+
visit("/admin/blog_posts?p=3")
45+
first(".button.-edit").click # Open the editor form for a random blog post
46+
first(".button.-cancel").click
47+
assert page.current_url.include?("p=3"), "page should be retained"
48+
end
2849

50+
test "after paginating index, should retain page on edit and save" do
51+
sign_in_dummy_administrator!
52+
visit("/admin/blog_posts?p=3")
53+
first(".button.-edit").click # Open the editor form for a random blog post
54+
first(".button.-save").click
55+
assert page.current_url.include?("p=3"), "page should be retained"
56+
end
57+
58+
test "after paginating index, should retain page on edit and failed validation" do
59+
sign_in_dummy_administrator!
60+
visit("/admin/blog_posts?p=3")
61+
first(".button.-edit").click # Open the editor form for a random blog post
62+
fill_in("blog_post[title]", with:"")
63+
first(".button.-save").click
64+
first(".button.-cancel").click
65+
assert page.current_url.include?("p=3"), "page should be retained"
66+
end
67+
68+
test "after paginating index, should NOT retain page on save and review" do
69+
sign_in_dummy_administrator!
70+
visit("/admin/blog_posts?p=3")
71+
first(".button.-edit").click # Open the editor form for a random blog post
72+
first(".button.-save-and-review").click
73+
assert page.current_url.exclude?("p=3"), "page should not have been retained"
2974
end
3075

3176
end

0 commit comments

Comments
 (0)