Skip to content

Commit

Permalink
Identify signing tags by user group
Browse files Browse the repository at this point in the history
  • Loading branch information
mirrec committed Dec 6, 2023
1 parent 7031cf7 commit e0a579c
Show file tree
Hide file tree
Showing 20 changed files with 213 additions and 111 deletions.
17 changes: 17 additions & 0 deletions app/models/draft_tag.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
# == Schema Information
#
# Table name: tags
#
# id :bigint not null, primary key
# color :enum
# external_name :string
# icon :string
# name :string not null
# tag_groups_count :integer default(0), not null
# type :string not null
# visible :boolean default(TRUE), not null
# created_at :datetime not null
# updated_at :datetime not null
# owner_id :bigint
# tenant_id :bigint not null
#
class DraftTag < Tag
def destroyable?
false
Expand Down
38 changes: 36 additions & 2 deletions app/models/group_membership.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,47 @@ class GroupMembership < ApplicationRecord

after_create do |group_membership|
if group_membership.group.is_a?(SignerGroup)
SignerGroup.user_added_to_group(group_membership.group, group_membership.user)
self.class.create_signing_tags_for(group_membership)
end
end

after_destroy do |group_membership|
if group_membership.group.is_a?(SignerGroup)
SignerGroup.user_removed_from_group(group_membership.group, group_membership.user)
self.class.destroy_all_signature_requests_for(group_membership)
end
end

def self.create_signing_tags_for(group_membership)
user = group_membership.user
user_group = user.user_group
tenant = group_membership.group.tenant

find_or_create_signing_tag(
tags_scope: tenant.signature_requested_from_tags,
user_group: user_group,
tag_name: "Na podpis - #{user.name}"
)

find_or_create_signing_tag(
tags_scope: tenant.signed_by_tags,
user_group: user_group,
tag_name: "Podpisané - #{user.name}"
)
end

def self.destroy_all_signature_requests_for(group_membership)
tag = group_membership.group.tenant.signature_requested_from_tags.find_tag_containing_group(group_membership.user.user_group)
tag.destroy if tag
end

def self.find_or_create_signing_tag(tags_scope:, user_group:, tag_name:)
tag = tags_scope.find_tag_containing_group(user_group) || tags_scope.find_or_initialize_by(
name: tag_name
)

tag.name = tag_name
tag.visible = true
tag.groups = [user_group]
tag.save!
end
end
19 changes: 19 additions & 0 deletions app/models/signature_requested_from_tag.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# == Schema Information
#
# Table name: tags
#
# id :bigint not null, primary key
# color :enum
# external_name :string
# icon :string
# name :string not null
# tag_groups_count :integer default(0), not null
# type :string not null
# visible :boolean default(TRUE), not null
# created_at :datetime not null
# updated_at :datetime not null
# owner_id :bigint
# tenant_id :bigint not null
#
class SignatureRequestedFromTag < Tag
end
22 changes: 12 additions & 10 deletions app/models/signature_requested_tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@
#
# Table name: tags
#
# id :bigint not null, primary key
# external_name :string
# name :string not null
# type :string not null
# visible :boolean default(TRUE), not null
# created_at :datetime not null
# updated_at :datetime not null
# owner_id :bigint
# tenant_id :bigint not null
# user_id :integer
# id :bigint not null, primary key
# color :enum
# external_name :string
# icon :string
# name :string not null
# tag_groups_count :integer default(0), not null
# type :string not null
# visible :boolean default(TRUE), not null
# created_at :datetime not null
# updated_at :datetime not null
# owner_id :bigint
# tenant_id :bigint not null
#
class SignatureRequestedTag < Tag
end
18 changes: 0 additions & 18 deletions app/models/signature_requested_to_tag.rb

This file was deleted.

23 changes: 12 additions & 11 deletions app/models/signed_by_tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@
#
# Table name: tags
#
# id :bigint not null, primary key
# external_name :string
# name :string not null
# type :string not null
# visible :boolean default(TRUE), not null
# created_at :datetime not null
# updated_at :datetime not null
# owner_id :bigint
# tenant_id :bigint not null
# user_id :integer
# id :bigint not null, primary key
# color :enum
# external_name :string
# icon :string
# name :string not null
# tag_groups_count :integer default(0), not null
# type :string not null
# visible :boolean default(TRUE), not null
# created_at :datetime not null
# updated_at :datetime not null
# owner_id :bigint
# tenant_id :bigint not null
#
class SignedByTag < Tag
belongs_to :user
end
22 changes: 12 additions & 10 deletions app/models/signed_tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@
#
# Table name: tags
#
# id :bigint not null, primary key
# external_name :string
# name :string not null
# type :string not null
# visible :boolean default(TRUE), not null
# created_at :datetime not null
# updated_at :datetime not null
# owner_id :bigint
# tenant_id :bigint not null
# user_id :integer
# id :bigint not null, primary key
# color :enum
# external_name :string
# icon :string
# name :string not null
# tag_groups_count :integer default(0), not null
# type :string not null
# visible :boolean default(TRUE), not null
# created_at :datetime not null
# updated_at :datetime not null
# owner_id :bigint
# tenant_id :bigint not null
#
class SignedTag < Tag
end
14 changes: 1 addition & 13 deletions app/models/signer_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,7 @@ def name
I18n.t("group.names.signer")
end

def self.user_added_to_group(signer_group, user)
tag = signer_group.tenant.signature_requested_to_tags.find_or_initialize_by(user_id: user.id)
tag.name = "Na podpis - #{user.name}"
tag.visible = true
tag.save!

tag = signer_group.tenant.signed_by_tags.find_or_initialize_by(user_id: user.id)
tag.name = "Podpisané - #{user.name}"
tag.visible = true
tag.save!
end

def self.user_removed_from_group(signer_group, user)
signer_group.tenant.signature_requested_to_tags.where(user_id: user.id).destroy_all
signer_group.tenant.signature_requested_from_tags.where(user_id: user.id).destroy_all
end
end
17 changes: 17 additions & 0 deletions app/models/simple_tag.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
# == Schema Information
#
# Table name: tags
#
# id :bigint not null, primary key
# color :enum
# external_name :string
# icon :string
# name :string not null
# tag_groups_count :integer default(0), not null
# type :string not null
# visible :boolean default(TRUE), not null
# created_at :datetime not null
# updated_at :datetime not null
# owner_id :bigint
# tenant_id :bigint not null
#
class SimpleTag < Tag
def destroyable?
true
Expand Down
21 changes: 21 additions & 0 deletions app/models/tag.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
# == Schema Information
#
# Table name: tags
#
# id :bigint not null, primary key
# color :enum
# external_name :string
# icon :string
# name :string not null
# tag_groups_count :integer default(0), not null
# type :string not null
# visible :boolean default(TRUE), not null
# created_at :datetime not null
# updated_at :datetime not null
# owner_id :bigint
# tenant_id :bigint not null
#
class Tag < ApplicationRecord
include AuditableEvents
include Colorized, Iconized
Expand Down Expand Up @@ -31,4 +48,8 @@ def gives_access?
def destroyable?
raise NotImplementedError
end

def self.find_tag_containing_group(group)
includes(:groups).to_a.find { |tag| tag.groups.include?(group) }
end
end
17 changes: 7 additions & 10 deletions app/models/tenant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,19 @@ class Tenant < ApplicationRecord
has_many :custom_groups

has_one :draft_tag
has_one :everything_tag
has_one :signature_requested_tag
has_one :signed_tag
has_one :everything_tag

has_many :boxes, dependent: :destroy
has_many :automation_rules, class_name: "Automation::Rule", dependent: :destroy
has_many :tags, dependent: :destroy
has_many :signature_requested_to_tags
has_many :signature_requested_from_tags
has_many :signed_by_tags
has_many :simple_tags
has_many :filters
after_create :create_default_objects

after_commit :create_default_objects, on: :create

validates_presence_of :name

Expand Down Expand Up @@ -70,17 +71,13 @@ def make_admins_see_everything!
def create_default_objects
create_draft_tag!(name: "Rozpracované", visible: true)
create_everything_tag!(name: "Všetky správy", visible: false)

create_all_group!(name: "all")
create_admin_group!(name: "admins")
create_signer_group!(name: "signers")
<<<<<<< HEAD
create_draft_tag!(name: "Rozpracované", visible: true)
create_signature_requested_tag!(name: "Na podpis", visible: true)
create_signed_tag!(name: "Podpísané", visible: true)
=======

create_all_group!(name: "all")
create_admin_group!(name: "admins")

make_admins_see_everything!
>>>>>>> main
end
end
17 changes: 17 additions & 0 deletions app/models/upvs/delivery_notification_tag.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
# == Schema Information
#
# Table name: tags
#
# id :bigint not null, primary key
# color :enum
# external_name :string
# icon :string
# name :string not null
# tag_groups_count :integer default(0), not null
# type :string not null
# visible :boolean default(TRUE), not null
# created_at :datetime not null
# updated_at :datetime not null
# owner_id :bigint
# tenant_id :bigint not null
#
class Upvs::DeliveryNotificationTag < ::Tag
def self.find_or_create_for_tenant!(tenant)
find_or_create_by!(
Expand Down
11 changes: 8 additions & 3 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ class User < ApplicationRecord

belongs_to :tenant

has_one :signature_requested_to_tag, dependent: :destroy
has_one :signed_by_tag, dependent: :nullify

has_many :group_memberships, dependent: :destroy
has_many :groups, through: :group_memberships
has_many :own_tags, class_name: 'Tag', inverse_of: :owner, foreign_key: :owner_id, dependent: :nullify
Expand All @@ -42,6 +39,14 @@ def user_group
groups.where(type: "UserGroup").first
end

def signed_by_tag
tenant.signed_by_tags.find_tag_containing_group(user_group)
end

def signature_requested_from_tag
tenant.signature_requested_from_tags.find_tag_containing_group(user_group)
end

private

def delete_user_group
Expand Down
26 changes: 0 additions & 26 deletions db/migrate/20231204133308_add_user_id_to_tags.rb

This file was deleted.

Loading

0 comments on commit e0a579c

Please sign in to comment.