Skip to content

Commit

Permalink
Handle creation and removing signing tags
Browse files Browse the repository at this point in the history
  • Loading branch information
mirrec committed Dec 4, 2023
1 parent bd761a5 commit b1274af
Show file tree
Hide file tree
Showing 16 changed files with 173 additions and 2 deletions.
1 change: 1 addition & 0 deletions app/models/draft_tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# updated_at :datetime not null
# owner_id :bigint
# tenant_id :bigint not null
# user_id :integer
#
class DraftTag < Tag
end
12 changes: 12 additions & 0 deletions app/models/group_membership.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,16 @@ class GroupMembership < ApplicationRecord

belongs_to :group
belongs_to :user

after_create do |group_membership|
if group_membership.group.is_a?(SignerGroup)
SignerGroup.user_added_to_group(group_membership.group, group_membership.user)
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)
end
end
end
17 changes: 17 additions & 0 deletions app/models/signature_requested_tag.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# == Schema Information
#
# 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
#
class SignatureRequestedTag < Tag
end
18 changes: 18 additions & 0 deletions app/models/signature_requested_to_tag.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# == Schema Information
#
# 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
#
class SignatureRequestedToTag < Tag
belongs_to :user
end
18 changes: 18 additions & 0 deletions app/models/signed_by_tag.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# == Schema Information
#
# 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
#
class SignedByTag < Tag
belongs_to :user
end
17 changes: 17 additions & 0 deletions app/models/signed_tag.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# == Schema Information
#
# 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
#
class SignedTag < Tag
end
16 changes: 16 additions & 0 deletions app/models/signer_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,20 @@ class SignerGroup < Group
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
end
end
1 change: 1 addition & 0 deletions app/models/simple_tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# updated_at :datetime not null
# owner_id :bigint
# tenant_id :bigint not null
# user_id :integer
#
class SimpleTag < Tag
def destroyable?
Expand Down
1 change: 1 addition & 0 deletions app/models/tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# updated_at :datetime not null
# owner_id :bigint
# tenant_id :bigint not null
# user_id :integer
#
class Tag < ApplicationRecord
include AuditableEvents
Expand Down
6 changes: 6 additions & 0 deletions app/models/tenant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@ class Tenant < ApplicationRecord
has_many :custom_groups

has_one :draft_tag
has_one :signature_requested_tag
has_one :signed_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 :signed_by_tags
has_many :simple_tags
has_many :filters
after_create :create_default_objects
Expand Down Expand Up @@ -63,5 +67,7 @@ def create_default_objects
create_admin_group!(name: "admins")
create_signer_group!(name: "signers")
create_draft_tag!(name: "Rozpracované", visible: true)
create_signature_requested_tag!(name: "Na podpis", visible: true)
create_signed_tag!(name: "Podpísané", visible: true)
end
end
1 change: 1 addition & 0 deletions app/models/upvs/delivery_notification_tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# updated_at :datetime not null
# owner_id :bigint
# tenant_id :bigint not null
# user_id :integer
#
class Upvs::DeliveryNotificationTag < ::Tag
def self.find_or_create_for_tenant!(tenant)
Expand Down
3 changes: 3 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ 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 Down
13 changes: 13 additions & 0 deletions db/migrate/20231204132823_generate_tenant_signing_tags.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class GenerateTenantSigningTags < ActiveRecord::Migration[7.0]
def up
Tenant.find_each do |tenant|
tenant.create_signature_requested_tag!(name: "Na podpis", visible: true)
tenant.create_signed_tag!(name: "Podpísané", visible: true)
end
end

def down
SignatureRequestedTag.destroy_all
SignedTag.destroy_all
end
end
26 changes: 26 additions & 0 deletions db/migrate/20231204133308_add_user_id_to_tags.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class AddUserIdToTags < ActiveRecord::Migration[7.0]
def up
add_column :tags, :user_id, :integer

# `name` is needed because there was a `user_id` that is now `owner_id` before
add_foreign_key :tags, :users, name: "tags_to_users"

SignerGroup.includes(:users).find_each do |group|
group.users.each do |user|
SignerGroup.user_added_to_group(group, user)
end
end
end

def down
SignerGroup.includes(:users).find_each do |group|
group.users.each do |user|
SignerGroup.user_removed_from_group(group, user)
end
end

remove_foreign_key :tags, :users, name: "tags_to_users"

remove_column :tags, :user_id, :integer
end
end
17 changes: 17 additions & 0 deletions db/migrate/20231204143711_add_unique_indexes_for_signings.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class AddUniqueIndexesForSignings < ActiveRecord::Migration[7.0]
def up
remove_index :group_memberships, :group_id
add_index :group_memberships, [:group_id, :user_id], unique: true

execute "CREATE UNIQUE INDEX signers_tags ON tags (tenant_id, type, user_id) WHERE (type IN ('SignatureRequestedToTag', 'SignedByTag') AND user_id IS NOT null);"
execute "CREATE UNIQUE INDEX signings_tags ON tags (tenant_id, type) WHERE (type IN ('SignatureRequestedTag', 'SignedTag'));"
end

def down
execute "DROP INDEX signings_tags;"
execute "DROP INDEX signers_tags;"

remove_index :group_memberships, [:group_id, :user_id], unique: true
add_index :group_memberships, :group_id
end
end
8 changes: 6 additions & 2 deletions db/schema.rb

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

0 comments on commit b1274af

Please sign in to comment.