Skip to content

Commit

Permalink
Merge pull request #577 from sparc-request/kg-add_sorting_to_protocol…
Browse files Browse the repository at this point in the history
…s_list

KG - Add 'Sort By' to Protocol Filter
  • Loading branch information
jleonardw9 committed Aug 3, 2016
2 parents 8724740 + 94d20f1 commit 381508f
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 8 deletions.
5 changes: 3 additions & 2 deletions app/controllers/dashboard/protocols_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def index
admin_orgs = @user.authorized_admin_organizations
@admin = !admin_orgs.empty?

default_filter_params = { show_archived: 0 }
default_filter_params = { show_archived: 0, sorted_by: 'id_asc' }

# if we are an admin we want to default to admin organizations
if @admin
Expand All @@ -49,7 +49,8 @@ def index
default_filter_params: default_filter_params,
select_options: {
with_status: AVAILABLE_STATUSES.invert,
with_organization: Dashboard::GroupedOrganizations.new(@organizations).collect_grouped_options
with_organization: Dashboard::GroupedOrganizations.new(@organizations).collect_grouped_options,
sorted_by: Protocol.options_for_sorted_by
},
persistence_id: false #resets filters on page reload
) || return
Expand Down
33 changes: 31 additions & 2 deletions app/models/protocol.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,14 @@ class Protocol < ActiveRecord::Base
}

filterrific(
default_filter_params: { show_archived: 0 },
default_filter_params: { show_archived: 0, sorted_by: 'asc' },
available_filters: [
:search_query,
:admin_filter,
:show_archived,
:with_status,
:with_organization
:with_organization,
:sorted_by
]
)

Expand Down Expand Up @@ -203,6 +204,34 @@ class Protocol < ActiveRecord::Base
where(sub_service_requests: { organization_id: org_id }).distinct
}

scope :sorted_by, -> (key) {
case key
when 'id_asc'
order(id: :asc)
when 'id_desc'
order(id: :desc)
when 'short_title_asc'
order('TRIM(REPLACE(short_title, CHAR(9), " ")) ASC')
when 'short_title_desc'
order('TRIM(REPLACE(short_title, CHAR(9), " ")) DESC')
when 'pi_asc'
joins(project_roles: :identity).where(project_roles: { role: 'primary-pi' }).order('.identities.first_name ASC')
when 'pi_desc'
joins(project_roles: :identity).where(project_roles: { role: 'primary-pi' }).order('.identities.first_name DESC')
end
}

def self.options_for_sorted_by
[
['ID (0-9)', 'id_asc'],
['ID (9-0)', 'id_desc'],
['Short Title (A-Z)', 'short_title_asc'],
['Short Title (Z-A)', 'short_title_desc'],
['PI (A-Z)', 'pi_asc'],
['PI (Z-A)', 'pi_desc']
]
end

def is_study?
self.type == 'Study'
end
Expand Down
4 changes: 3 additions & 1 deletion app/models/protocol_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class ProtocolFilter < ActiveRecord::Base
attr_accessible :search_query
attr_accessible :with_organization
attr_accessible :with_status
attr_accessible :sorted_by

scope :latest_for_user, -> (identity_id, limit) {
where(identity_id: identity_id).
Expand All @@ -48,7 +49,8 @@ def href
admin_filter: self.admin_filter,
search_query: self.search_query,
with_organization: self.with_organization,
with_status: self.with_status
with_status: self.with_status,
sorted_by: self.sorted_by
}
)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@
= form.label :with_organization, t(:dashboard)[:protocol_filters][:organization], class: 'col-lg-3 control-label'
.col-lg-9.drop-underneath
= form.select :with_organization, filterrific.select_options[:with_organization], {}, class: "form-control selectpicker core-select", multiple: true, data: { none_selected_text: t(:constants)[:prompts][:any] }
.form-group.row
= form.label :sorted_by, t(:dashboard)[:protocol_filters][:sort_by], class: 'col-lg-3 control-label'
.col-lg-9
= form.select :sorted_by, filterrific.select_options[:sorted_by], {}, class: 'form-control selectpicker sort-select'
- if admin
%hr
.form-group.row.identity-protocols
Expand Down
1 change: 1 addition & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,7 @@ en:
my_protocols: "My Protocols"
my_admin_protocols: "My Admin Protocols"
organization: "Organization"
sort_by: "Sort By"
filter: "Filter"
saved_filters:
header: "Recently Saved Filters"
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20160720151213_add_sorted_by_to_protocol_filter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddSortedByToProtocolFilter < ActiveRecord::Migration
def change
add_column :protocol_filters, :sorted_by, :string
end
end
9 changes: 8 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20160719150112) do
ActiveRecord::Schema.define(version: 20160720151213) do

create_table "admin_rates", force: :cascade do |t|
t.integer "line_item_id", limit: 4
Expand Down Expand Up @@ -184,6 +184,12 @@
add_index "charges", ["service_id"], name: "index_charges_on_service_id", using: :btree
add_index "charges", ["service_request_id"], name: "index_charges_on_service_request_id", using: :btree

create_table "click_counters", force: :cascade do |t|
t.integer "click_count", limit: 4
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "clinical_providers", force: :cascade do |t|
t.integer "identity_id", limit: 4
t.integer "organization_id", limit: 4
Expand Down Expand Up @@ -657,6 +663,7 @@
t.datetime "created_at"
t.datetime "updated_at"
t.string "admin_filter", limit: 255
t.string "sorted_by", limit: 255
end

create_table "protocols", force: :cascade do |t|
Expand Down
6 changes: 4 additions & 2 deletions spec/views/dashboard/protocols/index.html.haml_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
assign(:filterrific, double('filterrific',
select_options: {
with_status: [],
with_organization: []
with_organization: [],
sorted_by: "id_asc"
},
with_status: [],
search_query: '',
show_archived: 0,
admin_filter: "for_identity #{jug2.id}",
with_organization: false
with_organization: false,
sorted_by: "id_asc"
))
end

Expand Down

0 comments on commit 381508f

Please sign in to comment.