Skip to content

Commit

Permalink
Merge branch 'develop' into admission-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JoaoFelipe committed Nov 29, 2024
2 parents 235c702 + fc3f09c commit c58db2c
Show file tree
Hide file tree
Showing 13 changed files with 295 additions and 5 deletions.
33 changes: 33 additions & 0 deletions app/controllers/grants_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Copyright (c) Universidade Federal Fluminense (UFF).
# This file is part of SAPOS. Please, consult the license terms in the LICENSE file.

# frozen_string_literal: true

class GrantsController < ApplicationController
authorize_resource

active_scaffold :grant do |config|
config.create.label = :create_grant_label
config.columns = [:title, :start_year, :end_year, :professor, :kind, :funder, :amount]
config.actions.swap :search, :field_search
config.columns[:professor].form_ui = :record_select
config.columns[:kind].form_ui = :select
config.columns[:kind].options = {
options: Grant::KINDS,
default: Grant::PUBLIC,
include_blank: I18n.t("active_scaffold._select_")
}
config.columns[:amount].options[:format] = :currency
config.columns[:start_year].options[:format] = "%d"
config.columns[:end_year].options[:format] = "%d"
config.actions.exclude :deleted_records
end

protected
def do_new
super
unless current_user.professor.blank?
@record.professor = current_user.professor
end
end
end
1 change: 1 addition & 0 deletions app/controllers/report_configurations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

class ReportConfigurationsController < ApplicationController
authorize_resource
skip_before_action :verify_authenticity_token, only: [:preview] # noqa # do not remove this line based on Code Scanner suggestions

include ApplicationHelper
active_scaffold :report_configuration do |config|
Expand Down
17 changes: 17 additions & 0 deletions app/helpers/grants_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Copyright (c) Universidade Federal Fluminense (UFF).
# This file is part of SAPOS. Please, consult the license terms in the LICENSE file.

# frozen_string_literal: true

module GrantsHelper
def professor_form_column(record, options)
if can?(:edit_professor, record)
record_select_field :professor, record.professor || Professor.new, options
else
options[:value] = record.professor_id
label(
:record_value_1_params, record.id, record.professor.name
) + hidden_field(:record, :professor, options)
end
end
end
5 changes: 4 additions & 1 deletion app/models/ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Ability
PROFESSOR_MODELS = [
Professor, Advisement, AdvisementAuthorization,
ThesisDefenseCommitteeParticipation,
ProfessorResearchArea,
ProfessorResearchArea, Grant
]

SCHOLARSHIP_MODELS = [
Expand Down Expand Up @@ -140,6 +140,9 @@ def initialize_professors(user, roles)
end
if roles[Role::ROLE_PROFESSOR]
can :read, Ability::PROFESSOR_MODELS
cannot :edit_professor, Grant
can :create, Grant
can :update, Grant, professor: user.professor
end
end

Expand Down
53 changes: 53 additions & 0 deletions app/models/grant.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Copyright (c) Universidade Federal Fluminense (UFF).
# This file is part of SAPOS. Please, consult the license terms in the LICENSE file.

# frozen_string_literal: true

class Grant < ApplicationRecord
has_paper_trail
belongs_to :professor, optional: false

PUBLIC = I18n.translate(
"activerecord.attributes.grant.kinds.public"
)
PRIVATE = I18n.translate(
"activerecord.attributes.grant.kinds.private"
)
KINDS = [PUBLIC, PRIVATE]

validates :title, presence: true
validates :start_year, presence: true
validates :kind, presence: true, inclusion: { in: KINDS }
validates :funder, presence: true
validates :amount, presence: true, numericality: { greater_than_or_equal_to: 0 }
validates :professor, presence: true
validate :that_professor_cannot_edit_other_grants, if: -> { cannot?(:edit_professor, self) }
validate :that_end_year_is_greater_than_start_year


def that_professor_cannot_edit_other_grants
current_professor = current_user&.professor
return if current_professor.nil?
if self.changes[:professor_id].present? && self.changes[:professor_id] != [nil, current_professor.id]
self.errors.add(:professor, :cannot_edit_other_grants)
end
end

def that_end_year_is_greater_than_start_year
return if self.end_year.nil?
if self.end_year < self.start_year
self.errors.add(:end_year, :start_greater_than_end)
end
end

def to_label
"[#{self.start_year}] #{self.title}"
end

private
delegate :can?, :cannot?, to: :ability

def ability
@ability ||= Ability.new(current_user)
end
end
34 changes: 34 additions & 0 deletions config/locales/grant.pt-BR.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Copyright (c) Universidade Federal Fluminense (UFF).
# This file is part of SAPOS. Please, consult the license terms in the LICENSE file.

pt-BR:
activerecord:
attributes:
grant:
title: "Título"
start_year: "Ano de Início"
end_year: "Ano de Término"
professor: "Coordenador"
kind: "Tipo de financiamento"
kinds:
public: "Público"
private: "Privado"
funder: "Financiador"
amount: "Valor total"
description:
grant:
funder: "(e.g., CNPq, FAPERJ, nome da empresa, etc)"

errors:
models:
grant:
cannot_edit_other_grants: "não pode ser editado por este usuário"
start_greater_than_end: "não pode ser menor do que Ano de Início"

models:
grant:
one: "Coordenação de Projeto"
other: "Coordenações de Projetos"

active_scaffold:
create_grant_label: "Adicionar Coordenação de Projeto"
1 change: 1 addition & 0 deletions config/locales/navigation.pt-BR.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pt-BR:
advisement: Orientações
advisement_authorization: Credenciamentos
thesis_defense_committee_participation: Bancas
grant: Coordenações de Projetos

scholarships:
label: Bolsas
Expand Down
1 change: 1 addition & 0 deletions config/navigation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ def can_read?(*args)
submenu.modelitem Advisement
submenu.modelitem AdvisementAuthorization
submenu.modelitem ThesisDefenseCommitteeParticipation
submenu.modelitem Grant
end

scholar_models = [Scholarship, ScholarshipType, ScholarshipDuration]
Expand Down
5 changes: 5 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -519,4 +519,9 @@
if Rails.env.development?
mount LetterOpenerWeb::Engine, at: "/letter_opener"
end

resources :grants do
concerns :active_scaffold
end

end
20 changes: 20 additions & 0 deletions db/migrate/20241129022042_create_grants.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright (c) Universidade Federal Fluminense (UFF).
# This file is part of SAPOS. Please, consult the license terms in the LICENSE file.

# frozen_string_literal: true

class CreateGrants < ActiveRecord::Migration[7.0]
def change
create_table :grants do |t|
t.string :title
t.integer :start_year
t.integer :end_year
t.string :kind
t.string :funder
t.decimal :amount, precision: 14, scale: 2
t.references :professor, null: false, foreign_key: true

t.timestamps
end
end
end
52 changes: 48 additions & 4 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions spec/factories/factory_grant.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Copyright (c) Universidade Federal Fluminense (UFF).
# This file is part of SAPOS. Please, consult the license terms in the LICENSE file.

# frozen_string_literal: true

FactoryBot.define do
factory :grant do
title { "Projeto" }
start_year { 2024 }
kind { Grant::PUBLIC }
funder { "CNPq" }
amount { 100000 }
professor
end
end
Loading

0 comments on commit c58db2c

Please sign in to comment.