add template submissions filters
This commit is contained in:
committed by
Pete Matsyburka
parent
c2eb94b571
commit
b86aac86d1
@@ -115,4 +115,21 @@ class ApplicationController < ActionController::Base
|
||||
|
||||
redirect_to request.url.gsub('.co/', '.com/'), allow_other_host: true, status: :moved_permanently
|
||||
end
|
||||
|
||||
def date_range(key)
|
||||
p = params.permit(key => %i[from to]).fetch(key, {})
|
||||
timezone = ActiveSupport::TimeZone[current_account.timezone] || Time.zone
|
||||
start_date = timezone.parse(p[:from]) if p[:from].present?
|
||||
end_date = timezone.parse(p[:to]) if p[:to].present?
|
||||
|
||||
if start_date.present? && end_date.present? && start_date < end_date
|
||||
start_date..end_date
|
||||
elsif start_date.present? && end_date.present? && start_date == end_date
|
||||
start_date.beginning_of_day..end_date.end_of_day
|
||||
elsif start_date.present?
|
||||
start_date..
|
||||
elsif end_date.present?
|
||||
..end_date
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -4,6 +4,7 @@ class TemplatesController < ApplicationController
|
||||
load_and_authorize_resource :template
|
||||
|
||||
before_action :load_base_template, only: %i[new create]
|
||||
helper_method :created_at_range, :completed_at_range
|
||||
|
||||
def show
|
||||
submissions = @template.submissions.accessible_by(current_ability)
|
||||
@@ -14,12 +15,40 @@ class TemplatesController < ApplicationController
|
||||
|
||||
submissions = submissions.pending if params[:status] == 'pending'
|
||||
submissions = submissions.completed if params[:status] == 'completed'
|
||||
submissions = submissions.where(created_at: created_at_range) if created_at_range.present?
|
||||
|
||||
if completed_at_range.present?
|
||||
submissions =
|
||||
Submission.from(submissions.completed
|
||||
.joins(:submitters)
|
||||
.select('submissions.*, max(completed_at) AS last_completed_at')
|
||||
.group('submissions.id'), :submissions)
|
||||
.where(last_completed_at: completed_at_range)
|
||||
end
|
||||
|
||||
if params[:author].present?
|
||||
submissions =
|
||||
submissions.joins(:created_by_user)
|
||||
.where("concat_ws(' ', NULLIF(users.first_name, ''), NULLIF(last_name, '')) = ?", params[:author])
|
||||
end
|
||||
|
||||
@pagy, @submissions = pagy(submissions.preload(submitters: :start_form_submission_events).order(id: :desc))
|
||||
rescue ActiveRecord::RecordNotFound
|
||||
redirect_to root_path
|
||||
end
|
||||
|
||||
def filter
|
||||
return unless params[:filter] == 'author'
|
||||
|
||||
submissions = @template.submissions.accessible_by(current_ability)
|
||||
submissions = submissions.active if @template.archived_at.blank?
|
||||
@creator_names = submissions.includes(:created_by_user)
|
||||
.filter_map(&:created_by_user)
|
||||
.map(&:full_name)
|
||||
.uniq
|
||||
.sort
|
||||
end
|
||||
|
||||
def new
|
||||
@template.name = "#{@base_template.name} (#{I18n.t('clone')})" if @base_template
|
||||
end
|
||||
@@ -147,4 +176,12 @@ class TemplatesController < ApplicationController
|
||||
|
||||
@base_template = Template.accessible_by(current_ability).find_by(id: params[:base_template_id])
|
||||
end
|
||||
|
||||
def created_at_range
|
||||
@created_at_range ||= date_range(:created_at)
|
||||
end
|
||||
|
||||
def completed_at_range
|
||||
@completed_at_range ||= date_range(:completed_at)
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user