diff --git a/Gemfile b/Gemfile index 2c1c8c1..0b57b6c 100644 --- a/Gemfile +++ b/Gemfile @@ -85,7 +85,9 @@ gem 'yard', '>= 0.9.5' gem 'redcarpet', '>= 3.3.4' - +# ------------------------------------------------ +# PAGINATION +gem 'kaminari', '>= 1.0' # ------------------------------------------------ # ENVIRONMENT SPECIFIC DEPENDENCIES diff --git a/Gemfile.lock b/Gemfile.lock index 0b1f7b0..f1bb734 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -348,6 +348,7 @@ gettext_i18n_rails_js (~> 1.2.0) htmltoword (~> 0.5.1) jbuilder (~> 2.6.0) + kaminari (>= 1.0) ledermann-rails-settings (~> 2.4.2) libv8 (~> 3.16) minitest-rails-capybara (~> 2.1.2) diff --git a/app/controllers/plans_controller.rb b/app/controllers/plans_controller.rb index bd0559b..b3d9499 100644 --- a/app/controllers/plans_controller.rb +++ b/app/controllers/plans_controller.rb @@ -7,10 +7,9 @@ def index authorize Plan @plans = current_user.active_plans - @organisationally_or_publicly_visible_by_org = - current_user.org_id.present? ? - Plan.includes(:roles).organisationally_or_publicly_visible_by_org(current_user.org_id).to_a.select{ |p| !p.any_role?(current_user) } : - [] + @paginable = params[:page] + @organisationally_or_publicly_visible = @paginable.nil? ? Plan.organisationally_or_publicly_visible(current_user) : + Plan.organisationally_or_publicly_visible(current_user).page(params[:page]) end # GET /plans/new diff --git a/app/models/plan.rb b/app/models/plan.rb index 05a7b1d..2645278 100644 --- a/app/models/plan.rb +++ b/app/models/plan.rb @@ -62,10 +62,14 @@ scope :publicly_visible, -> { where(:visibility => visibilities[:publicly_visible]).order(:title => :asc) } # Retrieves any plan organisationally or publicly visible for a given org id - scope :organisationally_or_publicly_visible_by_org, -> (org_id) { - joins(:template).where( - visibility: [visibilities[:organisationally_visible], visibilities[:publicly_visible]], - "templates.org_id": org_id).order(:title => :asc) } + scope :organisationally_or_publicly_visible, -> (user) { + Plan.joins(:template) + .where({ + visibility: [visibilities[:organisationally_visible], visibilities[:publicly_visible]], + "templates.org_id": user.org_id}) + .where(['NOT EXISTS (SELECT 1 FROM roles WHERE plan_id = plans.id AND user_id = ?)', user.id]) + .order(:title => :asc) + } ## # Settings for the template has_settings :export, class_name: 'Settings::Template' do |s| diff --git a/app/views/kaminari/_first_page.html.erb b/app/views/kaminari/_first_page.html.erb new file mode 100644 index 0000000..958224e --- /dev/null +++ b/app/views/kaminari/_first_page.html.erb @@ -0,0 +1,11 @@ +<%# Link to the "First" page + - available local variables + url: url to the first page + current_page: a page object for the currently displayed page + total_pages: total number of pages + per_page: number of items to fetch per page + remote: data-remote +-%> + + <%= link_to_unless current_page.first?, _('First'), url, remote: remote %> + diff --git a/app/views/kaminari/_gap.html.erb b/app/views/kaminari/_gap.html.erb new file mode 100644 index 0000000..34b90be --- /dev/null +++ b/app/views/kaminari/_gap.html.erb @@ -0,0 +1,8 @@ +<%# Non-link tag that stands for skipped pages... + - available local variables + current_page: a page object for the currently displayed page + total_pages: total number of pages + per_page: number of items to fetch per page + remote: data-remote +-%> +<%= _('...') %> diff --git a/app/views/kaminari/_last_page.html.erb b/app/views/kaminari/_last_page.html.erb new file mode 100644 index 0000000..a37f875 --- /dev/null +++ b/app/views/kaminari/_last_page.html.erb @@ -0,0 +1,11 @@ +<%# Link to the "Last" page + - available local variables + url: url to the last page + current_page: a page object for the currently displayed page + total_pages: total number of pages + per_page: number of items to fetch per page + remote: data-remote +-%> + + <%= link_to_unless current_page.last?, _('Last'), url, remote: remote %> + diff --git a/app/views/kaminari/_next_page.html.erb b/app/views/kaminari/_next_page.html.erb new file mode 100644 index 0000000..6f1fdbb --- /dev/null +++ b/app/views/kaminari/_next_page.html.erb @@ -0,0 +1,11 @@ +<%# Link to the "Next" page + - available local variables + url: url to the next page + current_page: a page object for the currently displayed page + total_pages: total number of pages + per_page: number of items to fetch per page + remote: data-remote +-%> + + <%= link_to_unless current_page.last?, _('Next'), url, rel: 'next', remote: remote %> + diff --git a/app/views/kaminari/_page.html.erb b/app/views/kaminari/_page.html.erb new file mode 100644 index 0000000..393bfc4 --- /dev/null +++ b/app/views/kaminari/_page.html.erb @@ -0,0 +1,12 @@ +<%# Link showing page number + - available local variables + page: a page object for "this" page + url: url to this page + current_page: a page object for the currently displayed page + total_pages: total number of pages + per_page: number of items to fetch per page + remote: data-remote +-%> + + <%= link_to_unless page.current?, page, url, {remote: remote, rel: page.rel} %> + diff --git a/app/views/kaminari/_paginator.html.erb b/app/views/kaminari/_paginator.html.erb new file mode 100644 index 0000000..fb46ee2 --- /dev/null +++ b/app/views/kaminari/_paginator.html.erb @@ -0,0 +1,25 @@ +<%# The container tag + - available local variables + current_page: a page object for the currently displayed page + 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 -%> + +<% end -%> diff --git a/app/views/kaminari/_prev_page.html.erb b/app/views/kaminari/_prev_page.html.erb new file mode 100644 index 0000000..28602af --- /dev/null +++ b/app/views/kaminari/_prev_page.html.erb @@ -0,0 +1,11 @@ +<%# Link to the "Previous" page + - available local variables + url: url to the previous page + current_page: a page object for the currently displayed page + total_pages: total number of pages + per_page: number of items to fetch per page + remote: data-remote +-%> + + <%= link_to_unless current_page.first?, _('Previous'), url, rel: 'prev', remote: remote %> + diff --git a/app/views/plans/_publicly_visible_org.html.erb b/app/views/plans/_publicly_visible_org.html.erb index 6d83d5d..5104832 100644 --- a/app/views/plans/_publicly_visible_org.html.erb +++ b/app/views/plans/_publicly_visible_org.html.erb @@ -42,5 +42,22 @@ +