Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SAW - Add SSR Owner Filter on Dashboard #615

Merged
merged 8 commits into from
Aug 9, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions app/controllers/dashboard/protocols_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def index
select_options: {
with_status: AVAILABLE_STATUSES.invert,
with_organization: Dashboard::GroupedOrganizations.new(@organizations).collect_grouped_options,
with_owner: build_with_owner_params,
sorted_by: Protocol.options_for_sorted_by
},
persistence_id: false #resets filters on page reload
Expand Down Expand Up @@ -229,6 +230,14 @@ def view_details

private

def build_with_owner_params
service_providers = Identity.joins(:service_providers).where(service_providers: {
organization: Organization.authorized_for_identity(current_user.id) })
.distinct.order("last_name")

service_providers.map{|s| [s.last_name_first, s.id]}
end

def find_protocol
@protocol = Protocol.find(params[:id])
end
Expand Down
4 changes: 4 additions & 0 deletions app/models/identity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ def full_name
"#{first_name.try(:humanize)} #{last_name.try(:humanize)}".lstrip.rstrip
end

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of adding another method on identity, you can do:
full_name.split(' ').reverse.join(', ')

def last_name_first
"#{last_name.try(:humanize)}, #{first_name.try(:humanize)}"
end

# Returns this user's first and last name humanized, with their email.
def display_name
"#{first_name.try(:humanize)} #{last_name.try(:humanize)} (#{email})".lstrip.rstrip
Expand Down
9 changes: 9 additions & 0 deletions app/models/protocol.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ class Protocol < ActiveRecord::Base
:show_archived,
:with_status,
:with_organization,
:with_owner,
:sorted_by
]
)
Expand Down Expand Up @@ -204,6 +205,14 @@ class Protocol < ActiveRecord::Base
where(sub_service_requests: { organization_id: org_id }).distinct
}

scope :with_owner, -> (owner_id) {
return nil if owner_id.reject!(&:blank?) == []
joins(:sub_service_requests).
where(sub_service_requests:
{organization: Organization.joins(:service_providers).
where(service_providers: {identity_id: owner_id})})
}

scope :sorted_by, -> (key) {
case key
when 'id_asc'
Expand Down
2 changes: 2 additions & 0 deletions 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 :with_owner
attr_accessible :sorted_by

scope :latest_for_user, -> (identity_id, limit) {
Expand All @@ -50,6 +51,7 @@ def href
search_query: self.search_query,
with_organization: self.with_organization,
with_status: self.with_status,
with_owner: self.with_owner,
sorted_by: self.sorted_by
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@
= form.select :sorted_by, filterrific.select_options[:sorted_by], {}, class: 'form-control selectpicker sort-select'
- if admin
%hr
.form-group.row
= form.label :with_owner, t(:dashboard)[:protocol_filters][:service_providers], class: 'col-lg-3 control-label'
.col-lg-9
= form.select :with_owner, filterrific.select_options[:with_owner], {}, class: "form-control selectpicker owner-select", multiple: true, data: { none_selected_text: t(:constants)[:prompts][:any] }
.form-group.row.identity-protocols
= form.label :admin_filter, t(:dashboard)[:protocol_filters][:my_protocols], class: 'col-lg-10 control-label'
.col-lg-2
Expand Down
1 change: 1 addition & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,7 @@ en:
header: "Filter Protocols"
archived: "Archived"
status: "Status"
service_providers: "Owner"
my_protocols: "My Protocols"
my_admin_protocols: "My Admin Protocols"
organization: "Organization"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddWithOwnerToProtocolFilter < ActiveRecord::Migration
def change
add_column :protocol_filters, :with_owner, :string
add_index :protocol_filters, :with_owner
end
end
11 changes: 7 additions & 4 deletions 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: 20160727152900) do
ActiveRecord::Schema.define(version: 20160729194811) do

create_table "admin_rates", force: :cascade do |t|
t.integer "line_item_id", limit: 4
Expand Down Expand Up @@ -103,8 +103,8 @@
t.integer "subject_count", limit: 4, default: 1
t.integer "protocol_id", limit: 4
t.boolean "new_with_draft", default: false
t.integer "minimum_visit_count", limit: 4
t.integer "minimum_subject_count", limit: 4
t.integer "minimum_visit_count", limit: 4, default: 0
t.integer "minimum_subject_count", limit: 4, default: 0
end

add_index "arms", ["protocol_id"], name: "index_arms_on_protocol_id", using: :btree
Expand Down Expand Up @@ -525,7 +525,7 @@
t.integer "changed_by_id", limit: 4
end

add_index "past_statuses", ["changed_by_id"], name: "index_past_statuses_on_changed_by_id", using: :btree
add_index "past_statuses", ["changed_by_id"], name: "index_past_statuses_on_changed_by", using: :btree
add_index "past_statuses", ["sub_service_request_id"], name: "index_past_statuses_on_sub_service_request_id", using: :btree

create_table "past_subsidies", force: :cascade do |t|
Expand Down Expand Up @@ -657,9 +657,12 @@
t.datetime "created_at"
t.datetime "updated_at"
t.string "admin_filter", limit: 255
t.string "with_owner", limit: 255
t.string "sorted_by", limit: 255
end

add_index "protocol_filters", ["with_owner"], name: "index_protocol_filters_on_with_owner", using: :btree

create_table "protocols", force: :cascade do |t|
t.string "type", limit: 255
t.integer "next_ssr_id", limit: 4
Expand Down
22 changes: 22 additions & 0 deletions spec/features/dashboard/protocols/index/filters_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,28 @@ def visit_protocols_index_page
end
end

describe "Owner Dropdown" do
it 'should only display protocols with sub service requests that have the specified service provider' do
person = create(:identity, first_name: "Wilson", last_name: "Fisk")
organization1 = create(:organization, name: 'MagikarpLLC')
organization2 = create(:organization, name: 'Union Allied')
create(:service_provider, organization: organization1, identity: user)
create(:service_provider, organization: organization2, identity: user)
create(:service_provider, organization: organization2, identity: person)
protocol1 = create_protocol(archived: false, short_title: 'Magikarp Protocol', organization: organization1)
protocol1 = create_protocol(archived: false, short_title: 'Construction', organization: organization2)

visit_protocols_index_page

wait_for_javascript_to_finish
@page.filter_protocols.select_owner("Fisk, Wilson")
@page.filter_protocols.apply_filter_button.click

expect(@page.search_results).to have_protocols(text: "Construction")
expect(@page.search_results).to have_no_protocols(text: "Magikarp Protocol")
end
end

describe "My Admin Protocols" do
let(:organization) { create(:organization, admin: user, name: "MegaCorp") }

Expand Down
13 changes: 13 additions & 0 deletions spec/support/pages/dashboard/protocols/index_page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class IndexPage < SitePrism::Page
elements :core_options, "div.core-select li"

# these appear if user is an admin
element :owner_select, "div.owner-select button"
elements :owner_options, "div.owner-select li"
element :my_protocols_checkbox, ".identity-protocols input"
element :my_admin_organizations_checkbox, ".admin-protocols input"

Expand All @@ -45,6 +47,17 @@ def select_core(*cores)
page.find("body").click # seems like Capybara page is available in this context
wait_until_core_options_invisible
end

# select an owner (service provider) :owner_select by text
def select_owner(*owners)
owner_select.click
wait_for_owner_options
owners.each do |owner|
owner_options(text: /\A#{owner}\Z/).first.click
end
page.find("body").click
wait_until_owner_options_invisible
end
end

# appears when the save link in the Filter Protocols pane is clicked
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 @@ -9,14 +9,16 @@
select_options: {
with_status: [],
with_organization: [],
sorted_by: "id_asc"
sorted_by: "id_asc",
with_owner: []
},
with_status: [],
search_query: '',
show_archived: 0,
admin_filter: "for_identity #{jug2.id}",
with_organization: false,
sorted_by: "id_asc"
sorted_by: "id_asc",
with_owner: ["#{jug2.id}"]
))
end

Expand Down