-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into feature/roles
- Loading branch information
Showing
47 changed files
with
1,324 additions
and
98 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
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,7 +1,20 @@ | ||
module SystemAdmin | ||
class DashboardController < AdminController | ||
def show | ||
@kpis = Kpis.new | ||
@kpis = Kpis.new(**kpi_params) | ||
rescue ArgumentError => e | ||
flash[:alert] = e.message | ||
redirect_to(dashboard_path) | ||
end | ||
|
||
private | ||
|
||
def kpi_params | ||
params | ||
.fetch(:date_range, {}) | ||
.permit(:unit, :range_start, :range_end) | ||
.to_hash | ||
.symbolize_keys | ||
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,61 @@ | ||
# == Schema Information | ||
# | ||
# Table name: events | ||
# | ||
# id :bigint not null, primary key | ||
# action :string | ||
# data :jsonb | ||
# entity_class :string | ||
# created_at :datetime not null | ||
# updated_at :datetime not null | ||
# entity_id :integer | ||
# | ||
class Event < ApplicationRecord | ||
ACTIONS = [ | ||
CREATED = "created".freeze, | ||
UPDATED = "updated".freeze, | ||
DELETED = "deleted".freeze, | ||
].freeze | ||
|
||
validates(:action, presence: true) | ||
validates(:action, inclusion: { in: ACTIONS }) | ||
validates(:entity_class, presence: true) | ||
validates(:entity_id, presence: true) | ||
validates(:data, presence: true, unless: :deleted?) | ||
|
||
class << self | ||
def publish(action, model, model_changes = {}) | ||
create!( | ||
action: action&.to_s, | ||
entity_class: model&.class, | ||
entity_id: model&.id, | ||
data: filtered(model, model_changes), | ||
) | ||
end | ||
|
||
private | ||
|
||
def filtered(model, model_changes) | ||
model_changes.each_with_object({}) do |(attribute, diff), hsh| | ||
hsh[attribute] = diff | ||
hsh[attribute] = obfuscate(diff) if filtered_attribute?(model, attribute) | ||
|
||
hsh | ||
end | ||
end | ||
|
||
def filtered_attribute?(model, attribute) | ||
Rails.configuration.x.events.filtered_attributes | ||
.fetch(model.class.name, []) | ||
.include?(attribute) | ||
end | ||
|
||
def obfuscate(diff) | ||
Array(diff).map { |e| e.blank? ? e : "[FILTERED]" } | ||
end | ||
end | ||
|
||
def deleted? | ||
action == DELETED | ||
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
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 |
---|---|---|
@@ -1,38 +1,41 @@ | ||
# frozen_string_literal: true | ||
|
||
# Urn represents a Uniform Resource Name (URN) generator. | ||
# It generates a URN with a fixed prefix and a random alphanumeric suffix. | ||
# | ||
# == Schema Information | ||
# | ||
# Example: | ||
# Table name: urns | ||
# | ||
# Urn.generate('teacher') # => "IRPTE12345" | ||
# Urn.generate('teacher') # => "IRPTE12345" | ||
# Urn.generate('salaried_trainee') # => "IRPST12345" | ||
# id :bigint not null, primary key | ||
# code :string | ||
# prefix :string | ||
# suffix :integer | ||
# created_at :datetime not null | ||
# updated_at :datetime not null | ||
# | ||
class Urn | ||
attr_reader :value | ||
attr_writer :suffix | ||
|
||
def self.generate(applicant_type) | ||
code = applicant_type_code(applicant_type) | ||
PREFIX + code + Array.new(LENGTH) { CHARSET.sample }.join | ||
end | ||
class Urn < ApplicationRecord | ||
class NoUrnAvailableError < StandardError; end | ||
|
||
CHARSET = %w[0 1 2 3 4 5 6 7 8 9].freeze | ||
PREFIX = "IRP" | ||
LENGTH = 5 | ||
private_constant :CHARSET, :PREFIX, :LENGTH | ||
PREFIX = "IRP".freeze | ||
MAX_SUFFIX = 99_999 | ||
PADDING_SIZE = MAX_SUFFIX.to_s.size | ||
VALID_CODES = { | ||
"teacher" => "TE", | ||
"salaried_trainee" => "ST", | ||
}.freeze | ||
|
||
def self.applicant_type_code(applicant_type) | ||
case applicant_type | ||
when "teacher" | ||
"TE" | ||
when "salaried_trainee" | ||
"ST" | ||
else | ||
raise(ArgumentError, "Invalid applicant type: #{applicant_type}") | ||
def self.next(route) | ||
code = VALID_CODES.fetch(route) | ||
Urn.transaction do | ||
urn = find_by!(code:) | ||
urn.destroy! | ||
urn.to_s | ||
end | ||
rescue KeyError => e | ||
Sentry.capture_exception(e) | ||
raise(ArgumentError, "Unknown route #{route}") | ||
rescue ActiveRecord::RecordNotFound => e | ||
Sentry.capture_exception(e) | ||
raise(NoUrnAvailableError, "There no more unique URN available for #{route}") | ||
end | ||
|
||
def to_s | ||
[prefix, code, sprintf("%0#{PADDING_SIZE}d", suffix)].join | ||
end | ||
private_methods :applicant_type_code | ||
end |
Oops, something went wrong.