Skip to content

Commit

Permalink
Merge pull request #204 from sparc-request/protal-protocol-controller…
Browse files Browse the repository at this point in the history
…-spec

Refactor: Portal::ProtocolController#index
  • Loading branch information
ctt3 committed Nov 10, 2015
2 parents 0e57a67 + a04bb9e commit f946718
Show file tree
Hide file tree
Showing 8 changed files with 184 additions and 49 deletions.
29 changes: 5 additions & 24 deletions app/controllers/portal/protocols_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,33 +22,15 @@ class Portal::ProtocolsController < Portal::BaseController

respond_to :html, :json, :xlsx

before_filter :find_protocol, :only => [:show, :view_full_calendar, :update_from_fulfillment, :edit, :update, :update_protocol_type]
before_filter :protocol_authorizer_view, :only => [:show, :view_full_calendar]
before_filter :protocol_authorizer_edit, :only => [:update_from_fulfillment, :edit, :update, :update_protocol_type]
before_filter :find_protocol, only: [:show, :view_full_calendar, :update_from_fulfillment, :edit, :update, :update_protocol_type]
before_filter :protocol_authorizer_view, only: [:show, :view_full_calendar]
before_filter :protocol_authorizer_edit, only: [:update_from_fulfillment, :edit, :update, :update_protocol_type]

def index
@protocols = []
include_archived = (params[:include_archived] == "true")

@user.protocols.each do |protocol|
if protocol.project_roles.find_by_identity_id(@user.id).project_rights != 'none'
@protocols << protocol if include_archived || !protocol.archived
end
end
@protocols = @protocols.sort_by { |pr| (pr.id || '0000') + pr.id }.reverse
@notifications = @user.user_notifications
#@projects = Project.remove_projects_due_to_permission(@projects, @user)

# params[:default_project] = '0f6a4d750fd369ff4ae409373000ba69'
if params[:default_protocol] && @protocols.map(&:id).include?(params[:default_protocol].to_i)
protocol = @protocols.select{ |p| p.id == params[:default_protocol].to_i}[0]
@protocols.delete(protocol)
@protocols.insert(0, protocol)
end
@protocols = Portal::ProtocolFinder.new(current_user, params).protocols

respond_to do |format|
format.js
format.html
format.js { render }
end
end

Expand Down Expand Up @@ -229,7 +211,6 @@ def remove_arm
render 'portal/service_requests/add_per_patient_per_visit_visit'
end


private

def find_protocol
Expand Down
2 changes: 2 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ class Application < Rails::Application

# /lib path
config.autoload_paths += Dir[Rails.root.join('lib')]
# /lib/portal path
config.autoload_paths += Dir[Rails.root.join('lib/portal')]

# /app/jobs path
config.paths.add File.join('app', 'jobs'), glob: File.join('**', '*.rb')
Expand Down
60 changes: 60 additions & 0 deletions lib/portal/protocol_finder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
module Portal

class ProtocolFinder

def initialize(identity, params)
@identity = identity
@params = params
@protocols = Array.new
end

def protocols
protocols = find_identity_protocols

promote_default_protocol if default_protocol_present?

protocols
end

private

def promote_default_protocol
default_protocol = @identity_protocols.
select { |protocol| protocol.id == @params[:default_protocol].to_i }.
first

if promoted_protocol = @identity_protocols.delete(default_protocol)
@identity_protocols.unshift promoted_protocol
end
end

def default_protocol_present?
@params[:default_protocol].present? &&
@identity_protocols.any? &&
@identity_protocols.
map(&:id).
include?(@params[:default_protocol].to_i)
end

def find_identity_protocols
@identity_protocols ||= Protocol.
where(archived: include_archived?).
joins(:project_roles).
where('project_roles.identity_id = ?', @identity.id).
where('project_roles.project_rights != ?', 'none').
uniq.
sort_by { |protocol| (protocol.id || '0000') + protocol.id }.
reverse
end

def include_archived?
archived = [false]

if @params[:include_archived] == 'true'
archived.push true
end

archived
end
end
end
47 changes: 27 additions & 20 deletions spec/controllers/portal/protocols_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -1,34 +1,41 @@
require 'rails_helper'

RSpec.describe Portal::ProtocolsController do

stub_portal_controller

let!(:identity) { create(:identity)}

before :each do
session[:identity_id] = identity.id
end
before(:each) { session[:identity_id] = identity.id }

describe 'GET #index.js' do

let!(:archived_protocol) { create(:protocol_without_validations, archived: true) }
let!(:unarchived_protocol) { create(:protocol_without_validations) }

before do
create(:project_role_approve, protocol: unarchived_protocol,
identity: identity)
create(:project_role_approve, protocol: archived_protocol,
identity: identity)
end

context 'default portal view' do

describe "GET #index.js" do
let!(:archived_protocol) { create(:protocol_without_validations, archived: true) }
let!(:unarchived_protocol) { create(:protocol_without_validations, archived: false) }
let!(:project_role_one) { create(:project_role, protocol: unarchived_protocol, identity: identity, project_rights: "not none") }
let!(:project_role_two) { create(:project_role, protocol: archived_protocol, identity: identity, project_rights: "not none") }
before { get :index, format: :js }

context "default portal view" do
before {get :index, format: :js}
it 'does not contain archived protocol' do
expect(assigns(:protocols)).to eq([unarchived_protocol])
end
end

it "does not contain archived protocol" do
expect(assigns(:protocols)).to eq([unarchived_protocol])
end
end
context 'filtered portal view' do

context "filtered portal view" do
before {get :index, include_archived: "true", format: :js}
before { get :index, include_archived: 'true', format: :js }

it "shows archived and unarchived protocols" do
expect(assigns(:protocols).sort).to eq([unarchived_protocol, archived_protocol].sort)
end
end
it 'shows archived and unarchived protocols' do
expect(assigns(:protocols).sort).to eq([archived_protocol, unarchived_protocol])
end
end
end
end
8 changes: 6 additions & 2 deletions spec/factories/project_role.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@
factory :project_role do
protocol nil
identity nil

project_rights { Faker::Lorem.sentence(2) }
role 'primary-pi'
role 'primary-pi'

trait :approve do
project_rights 'approve'
end

trait :with_identity do
identity
Expand All @@ -39,5 +42,6 @@

factory :project_role_with_identity, traits: [:with_identity]
factory :project_role_with_identity_and_protocol, traits: [:with_identity, :with_protocol]
factory :project_role_approve, traits: [:approve]
end
end
80 changes: 80 additions & 0 deletions spec/lib/portal/protocol_finder_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
require 'rails_helper'

RSpec.describe Portal::ProtocolFinder do

describe '.protocols' do

let!(:identity) { create :identity }
let!(:default_protocol) { create :protocol_without_validations }
let!(:unarchived_protocol) { create :protocol_without_validations }
let!(:archived_protocol) { create :protocol_without_validations,
archived: true }
before do
create_list :project_role_approve, 3,
protocol: default_protocol,
identity: identity
create_list :project_role_approve, 3,
protocol: unarchived_protocol,
identity: identity
create_list :project_role_approve, 3,
protocol: archived_protocol,
identity: identity
end

context 'default_protocol not present' do

context 'including archived Protocols' do

let(:params) { { include_archived: 'true' } }
let(:protocol_finder) { Portal::ProtocolFinder.new identity, params }

it 'should return an array of archived and unarchived Protocols' do
expect(protocol_finder.protocols).to eq([archived_protocol, unarchived_protocol, default_protocol])
end
end

context 'not including archived Protocols' do

let(:params) { { include_archived: 'false' } }
let(:protocol_finder) { Portal::ProtocolFinder.new identity, params }

it 'should return an array of unarchived Protocols' do
expect(protocol_finder.protocols).to eq([unarchived_protocol, default_protocol])
end
end
end

context 'default_protocol present' do

context 'including archived Protocols' do

let(:params) {
{
include_archived: 'true',
default_protocol: default_protocol.id
}
}
let(:protocol_finder) { Portal::ProtocolFinder.new identity, params }

it 'should return an array of archived and unarchived Protocols with the default Protocol first' do
expect(protocol_finder.protocols).to eq([default_protocol, archived_protocol, unarchived_protocol])
end
end

context 'not including archived Protocols' do

let(:params) {
{
include_archived: 'false',
default_protocol: default_protocol.id
}
}
let(:protocol_finder) { Portal::ProtocolFinder.new identity, params }

it 'should return an array of unarchived Protocols with the default Protocol first' do
expect(protocol_finder.protocols).to eq([default_protocol, unarchived_protocol])
end
end
end
end
end
2 changes: 1 addition & 1 deletion spec/models/project_role/project_role_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

require 'rails_helper'

RSpec.describe "Project Role" do
RSpec.describe 'Project Role' do

let!(:user) {create(:identity)}
let!(:user2) {create(:identity)}
Expand Down
5 changes: 3 additions & 2 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@

# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
Dir[Rails.root.join("spec/support/*.rb")].each { |f| require f }

Dir[Rails.root.join('spec/support/**/*.rb')].each { |file| require file }
Dir[Rails.root.join('lib/**/*.rb')].each { |file| require file }

# This file was generated by the `rails generate rspec:install` command. Conventionally, all
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
Expand Down

0 comments on commit f946718

Please sign in to comment.