diff --git a/app/controllers/concerns/paginable.rb b/app/controllers/concerns/paginable.rb index 7333a0f..dea6bb3 100644 --- a/app/controllers/concerns/paginable.rb +++ b/app/controllers/concerns/paginable.rb @@ -6,13 +6,16 @@ # partial {String} - Represents a path to where the partial view is stored # controller {String} - Represents the name of the controller to handles the pagination # action {String} - Represents the method name within the controller + # path_params {Hash} - A hash of additional URL path parameters (e.g. path_paths = { id: 'foo' } for /paginable/templates/:id/history/:page) # scope {ActiveRecord::Relation} - Represents scope variable # locals {Hash} - A hash objects with any additional local variables to be passed to the partial view - def paginable_renderise(partial: nil, controller: params[:controller], action: params[:action], scope: nil, locals: {}) - raise ArgumentError, 'scope should be an instance of ActiveRecord::Relation class' unless scope.is_a?(ActiveRecord::Relation) - raise ArgumentError, 'locals should be an instance of Hash' unless locals.is_a?(Hash) + def paginable_renderise(partial: nil, controller: params[:controller], action: params[:action], path_params: {}, scope: nil, locals: {}) + raise ArgumentError, 'scope should be an ActiveRecord::Relation object' unless scope.is_a?(ActiveRecord::Relation) + raise ArgumentError, 'path_params should be a Hash object' unless path_params.is_a?(Hash) + raise ArgumentError, 'locals should be a Hash object' unless locals.is_a?(Hash) @paginable_controller = controller @paginable_action = action + @paginable_path_params = path_params refined_scope = refine_query(scope) render(layout: "/layouts/paginable", partial: partial, locals: { controller: controller, @@ -20,6 +23,15 @@ paginable: refined_scope.respond_to?(:total_pages), scope: refined_scope }.merge(locals)) end + # Returns the base url of the paginable route for a given page passed + def paginable_base_url(page = 1) + options = { controller: @paginable_controller || params[:controller], + action: @paginable_action || params[:action], page: page } + if @paginable_path_params.present? + options = @paginable_path_params.merge(options) + end + return url_for(options) + end # Generates an HTML link to sort given a sort field. # sort_field {String} - Represents the column name for a table def paginable_sort_link(sort_field) @@ -39,10 +51,6 @@ end end private - # Returns the base url of the paginable route for a given page passed - def paginable_base_url(page = 1) - return url_for(controller: @paginable_controller || params[:controller], action: @paginable_action || params[:action], page: page) - end # Returns the upcase string (e.g ASC or DESC) if sort_direction param is present in any of the forms 'asc', 'desc', 'ASC', 'DESC' # otherwise returns nil def sort_direction diff --git a/app/controllers/paginable/templates_controller.rb b/app/controllers/paginable/templates_controller.rb index db35139..441bd5d 100644 --- a/app/controllers/paginable/templates_controller.rb +++ b/app/controllers/paginable/templates_controller.rb @@ -75,4 +75,17 @@ partial: 'publicly_visible', scope: Template.includes(:org).where(dmptemplate_id: template_ids.uniq.flatten).valid.published) end + + # GET /paginable/templates/:id/history/:page (AJAX) + # ----------------------------------------------------- + def history + @template = Template.find(params[:id]) + authorize @template + @templates = Template.where(dmptemplate_id: @template.dmptemplate_id) + @current = Template.current(@template.dmptemplate_id) + paginable_renderise( + partial: 'history', + scope: @templates, + locals: { current: @current }) + end end diff --git a/app/models/phase.rb b/app/models/phase.rb index 1beaae0..bc95a68 100644 --- a/app/models/phase.rb +++ b/app/models/phase.rb @@ -38,7 +38,11 @@ # What do they do? do they do it efficiently, and do we need them? - + # Callbacks + after_save do |phase| + # Updates the template.updated_at attribute whenever a phase has been created/updated + phase.template.touch + end ## diff --git a/app/views/layouts/_paginable.html.erb b/app/views/layouts/_paginable.html.erb index c42df21..d50573b 100644 --- a/app/views/layouts/_paginable.html.erb +++ b/app/views/layouts/_paginable.html.erb @@ -34,18 +34,18 @@ <% else %> <%= paginable_search_link(_('View less search results'), 1) %> <% end %> -
| <%= _('Title') %> | -<%= _('Version') %> | -<%= _('Published') %> | -<%= _('Last updated') %> | -<%= _('Actions') %> | -
|---|---|---|---|---|
| - <%= org_template.title%> - <% if org_template == @current && !org_template.published%> - <%=_('Draft')%> - <% end %> - | -- <%= org_template.version %> - | -- <%= (org_template.published? ? _('Yes') : _('No')) %> - | -- <% last_temp_updated = org_template.updated_at %> - <% org_template.phases.each do |phase|%> - <% if org_template.updated_at.to_date < phase.updated_at.to_date %> - <% last_temp_updated = phase.updated_at %> - <% end %> - <% end %> - <%= l last_temp_updated.to_date, formats: :short %> - | -- <%= link_to (org_template == @current ? _('Edit') : _('View')), edit_org_admin_template_path(id: org_template), class: "dmp_table_link"%> - | -
<%= _('This template is new and does not yet have any publication history.') %>
<% end %> diff --git a/app/views/paginable/templates/_history.html.erb b/app/views/paginable/templates/_history.html.erb new file mode 100644 index 0000000..bf54288 --- /dev/null +++ b/app/views/paginable/templates/_history.html.erb @@ -0,0 +1,38 @@ +<%# locals: { controller, action, paginable, scope, current } %> +| <%= _('Title') %> <%= paginable_sort_link('title') %> | +<%= _('Version') %> <%= paginable_sort_link('version') %> | +<%= _('Published') %> <%= paginable_sort_link('published') %> | +<%= _('Last updated') %> <%= paginable_sort_link('updated_at') %> | +<%= _('Actions') %> | +
|---|---|---|---|---|
| + <%= org_template.title%> + <% if org_template == current && !org_template.published%> + <%=_('Draft')%> + <% end %> + | ++ <%= org_template.version %> + | ++ <%= (org_template.published? ? _('Yes') : _('No')) %> + | ++ <%= l org_template.updated_at.to_date, formats: :short %> + | ++ <%= link_to (org_template == current ? _('Edit') : _('View')), edit_org_admin_template_path(id: org_template), class: "dmp_table_link"%> + | +