mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2025-09-10 23:25:31 +08:00
Add sorting and search to forms [SCI-11432]
This commit is contained in:
parent
6c007705b1
commit
61976c7704
4 changed files with 54 additions and 4 deletions
|
@ -6,6 +6,10 @@ class Form < ApplicationRecord
|
|||
include ArchivableModel
|
||||
include PermissionCheckableModel
|
||||
include Assignable
|
||||
include SearchableModel
|
||||
include SearchableByNameModel
|
||||
|
||||
SEARCHABLE_ATTRIBUTES = ['forms.name'].freeze
|
||||
|
||||
belongs_to :team
|
||||
belongs_to :parent, class_name: 'Form', optional: true
|
||||
|
|
|
@ -6,7 +6,7 @@ module Lists
|
|||
include Rails.application.routes.url_helpers
|
||||
|
||||
attributes :id, :name, :published_on, :published_by, :updated_at, :urls, :code, :top_level_assignable, :hidden,
|
||||
:team, :default_public_user_role_id, :permissions, :assigned_users
|
||||
:team, :default_public_user_role_id, :permissions, :assigned_users, :versions, :used_in_protocols
|
||||
|
||||
def published_by
|
||||
object.published_by&.full_name
|
||||
|
@ -28,6 +28,14 @@ module Lists
|
|||
object.hidden?
|
||||
end
|
||||
|
||||
def used_in_protocols
|
||||
object.form_responses.count
|
||||
end
|
||||
|
||||
def versions
|
||||
I18n.t("forms.#{object.published? ? 'published' : 'draft'}")
|
||||
end
|
||||
|
||||
def team
|
||||
object.team.name
|
||||
end
|
||||
|
|
|
@ -8,7 +8,19 @@ module Lists
|
|||
end
|
||||
|
||||
def fetch_records
|
||||
@records = Form.where(team: @team).readable_by_user(@user)
|
||||
@records =
|
||||
Form.includes(:team, user_assignments: %i(user user_role))
|
||||
.joins(:user_assignments)
|
||||
.left_outer_joins(:form_responses)
|
||||
.joins(
|
||||
'LEFT OUTER JOIN users AS publishers ' \
|
||||
'ON forms.published_by_id = publishers.id'
|
||||
).select(
|
||||
'forms.* AS forms',
|
||||
'publishers.full_name AS published_by_user',
|
||||
'COUNT(DISTINCT form_responses.id) AS used_in_protocols_count',
|
||||
'COUNT(DISTINCT user_assignments.id) AS user_assignment_count'
|
||||
).where(team: @team).readable_by_user(@user).group('forms.id', 'publishers.full_name')
|
||||
|
||||
view_mode = @params[:view_mode] || 'active'
|
||||
|
||||
|
@ -16,8 +28,32 @@ module Lists
|
|||
@records = @records.active if view_mode == 'active'
|
||||
end
|
||||
|
||||
def filter_records; end
|
||||
def filter_records
|
||||
return unless @params[:search]
|
||||
|
||||
def sort_records; end
|
||||
@records = @records.where_attributes_like(
|
||||
['forms.name'],
|
||||
@params[:search]
|
||||
)
|
||||
end
|
||||
|
||||
def sort_records
|
||||
return unless @params[:order]
|
||||
|
||||
super
|
||||
end
|
||||
|
||||
def sortable_columns
|
||||
@sortable_columns ||= {
|
||||
name: 'forms.name',
|
||||
id: 'forms.id',
|
||||
updated_at: 'forms.updated_at',
|
||||
assigned_users: 'user_assignment_count',
|
||||
used_in_protocols: 'used_in_protocols_count',
|
||||
versions: 'forms.published_on', # temporary until proper versioning is implemented
|
||||
published_by: 'published_by_user',
|
||||
published_on: 'forms.published_on'
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1056,6 +1056,8 @@ en:
|
|||
add_user_generic_error: "An error occurred. "
|
||||
can_add_user_to_project: "Can not add user to the project."
|
||||
forms:
|
||||
published: "Published"
|
||||
draft: "Draft"
|
||||
default_name: "Untitled form"
|
||||
restored:
|
||||
success_flash: "<strong>%{number}</strong> form(s) successfully restored."
|
||||
|
|
Loading…
Add table
Reference in a new issue