-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #204 from sparc-request/protal-protocol-controller…
…-spec Refactor: Portal::ProtocolController#index
- Loading branch information
Showing
8 changed files
with
184 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters