diff --git a/app/controllers/guidances_controller.rb b/app/controllers/guidances_controller.rb index 90a7051..c21372c 100644 --- a/app/controllers/guidances_controller.rb +++ b/app/controllers/guidances_controller.rb @@ -6,8 +6,8 @@ # GET /guidances def admin_index authorize Guidance - @guidances = policy_scope(Guidance) - @guidance_groups = GuidanceGroup.where(org_id: current_user.org_id) + @guidances = Guidance.by_org(current_user.org).includes(:guidance_group, :themes).page(1) + @guidance_groups = GuidanceGroup.by_org(current_user.org).page(1) end def admin_new diff --git a/app/controllers/paginable/guidance_groups_controller.rb b/app/controllers/paginable/guidance_groups_controller.rb new file mode 100644 index 0000000..a6400d1 --- /dev/null +++ b/app/controllers/paginable/guidance_groups_controller.rb @@ -0,0 +1,11 @@ +module Paginable + class GuidanceGroupsController < ApplicationController + include Paginable + # /paginable/guidance_groups/index/:page + def index + authorize(Guidance) + paginable_renderise(partial: 'index', + scope: GuidanceGroup.by_org(current_user.org)) + end + end +end \ No newline at end of file diff --git a/app/controllers/paginable/guidances_controller.rb b/app/controllers/paginable/guidances_controller.rb new file mode 100644 index 0000000..5887df5 --- /dev/null +++ b/app/controllers/paginable/guidances_controller.rb @@ -0,0 +1,11 @@ +module Paginable + class GuidancesController < ApplicationController + include Paginable + # /paginable/guidances/index/:page + def index + authorize(Guidance) + paginable_renderise(partial: 'index', + scope: Guidance.by_org(current_user.org).includes(:guidance_group, :themes)) + end + end +end \ No newline at end of file diff --git a/app/controllers/paginable/plans_controller.rb b/app/controllers/paginable/plans_controller.rb index 4df0a06..d691dda 100644 --- a/app/controllers/paginable/plans_controller.rb +++ b/app/controllers/paginable/plans_controller.rb @@ -11,4 +11,15 @@ paginable_renderise(partial: 'organisationally_or_publicly_visible', scope: Plan.organisationally_or_publicly_visible(current_user)) end + # GET /paginable/plans/publicly_visible/:page + def publicly_visible + paginable_renderise(partial: 'publicly_visible', + scope: Plan.publicly_visible) + end + # GET /paginable/plans/org_admin/:page + def org_admin + raise Pundit::NotAuthorizedError unless current_user.present? && current_user.can_org_admin? + paginable_renderise(partial: 'org_admin', + scope: current_user.org.plans) + end end \ No newline at end of file diff --git a/app/controllers/paginable/templates_controller.rb b/app/controllers/paginable/templates_controller.rb index a5f69f7..db35139 100644 --- a/app/controllers/paginable/templates_controller.rb +++ b/app/controllers/paginable/templates_controller.rb @@ -2,7 +2,7 @@ include Paginable include TemplateFilter - # GET /org_admin/templates/all/:page (AJAX) + # GET /paginable/templates/all/:page (AJAX) # ----------------------------------------------------- def all raise Pundit::NotAuthorizedError unless Paginable::TemplatePolicy.new(current_user).all? @@ -23,7 +23,7 @@ scopes: hash[:scopes]} end - # GET /org_admin/templates/funders/:page (AJAX) + # GET /paginable/templates/funders/:page (AJAX) # ----------------------------------------------------- def funders raise Pundit::NotAuthorizedError unless Paginable::TemplatePolicy.new(current_user).funders? @@ -44,7 +44,7 @@ scopes: hash[:scopes] } end - # GET /org_admin/templates/orgs/:page (AJAX) + # GET /paginable/templates/orgs/:page (AJAX) # ----------------------------------------------------- def orgs raise Pundit::NotAuthorizedError unless Paginable::TemplatePolicy.new(current_user).orgs? @@ -64,4 +64,15 @@ published: published, scopes: hash[:scopes]} end + + # GET /paginable/templates/publicly_visible/:page (AJAX) + # ----------------------------------------------------- + def publicly_visible + template_ids = Template.families(Org.funder.pluck(:id)).publicly_visible.pluck(:dmptemplate_id) << + Template.where(is_default: true).valid.published.pluck(:dmptemplate_id) + + paginable_renderise( + partial: 'publicly_visible', + scope: Template.includes(:org).where(dmptemplate_id: template_ids.uniq.flatten).valid.published) + end end diff --git a/app/controllers/public_pages_controller.rb b/app/controllers/public_pages_controller.rb index fe8c78a..3077b53 100644 --- a/app/controllers/public_pages_controller.rb +++ b/app/controllers/public_pages_controller.rb @@ -4,13 +4,9 @@ # GET template_index # ----------------------------------------------------- def template_index - template_ids = Template.where(org_id: Org.funder.pluck(:id), visibility: Template.visibilities[:publicly_visible]).valid.pluck(:dmptemplate_id).uniq << Template.where(is_default: true, published: true).pluck(:dmptemplate_id) - @templates = [] - template_ids.flatten.each do |tid| - t = Template.live(tid) - @templates << t unless t.nil? - end - @templates = @templates.sort{|x,y| x.title <=> y.title } if @templates.count > 1 + template_ids = Template.families(Org.funder.pluck(:id)).publicly_visible.pluck(:dmptemplate_id) << + Template.where(is_default: true).valid.published.pluck(:dmptemplate_id) + @templates = Template.includes(:org).where(dmptemplate_id: template_ids.uniq.flatten).valid.published.order(title: :asc).page(1) end # GET template_export/:id @@ -119,7 +115,6 @@ # GET /plans_index # ------------------------------------------------------------------------------------ def plan_index - @plans = Plan.publicly_visible - @plans = @plans.sort{|x,y| x.title <=> y.title } if @plans.count > 1 + @plans = Plan.publicly_visible.order(:title => :asc).page(1) end end diff --git a/app/models/guidance.rb b/app/models/guidance.rb index 24c1cc0..4c5bb5c 100644 --- a/app/models/guidance.rb +++ b/app/models/guidance.rb @@ -27,7 +27,15 @@ validates :text, presence: {message: _("can't be blank")} + # Retrieves every guidance associated to an org + scope :by_org, -> (org) { + joins(:guidance_group).merge(GuidanceGroup.by_org(org)) + } + scope :search, -> (term) { + search_pattern = "%#{term}%" + joins(:guidance_group).where("guidances.text LIKE ? OR guidance_groups.name LIKE ?", search_pattern, search_pattern) + } ## # Determine if a guidance is in a group which belongs to a specified organisation # @@ -43,20 +51,6 @@ end ## - # returns all guidance that belongs to a specified organisation - # - # @param org_id [Integer] the integer id for an organisation - # @return [Array] list of guidance - def self.by_org(org_id) - org_guidance = [] - # TODO: re-write below querry when guidance_in_group removed from model - Org.find_by(id: org_id).guidance_groups.each do |group| - org_guidance += Guidance.where(guidance_group_id: group.id) - end - return org_guidance - end - - ## # returns all templates belgonging to a specified guidance group # # @param guidance_group [Integer] the integer id for an guidance_group diff --git a/app/models/guidance_group.rb b/app/models/guidance_group.rb index 8974a37..b898ded 100644 --- a/app/models/guidance_group.rb +++ b/app/models/guidance_group.rb @@ -22,10 +22,14 @@ # # What do they do? do they do it efficiently, and do we need them? - - - - + # Retrieves every guidance group associated to an org + scope :by_org, -> (org) { + where(org_id: org.id) + } + scope :search, -> (term) { + search_pattern = "%#{term}%" + where("name LIKE ?", search_pattern) + } ## # Converts the current guidance group to a string containing the display name. diff --git a/app/models/plan.rb b/app/models/plan.rb index 3c0cec8..9aa7c51 100644 --- a/app/models/plan.rb +++ b/app/models/plan.rb @@ -52,7 +52,7 @@ # Scope queries # Note that in ActiveRecord::Enum the mappings are exposed through a class method with the pluralized attribute name (e.g visibilities rather than visibility) - scope :publicly_visible, -> { where(:visibility => visibilities[:publicly_visible]).order(:title => :asc) } + scope :publicly_visible, -> { includes(:template).where(:visibility => visibilities[:publicly_visible]) } # Retrieves any plan in which the user has an active role and it is not a reviewer scope :active, -> (user) { diff --git a/app/models/template.rb b/app/models/template.rb index c14d83f..0527bf2 100644 --- a/app/models/template.rb +++ b/app/models/template.rb @@ -47,7 +47,7 @@ Template.where(templates: { is_default: is_default }).valid().published() } - scope :publicly_visible, -> { where(:visibility => Template.visibilities[:publicly_visible]).order(:title => :asc) } + scope :publicly_visible, -> { where(:visibility => Template.visibilities[:publicly_visible]) } # Retrieves template with distinct dmptemplate_id that are valid (e.g. migrated false) and customization_of is nil. Note, # if organisation ids are passed, the query will filter only those distinct dmptemplate_ids for those organisations diff --git a/app/policies/guidance_policy.rb b/app/policies/guidance_policy.rb index 3bcc0d1..09cf419 100644 --- a/app/policies/guidance_policy.rb +++ b/app/policies/guidance_policy.rb @@ -19,6 +19,10 @@ user.can_modify_guidance? && guidance.in_group_belonging_to?(user.org_id) end + def index? + admin_index? + end + def admin_index? user.can_modify_guidance? end @@ -58,10 +62,4 @@ def update_questions? user.can_modify_guidance? end - - class Scope < Scope - def resolve - scope = Guidance.includes(:guidance_group, :themes).by_org(user.org_id) - end - end end \ No newline at end of file diff --git a/app/views/guidances/admin_index.html.erb b/app/views/guidances/admin_index.html.erb index 93139ef..3cecb9b 100644 --- a/app/views/guidances/admin_index.html.erb +++ b/app/views/guidances/admin_index.html.erb @@ -13,75 +13,11 @@

<%= _('Guidance group list') %>

- <% if @guidance_groups.length > 0 then%> -
- - - <% if @guidance_groups.length > TABLE_FILTER_MIN_ROWS %> - - - - <% end %> - - - - - - - - - - <% !@guidance_groups.each do |guidance_gr| %> - - - - - - - - <% end %> - -
- <%= render(partial: "shared/table_filter", - locals: {path: admin_index_guidance_path(current_user.org_id), placeholder: _('Filter guidance groups')}) %> -
<%= _('Name') %><%= _('Status') %><%= _('Optional subset') %><%= _('Last updated') %><%= _('Actions') %>
- <%= guidance_gr.name %> - - <% if guidance_gr.published.nil? || guidance_gr.published == false then%> - <%= _('Unpublished')%> - <% else %> - <%= _('Published')%> - <% end %> - - <% if guidance_gr.optional_subset.nil? || guidance_gr.optional_subset == false then%> - <%= _('No')%> - <% else %> - <%= _('Yes')%> - <% end %> - - <%= l guidance_gr.updated_at.to_date, formats: :short %> - - -
-
- <%end%> - + <%= paginable_renderise( + partial: '/paginable/guidance_groups/index', + controller: 'paginable/guidance_groups', + action: 'index', + scope: @guidance_groups) %>
<%= _('Create a guidance group') %>
@@ -96,91 +32,11 @@

- <% if @guidances.length > 0 then%> -
- - - <% if @guidances.length > TABLE_FILTER_MIN_ROWS %> - - <%= render(partial: "shared/table_filter", - locals: {path: admin_index_guidance_path(current_user.org_id), placeholder: _('Filter guidance')}) %> - - <% end %> - - - - - - - - - - - <% @guidances.each do |guidance| %> - <% if guidance.in_group_belonging_to?(current_user.org_id) then %> - - - <% if guidance.themes.present? then %> - - <% else %> - - <% end %> - <% if guidance.guidance_group.present? then %> - - <% else %> - - <% end %> - - - - - <% end %> - <% end %> - -
<%= _('Text') %><%= _('Themes') %><%= _('Guidance group') %><%= _('Status') %><%= _('Last updated') %><%= _('Actions') %>
- <%= guidance.text.html_safe%> - - <% guidance.themes.each do |th| %> - <%= th.title %> - <% end %> - - - - - <%= guidance.guidance_group.name %> - - - - - <% if guidance.published.nil? || guidance.published == false then%> - <%= _('Unpublished')%> - <% else %> - <%= _('Published')%> - <% end %> - - <%= l guidance.updated_at.to_date, formats: :short %> - - -
-
- <% end %> + <%= paginable_renderise( + partial: '/paginable/guidances/index', + controller: 'paginable/guidances', + action: 'index', + scope: @guidances) %>
<%= _('Create guidance') %> diff --git a/app/views/layouts/_paginable.html.erb b/app/views/layouts/_paginable.html.erb index dc5ab8b..c42df21 100644 --- a/app/views/layouts/_paginable.html.erb +++ b/app/views/layouts/_paginable.html.erb @@ -28,14 +28,14 @@
<% if total > Kaminari.config.default_per_page %> <% if searchable? %> - <% if paginable %>
    + <% if paginable %>
  • <%= paginable_search_link(_('View all search results'), 'ALL') %>
  • + <% else %> + <%= paginable_search_link(_('View less search results'), 1) %> + <% end %>
  • <%= link_to(_('Clear search results'), url_for(controller: controller, action: action, page: 1), { 'data-remote': true, class: 'clear' }) %>
- <% else %> - <%= paginable_search_link(_('View less search results'), 1) %> - <% end %> <% else %> <% if paginable %> <%= link_to(_('View all'), url_for(controller: controller, action: action, page: 'ALL'), { 'data-remote': true }) %> diff --git a/app/views/org_admin/plans/index.html.erb b/app/views/org_admin/plans/index.html.erb index 6f3cdc5..ebe589e 100644 --- a/app/views/org_admin/plans/index.html.erb +++ b/app/views/org_admin/plans/index.html.erb @@ -33,47 +33,13 @@
<% end %> - <% if @plans.length > 0 %> <%= link_to _('Download plans'), org_admin_download_plans_path(format: :csv), target: '_blank', class: 'btn btn-default pull-right' %> -
- - - <% if @plans.length > TABLE_FILTER_MIN_ROWS %> - - - - <% end %> - - - - - - - - - - - <% @plans.each do |plan| %> - - - - - - - - - <% end %> - -
- <%= render(partial: "shared/table_filter", - locals: { placeholder: _('Filter plans')}) %> -
<%= _('Project Title') %><%= _('Template') %><%= _('Organisation') %><%= _('Owner') %><%= _('Updated') %><%= _('Visibility') %>
- <%= link_to "#{plan.title.length > 60 ? "#{plan.title[0..59]} ..." : plan.title}", plan_path(plan) %> - <%= plan.template.title %><%= plan.users.first.org.name %><%= plan.users.first.name(false) %><%= l(plan.latest_update.to_date, formats: :short) %> - <%= plan.visibility === 'is_test' ? _('Test') : raw(display_visibility(plan.visibility)) %> -
-
+ <%= paginable_renderise( + partial: '/paginable/plans/org_admin', + controller: 'paginable/plans', + action: 'org_admin', + scope: @plans) %> <% end %> diff --git a/app/views/paginable/guidance_groups/_index.html.erb b/app/views/paginable/guidance_groups/_index.html.erb new file mode 100644 index 0000000..9f8e918 --- /dev/null +++ b/app/views/paginable/guidance_groups/_index.html.erb @@ -0,0 +1,57 @@ +
+ + + + + + + + + + + + <% scope.each do |guidance_gr| %> + + + + + + + + <% end %> + +
<%= _('Name') %><%= _('Status') %> <%= paginable_sort_link('published') %><%= _('Optional subset') %> <%= paginable_sort_link('optional_subset') %><%= _('Last updated') %> <%= paginable_sort_link('updated_at') %><%= _('Actions') %>
<%= guidance_gr.name %> + <% if guidance_gr.published.nil? || guidance_gr.published == false %> + <%= _('Unpublished')%> + <% else %> + <%= _('Published')%> + <% end %> + + <% if guidance_gr.optional_subset.nil? || guidance_gr.optional_subset == false %> + <%= _('No')%> + <% else %> + <%= _('Yes')%> + <% end %> + <%= l guidance_gr.updated_at.to_date, formats: :short %> + +
+
\ No newline at end of file diff --git a/app/views/paginable/guidances/_index.html.erb b/app/views/paginable/guidances/_index.html.erb new file mode 100644 index 0000000..98761b9 --- /dev/null +++ b/app/views/paginable/guidances/_index.html.erb @@ -0,0 +1,71 @@ +
+ + + + + + + + + + + + + <% scope.each do |guidance| %> + <% if guidance.in_group_belonging_to?(current_user.org_id) %> + + + <% if guidance.themes.present? %> + + <% else %> + + <% end %> + <% if guidance.guidance_group.present? %> + + <% else %> + + <% end %> + + + + + <% end %> + <% end %> + +
<%= _('Text') %> <%= paginable_sort_link('guidances.text') %><%= _('Themes') %><%= _('Guidance group') %> <%= paginable_sort_link('guidance_groups.name') %><%= _('Status') %> <%= paginable_sort_link('guidances.published') %><%= _('Last updated') %> <%= paginable_sort_link('guidances.updated_at') %><%= _('Actions') %>
<%= guidance.text.html_safe %> + <% guidance.themes.each do |th| %> + <%= th.title %> + <% end %> + -<%= guidance.guidance_group.name %>- + <% if guidance.published.nil? || guidance.published == false %> + <%= _('Unpublished')%> + <% else %> + <%= _('Published')%> + <% end %> + + <%= l guidance.updated_at.to_date, formats: :short %> + + +
+
\ No newline at end of file diff --git a/app/views/paginable/plans/_org_admin.html.erb b/app/views/paginable/plans/_org_admin.html.erb new file mode 100644 index 0000000..79d27af --- /dev/null +++ b/app/views/paginable/plans/_org_admin.html.erb @@ -0,0 +1,30 @@ +
+ + + + + + + + + + + + + <% scope.each do |plan| %> + + + + + + + + + <% end %> + +
<%= _('Project Title') %> <%= paginable_sort_link('plans.title') %><%= _('Template') %> <%= paginable_sort_link('templates.title') %><%= _('Organisation') %><%= _('Owner') %><%= _('Updated') %> <%= paginable_sort_link('plans.updated_at') %><%= _('Visibility') %>
+ <%= link_to "#{plan.title.length > 60 ? "#{plan.title[0..59]} ..." : plan.title}", plan_path(plan) %> + <%= plan.template.title %><%= plan.users.first.org.name %><%= plan.users.first.name(false) %><%= l(plan.latest_update.to_date, formats: :short) %> + <%= plan.visibility === 'is_test' ? _('Test') : raw(display_visibility(plan.visibility)) %> +
+
\ No newline at end of file diff --git a/app/views/paginable/plans/_organisationally_or_publicly_visible.html.erb b/app/views/paginable/plans/_organisationally_or_publicly_visible.html.erb index 1aacad8..a46c18f 100644 --- a/app/views/paginable/plans/_organisationally_or_publicly_visible.html.erb +++ b/app/views/paginable/plans/_organisationally_or_publicly_visible.html.erb @@ -7,7 +7,7 @@
- +
diff --git a/app/views/paginable/plans/_privately_visible.html.erb b/app/views/paginable/plans/_privately_visible.html.erb index fbf9789..eab6346 100644 --- a/app/views/paginable/plans/_privately_visible.html.erb +++ b/app/views/paginable/plans/_privately_visible.html.erb @@ -1,5 +1,5 @@
-
<%= _('Project Title') %> <%= paginable_sort_link('plans.title') %>
+
diff --git a/app/views/paginable/plans/_publicly_visible.html.erb b/app/views/paginable/plans/_publicly_visible.html.erb new file mode 100644 index 0000000..fe147b9 --- /dev/null +++ b/app/views/paginable/plans/_publicly_visible.html.erb @@ -0,0 +1,26 @@ +
+
<%= _('Project Title') %> <%= paginable_sort_link('plans.title') %>
+ + + + + + + + + + + <% scope.each do |plan| %> + + + + + + + + <% end %> + +
<%= _('Project Title') %> <%= paginable_sort_link('plans.title') %><%= _('Template') %> <%= paginable_sort_link('templates.title') %><%= _('Institution') %><%= _('Owner') %><%= _('Download') %>
<%= plan.title %><%= plan.template.title %><%= (plan.owner.nil? || plan.owner.org.nil? ? _('Not Applicable') : plan.owner.org.name) %><%= (plan.owner.nil? ? _('Unknown') : plan.owner.name(false)) %> + <%= link_to _('PDF'), plan_export_path(plan, format: :pdf), class: "dmp_table_link", target: '_blank' %> +
+
\ No newline at end of file diff --git a/app/views/paginable/templates/_all.html.erb b/app/views/paginable/templates/_all.html.erb index 1a4dabd..66a2ab1 100644 --- a/app/views/paginable/templates/_all.html.erb +++ b/app/views/paginable/templates/_all.html.erb @@ -1,7 +1,7 @@ <% # locals: templates, current_org %>
- +
<% if scopes.present? %>