Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simple pagination #1471

Merged
merged 11 commits into from
Sep 18, 2018
15 changes: 10 additions & 5 deletions app/assets/stylesheets/alchemy/pagination.scss
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
.pagination {
background: $light_gray;
position: fixed;
display: flex;
align-items: center;
background: $light_gray;
bottom: 0;
right: 0;
width: 100%;
left: 0px;
height: 52px;
line-height: 35px;
padding: 2*$default-padding;
padding-left: $main-menu-width + 10px;
text-align: left;
Expand All @@ -18,6 +19,7 @@
font-weight: bold;
color: $icon-color;
padding: $default-padding 2*$default-padding;
margin: 0 $default-margin/2;
}

a:hover {
Expand All @@ -28,15 +30,12 @@

span a { padding: 0 }

.page { padding: 0px 8px }

.current {
color: $blue;
border: 1px solid $blue;
text-shadow: 0px 1px 1px #fff;
cursor: default;
border-radius: $default-border-radius;
padding: 5px 8px;
}

.previous_page {
Expand All @@ -55,13 +54,19 @@
border: none;
background: transparent;
cursor: default;
margin: 0;
}

.icon {
color: inherit;
}
}

.per-page-select-form {
margin-left: auto;
display: inline;
}

#assign_image_list .pagination, #assign_file_list .pagination {
position: absolute;
padding-left: 8px;
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/alchemy/admin/attachments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def index

@attachments = @attachments
.page(params[:page] || 1)
.per(15)
.per(items_per_page)

if in_overlay?
archive_overlay
Expand Down
3 changes: 2 additions & 1 deletion app/controllers/alchemy/admin/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ def render_errors_or_redirect(object, redirect_url, flash_notice)
end

def per_page_value_for_screen_size
return 25 if session[:screen_size].blank?
Alchemy::Deprecation.warn("#per_page_value_for_screen_size is deprecated, please use #items_per_page instead")
return items_per_page if session[:screen_size].blank?
screen_height = session[:screen_size].split('x').last.to_i
(screen_height / 50) - 12
end
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/alchemy/admin/languages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Admin
class LanguagesController < ResourcesController
def index
@query = Language.on_current_site.ransack(search_filter_params[:q])
@languages = @query.result.page(params[:page] || 1).per(per_page_value_for_screen_size)
@languages = @query.result.page(params[:page] || 1).per(items_per_page)
end

def new
Expand Down
22 changes: 17 additions & 5 deletions app/controllers/alchemy/admin/pictures_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ class PicturesController < Alchemy::Admin::ResourcesController
def index
@size = params[:size].present? ? params[:size] : 'medium'
@query = Picture.ransack(search_filter_params[:q])
@pictures = Picture.search_by(search_filter_params, @query, pictures_per_page_for_size(@size))
@pictures = Picture.search_by(
search_filter_params,
@query,
items_per_page
)

if in_overlay?
archive_overlay
Expand Down Expand Up @@ -108,18 +112,26 @@ def destroy
redirect_to_index
end

def items_per_page
cookies[:alchemy_pictures_per_page] = params[:per_page] || cookies[:alchemy_pictures_per_page] || pictures_per_page_for_size(@size)
end

def items_per_page_options
per_page = pictures_per_page_for_size(@size)
[per_page, per_page * 2, per_page * 4]
end

private

def pictures_per_page_for_size(size)
case size
when 'small'
per_page = in_overlay? ? 25 : (per_page_value_for_screen_size * 2.9).floor
in_overlay? ? 25 : 60
when 'large'
per_page = in_overlay? ? 4 : (per_page_value_for_screen_size / 1.7).floor + 1
in_overlay? ? 4 : 12
else
per_page = in_overlay? ? 9 : (per_page_value_for_screen_size / 1.0).ceil + 4
in_overlay? ? 9 : 20
end
per_page
end

def in_overlay?
Expand Down
17 changes: 14 additions & 3 deletions app/controllers/alchemy/admin/resources_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ class ResourcesController < Alchemy::Admin::BaseController
include Alchemy::ResourcesHelper

helper Alchemy::ResourcesHelper, TagsHelper
helper_method :resource_handler, :search_filter_params
helper_method :resource_handler, :search_filter_params,
:items_per_page, :items_per_page_options

before_action :load_resource,
only: [:show, :edit, :update, :destroy]
Expand All @@ -37,7 +38,7 @@ def index

respond_to do |format|
format.html {
items = items.page(params[:page] || 1).per(per_page_value_for_screen_size)
items = items.page(params[:page] || 1).per(items_per_page)
instance_variable_set("@#{resource_handler.resources_name}", items)
}
format.csv {
Expand Down Expand Up @@ -147,9 +148,19 @@ def common_search_filter_includes
{q: resource_handler.search_field_name},
:tagged_with,
:filter,
:page
:page,
:per_page
].freeze
end

def items_per_page
cookies[:alchemy_items_per_page] = params[:per_page] || cookies[:alchemy_items_per_page] || Alchemy::Config.get(:items_per_page)
end

def items_per_page_options
per_page = Alchemy::Config.get(:items_per_page)
[per_page, per_page * 2, per_page * 4]
end
end
end
end
2 changes: 1 addition & 1 deletion app/controllers/alchemy/admin/tags_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def index
@tags = @query
.result
.page(params[:page] || 1)
.per(per_page_value_for_screen_size)
.per(items_per_page)
.order("name ASC")
end

Expand Down
3 changes: 2 additions & 1 deletion app/views/alchemy/admin/pictures/_archive.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@
<% end %>
<% else %>
<%= render partial: 'picture', collection: @pictures %>
<%= paginate @pictures, theme: 'alchemy' %>
<% end %>
</div>
<% end %>

<%= paginate @pictures, theme: 'alchemy' %>
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
<%= render partial: 'picture_to_assign',
collection: @pictures,
locals: {options: options_from_params, size: @size} %>
<%= paginate @pictures, theme: 'alchemy', remote: true %>
<%= paginate @pictures, theme: 'alchemy', remote: true, hide_per_page_select: true %>
<% end %>
20 changes: 20 additions & 0 deletions app/views/alchemy/admin/resources/_per_page_select.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<%= form_tag url_for, method: :get, class: 'per-page-select-form' do |f| %>
<% search_filter_params.reject { |k, _| k == 'page' || k == 'per_page' }.each do |key, value| %>
<% if value.is_a? Hash %>
<% value.each do |k, v| %>
<%= hidden_field_tag "#{key}[#{k}]", v %>
<% end %>
<% else %>
<%= hidden_field_tag key, value %>
<% end %>
<% end %>
<label>
<%= Alchemy.t(:items_per_page, model_name: resource_model.model_name.human(count: 2)) %>
<%= select_tag :per_page,
options_for_select(
items_per_page_options,
items_per_page
),
onchange: 'this.form.submit()' %>
</label>
<% end %>
13 changes: 8 additions & 5 deletions app/views/kaminari/alchemy/_paginator.html.erb
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<%# The container tag
- available local variables
current_page: a page object for the currently displayed page
num_pages: total number of pages
total_pages: total number of pages
per_page: number of items to fetch per page
remote: data-remote
paginator: the paginator that renders the pagination tags inside
-%>
<%= paginator.render do -%>
<div class="pagination">
<div class="pagination">
<%= paginator.render do -%>
<%= first_page_tag %>
<%= prev_page_tag -%>
<% each_page do |page| -%>
Expand All @@ -19,5 +19,8 @@
<% end -%>
<%= next_page_tag -%>
<%= last_page_tag %>
</div>
<% end -%>
<% end -%>
<% unless local_assigns[:hide_per_page_select] %>
<%= render 'alchemy/admin/resources/per_page_select' %>
<% end %>
</div>
5 changes: 5 additions & 0 deletions config/alchemy/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ sitemap:
#
url_nesting: true

# === Default items per page in admin views
#
# In Alchemy's Admin, change how many items you would get shown per page by Kaminari
items_per_page: 15

# === Picture rendering settings
#
# Alchemy uses Dragonfly to render images. Use {size: "XXXxYYY", crop: BOOLEAN [true]} to resize images.
Expand Down
1 change: 1 addition & 0 deletions config/locales/alchemy.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ en:
"Hide childpages": "Hide childpages"
hide_elements: Hide Elements
hide_element: "Hide element"
items_per_page: "%{model_name} per page"
"Image missing": "Image missing"
"Image size": "Image size"
"Language successfully created": "Language successfully created."
Expand Down
3 changes: 3 additions & 0 deletions spec/dummy/config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ en:
event:
one: Event
other: Events
location:
one: Location
other: Locations
attributes:
event:
tag_names: Tags
Expand Down