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

Panel front end #638

Merged
merged 15 commits into from
Oct 8, 2020
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions app/assets/stylesheets/app.scss
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,6 @@
font-size: .95em;
}
}

thead tr {
th {
font-size: .85em;
vertical-align: bottom;
}
}
}

footer {
Expand All @@ -99,7 +92,9 @@
}

th {
font-size: .85em;
text-align: left;
vertical-align: bottom;
}

// submission labels contain paragraphs
Expand Down
7 changes: 4 additions & 3 deletions app/controllers/grant_submissions/submissions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ def show
def new
@grant = Grant.kept
.friendly
.includes(form:
.includes( form:
{ sections:
{questions: :multiple_choice_options} } )
{ questions: :multiple_choice_options} } )
.with_reviewers.with_panel
.find(params[:grant_id])
@grant = GrantDecorator.new(@grant)
set_submission
Expand Down Expand Up @@ -85,7 +86,7 @@ def destroy
private

def set_grant
@grant = Grant.kept.friendly.find(params[:grant_id])
@grant = Grant.kept.friendly.with_reviewers.with_panel.find(params[:grant_id])
end

def submission_redirect(grant, submission)
Expand Down
47 changes: 44 additions & 3 deletions app/controllers/panels_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,46 @@
class PanelsController < ApplicationController
def edit; end
def update; end
def show; end
before_action :set_grant_and_panel
before_action :set_submissions, only: :show

def show
authorize @panel
render :show
end

def edit
authorize @grant, :grant_editor_access?
render :edit
end

def update
authorize @grant, :grant_editor_access?
if @panel.update(panel_params)
flash[:notice] = 'Panel information successfully updated.'
redirect_to edit_grant_panel_path(@grant)
else
flash.now[:alert] = @panel.errors.full_messages
render :edit
end
end

private

def set_grant_and_panel
@grant = Grant.kept.friendly.find(params[:grant_id])
@panel = @grant.panel
end

def set_submissions
@submissions = @grant.submissions.reviewed.with_applicant
end

def panel_params
params.require(:panel).permit(
:start_datetime,
:end_datetime,
:instructions,
:meeting_link,
:meeting_location
)
end
end
1 change: 1 addition & 0 deletions app/helpers/grants_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def populate_grant_tabs(grant_permission_role:, grant:)
'Submissions' => grant_submissions_path(grant),
'Reviewers' => grant_reviewers_path(grant),
'Reviews' => grant_reviews_path(grant),
'Panel' => edit_grant_panel_path(grant),
'Permissions' => grant_grant_permissions_path(grant) }
when 'viewer'
{ 'View' => grant_path(grant),
Expand Down
8 changes: 8 additions & 0 deletions app/models/grant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ class Grant < ApplicationRecord
scope :unassigned_submissions, lambda { |*args| where('submission_reviews_count < :max_reviewers', { :max_reviewers => args.first || 2 }) }
scope :with_reviewers, -> { includes(:reviewers) }
scope :with_reviews, -> { includes(:reviews) }
scope :with_panel, -> { includes(:panel) }

def is_discardable?
SOFT_DELETABLE_STATES.include?(state) ? true : send("#{state}_discardable?")
Expand All @@ -158,6 +159,13 @@ def requires_one_criteria
errors.add(:base, 'Must have at least one review criteria.')
end

# def admins, def editors, def viewers
GrantPermission::ROLES.each do |_,role|
define_method "#{role.pluralize}".to_sym do
grant_permissions.send("role_#{role}").to_a.map(&:user)
end
end

private

def set_default_state
Expand Down
1 change: 1 addition & 0 deletions app/models/grant_submission/submission.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class Submission < ApplicationRecord
scope :sort_by_composite_score_nulls_last_asc, -> { order(Arel.sql("composite_score = 0 nulls last, composite_score ASC")) }
scope :sort_by_composite_score_nulls_last_desc, -> { order(Arel.sql("composite_score = 0 nulls last, composite_score DESC")) }

scope :reviewed, -> { where("average_overall_impact_score > 0") }

# TODO: available? to...edit? delete?
def available?
Expand Down
23 changes: 21 additions & 2 deletions app/models/panel.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,33 @@
class Panel < ApplicationRecord
has_paper_trail versions: { class_name: 'PaperTrail::PanelVersion' },
meta: { grant_id: :grant_id }

belongs_to :grant

validates_presence_of :start_datetime, if: -> { end_datetime? },
message: 'is required if end is provided.'
validates_presence_of :end_datetime, if: -> { start_datetime? },
message: 'is required if start is provided.'

validates_datetime :start_datetime, before: :end_datetime,
if: -> { start_datetime? || end_datetime? }
if: -> { start_datetime? || end_datetime? },
before_message: 'must be before End Date/Time'

validates_uniqueness_of :grant
validates :meeting_link, if: -> { meeting_link? },
format: { with: /\A(https:\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$)\z/ix,
message: 'is not a valid secure URL.' }

validate :start_is_after_submission_deadline, if: :start_datetime?

def is_open?
return false if !start_datetime? || !end_datetime?
DateTime.now.between?(start_datetime, end_datetime)
end

private

def start_is_after_submission_deadline
errors.add(:start_datetime, message: 'must be after the submission close date.') if start_datetime < grant.submission_close_date.end_of_day
errors.add(:start_datetime, :before_submission_deadline) if start_datetime < grant.submission_close_date.end_of_day
end
end
6 changes: 6 additions & 0 deletions app/models/paper_trail/panel_version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

class PaperTrail::PanelVersion < PaperTrail::Version
self.table_name = :panel_versions
self.sequence_name = :panel_versions_id_seq
end
31 changes: 6 additions & 25 deletions app/policies/grant_policy.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
# frozen_string_literal: true

class GrantPolicy < ApplicationPolicy
GRANT_ACCESS = { 'viewer' => 1,
'editor' => 2,
'admin' => 3 }
GRANT_ACCESS.default = -1
GRANT_ACCESS.freeze
include GrantRoleAccess

class Scope < Scope
def resolve
Expand All @@ -26,48 +22,33 @@ def show?
end

def create?
user.system_admin? || user.grant_creator?
user.present? && user.system_admin? || user.grant_creator?
end

def new?
create?
user.present? && create?
end

def update?
grant_editor_access?
user.present? && grant_editor_access?
end

def edit?
grant_viewer_access?
user.present? && grant_viewer_access?
end

def destroy?
grant_admin_access?
user.present? && grant_admin_access?
end

def duplicate?
user.system_admin? || (user.grant_creator? && grant_editor_access?)
end

def grant_admin_access?
GRANT_ACCESS['admin'] <= GRANT_ACCESS[user.get_role_by_grant(grant: grant)]
end

def grant_editor_access?
GRANT_ACCESS['editor'] <= GRANT_ACCESS[user.get_role_by_grant(grant: grant)]
end

def grant_viewer_access?
GRANT_ACCESS['viewer'] <= GRANT_ACCESS[user.get_role_by_grant(grant: grant)]
end

private

def grant
record
end

def user_is_grant_reviewer?
GrantReviewer.find_by(reviewer: user, grant: grant).present?
end
end
23 changes: 23 additions & 0 deletions app/policies/grant_role_access.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module GrantRoleAccess
GRANT_ACCESS = { 'viewer' => 1,
'editor' => 2,
'admin' => 3 }
GRANT_ACCESS.default = -1
GRANT_ACCESS.freeze

def grant_admin_access?
GRANT_ACCESS['admin'] <= GRANT_ACCESS[user.get_role_by_grant(grant: grant)]
end

def grant_editor_access?
GRANT_ACCESS['editor'] <= GRANT_ACCESS[user.get_role_by_grant(grant: grant)]
end

def grant_viewer_access?
GRANT_ACCESS['viewer'] <= GRANT_ACCESS[user.get_role_by_grant(grant: grant)]
end

def user_is_grant_reviewer?
GrantReviewer.find_by(reviewer: user, grant: grant).present?
end
end
5 changes: 3 additions & 2 deletions app/policies/grant_submission/form_policy.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# frozen_string_literal: true

class GrantSubmission::FormPolicy < GrantPolicy
class GrantSubmission::FormPolicy < ApplicationPolicy
include GrantRoleAccess

def update?
super
user.present? && grant_editor_access?
end

def edit?
Expand Down
3 changes: 2 additions & 1 deletion app/policies/grant_submission/submission_policy.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# frozen_string_literal: true

class GrantSubmission::SubmissionPolicy < GrantPolicy
class GrantSubmission::SubmissionPolicy < ApplicationPolicy
attr_reader :user, :grant
include GrantRoleAccess

def initialize(context, record)
@user = context.user
Expand Down
27 changes: 27 additions & 0 deletions app/policies/panel_policy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

class PanelPolicy < ApplicationPolicy
include GrantRoleAccess

def show?
user.present? && (user_is_grant_reviewer? || grant_viewer_access?)
end

def edit?
user.present? && grant_editor_access?
end

def update?
edit?
end

private

def panel
record
end

def grant
record.grant
end
end
1 change: 1 addition & 0 deletions app/services/grant_services/duplicate_dependencies.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def self.call(original_grant:, new_grant:)

GrantSubmissionFormServices::Duplicate.call(original_grant: original_grant, new_grant: new_grant)

PanelServices::Duplicate.call(original_grant: original_grant, new_grant: new_grant)
end
OpenStruct.new(success?: true)
rescue ActiveRecord::RecordInvalid => invalid
Expand Down
2 changes: 2 additions & 0 deletions app/services/grant_services/new.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ def self.call(grant:, user:)
GrantSubmissionFormServices::New.call(grant: grant, user: user)
# Create starter criteria
CriterionServices::New.call(grant: grant)
# Create starter panel
Panel.create!(grant: grant)
end
OpenStruct.new(success?: true)
rescue ActiveRecord::RecordInvalid => invalid
Expand Down
12 changes: 12 additions & 0 deletions app/services/panel_services/duplicate.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

module PanelServices
module Duplicate
def self.call(original_grant:, new_grant:)
new_panel = original_grant.panel.dup
new_panel.update(grant: new_grant, start_datetime: nil, end_datetime: nil)
rescue ActiveRecord::RecordInvalid => invalid
raise ServiceError::InputInvalid.new(error: invalid)
end
end
end
9 changes: 9 additions & 0 deletions app/views/grants/_dates_card.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,12 @@
Submission Close Date:
%dd
= grant.submission_close_date
- if current_user.present? && grant.panel.start_datetime? && (current_user.get_role_by_grant(grant: grant).present? || grant.reviewers.include?(current_user))
%dt
Panel Start:
%dd
= grant.panel.start_datetime
%dt.secondary
Panel End:
%dd
= grant.panel.end_datetime
13 changes: 13 additions & 0 deletions app/views/panels/_admins.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

%table
%tr
%th
Applicant
%th
Submission (link)
%th
Reviews (link)
%th
Overall Impact Avg.
%th
Composite Avg.
Loading