diff --git a/app/models/stash_engine/funder.rb b/app/models/stash_engine/funder.rb index 0e993c08a0..ce3fe8af2d 100644 --- a/app/models/stash_engine/funder.rb +++ b/app/models/stash_engine/funder.rb @@ -19,7 +19,7 @@ module StashEngine class Funder < ApplicationRecord self.table_name = 'stash_engine_funders' belongs_to :ror_org, class_name: 'StashEngine::RorOrg', primary_key: 'ror_id', foreign_key: 'ror_id', optional: true - has_many :roles, class_name: 'StashEngine::Role', as: :role_object + has_many :roles, class_name: 'StashEngine::Role', as: :role_object, dependent: :destroy has_many :users, through: :roles enum payment_plan: { diff --git a/app/models/stash_engine/funder_role.rb b/app/models/stash_engine/funder_role.rb deleted file mode 100644 index 17a8b77657..0000000000 --- a/app/models/stash_engine/funder_role.rb +++ /dev/null @@ -1,24 +0,0 @@ -# == Schema Information -# -# Table name: stash_engine_funder_roles -# -# id :bigint not null, primary key -# funder_name :string(191) -# role :string(191) -# created_at :datetime not null -# updated_at :datetime not null -# funder_id :string(191) -# user_id :bigint -# -# Indexes -# -# index_stash_engine_funder_roles_on_user_id (user_id) -# -module StashEngine - class FunderRole < ApplicationRecord - self.table_name = 'stash_engine_funder_roles' - belongs_to :user - - scope :admins, -> { where(role: 'admin') } - end -end diff --git a/app/models/stash_engine/journal.rb b/app/models/stash_engine/journal.rb index 9612dc56b2..9fd55f97a8 100644 --- a/app/models/stash_engine/journal.rb +++ b/app/models/stash_engine/journal.rb @@ -31,7 +31,7 @@ class Journal < ApplicationRecord # validates :issn, uniqueness: { case_sensitive: false } has_many :issns, -> { order(created_at: :asc) }, class_name: 'StashEngine::JournalIssn', dependent: :destroy has_many :alternate_titles, class_name: 'StashEngine::JournalTitle', dependent: :destroy - has_many :roles, class_name: 'StashEngine::Role', as: :role_object + has_many :roles, class_name: 'StashEngine::Role', as: :role_object, dependent: :destroy has_many :users, through: :roles belongs_to :sponsor, class_name: 'StashEngine::JournalOrganization', optional: true diff --git a/app/models/stash_engine/journal_organization.rb b/app/models/stash_engine/journal_organization.rb index 855e0d39b7..991766bb6d 100644 --- a/app/models/stash_engine/journal_organization.rb +++ b/app/models/stash_engine/journal_organization.rb @@ -15,7 +15,7 @@ class JournalOrganization < ApplicationRecord self.table_name = 'stash_engine_journal_organizations' has_many :children, class_name: 'JournalOrganization', primary_key: :id, foreign_key: :parent_org_id, inverse_of: :parent_org belongs_to :parent_org, class_name: 'JournalOrganization', optional: true, inverse_of: :children - has_many :roles, class_name: 'StashEngine::Role', as: :role_object + has_many :roles, class_name: 'StashEngine::Role', as: :role_object, dependent: :destroy has_many :users, through: :roles scope :has_children, -> { distinct.joins(:children) } diff --git a/app/models/stash_engine/journal_role.rb b/app/models/stash_engine/journal_role.rb deleted file mode 100644 index 8db0a6642f..0000000000 --- a/app/models/stash_engine/journal_role.rb +++ /dev/null @@ -1,24 +0,0 @@ -# == Schema Information -# -# Table name: stash_engine_journal_roles -# -# id :integer not null, primary key -# journal_id :integer -# user_id :integer -# role :string(191) -# created_at :datetime -# updated_at :datetime -# journal_organization_id :integer -# -module StashEngine - class JournalRole < ApplicationRecord - self.table_name = 'stash_engine_journal_roles' - belongs_to :user - # must contain either a journal or a journal_organization - belongs_to :journal, optional: true - belongs_to :journal_organization, optional: true - - scope :admins, -> { where(role: 'admin') } - scope :org_admins, -> { where(role: 'org_admin') } - end -end diff --git a/app/models/stash_engine/tenant.rb b/app/models/stash_engine/tenant.rb index a0e2f70858..1568128ebf 100644 --- a/app/models/stash_engine/tenant.rb +++ b/app/models/stash_engine/tenant.rb @@ -29,7 +29,7 @@ class Tenant < ApplicationRecord has_many :sponsored, class_name: 'Tenant', primary_key: :id, foreign_key: :sponsor_id, inverse_of: :sponsor has_many :tenant_ror_orgs, class_name: 'StashEngine::TenantRorOrg', dependent: :destroy has_many :ror_orgs, class_name: 'StashEngine::RorOrg', through: :tenant_ror_orgs - has_many :roles, class_name: 'StashEngine::Role', as: :role_object + has_many :roles, class_name: 'StashEngine::Role', as: :role_object, dependent: :destroy has_many :users, through: :roles enum payment_plan: { diff --git a/db/migrate/20240425204927_add_roles_table.rb b/db/migrate/20240425204927_add_roles_table.rb index 9b172e6c85..35b2f013d1 100644 --- a/db/migrate/20240425204927_add_roles_table.rb +++ b/db/migrate/20240425204927_add_roles_table.rb @@ -1,4 +1,22 @@ class AddRolesTable < ActiveRecord::Migration[6.1] + JOURNAL_ROLE = <<-SQL.freeze + INSERT INTO stash_engine_roles (user_id, role, role_object_type, role_object_id) SELECT user_id, role, 'StashEngine::Journal', journal_id FROM stash_engine_journal_roles WHERE `stash_engine_journal_roles`.`role` = 'admin' + SQL + JOURNAL_ORG_ROLE = <<-SQL.freeze + INSERT INTO stash_engine_roles (user_id, role, role_object_type, role_object_id) SELECT user_id, 'admin', 'StashEngine::JournalOrganization', journal_organization_id FROM stash_engine_journal_roles WHERE `stash_engine_journal_roles`.`role` = 'org_admin' + SQL + FUNDER_ROLE = <<-SQL.freeze + INSERT INTO stash_engine_roles (user_id, role, role_object_type, role_object_id) SELECT user_id, 'admin', 'StashEngine::Funder', 1 FROM stash_engine_funder_roles + SQL + ROLE_JOURNAL = <<-SQL.freeze + INSERT INTO stash_engine_journal_roles (user_id, role, journal_id) SELECT user_id, 'admin', role_object_id FROM stash_engine_roles where role_object_type = 'StashEngine::Journal' + SQL + ROLE_JOURNAL_ORG = <<-SQL.freeze + INSERT INTO stash_engine_journal_roles (user_id, role, journal_organization_id) SELECT user_id, 'org_admin', role_object_id FROM stash_engine_roles where role_object_type = 'StashEngine::JournalOrganization' + SQL + ROLE_FUNDER = <<-SQL.freeze + INSERT INTO stash_engine_funder_roles (user_id, role, funder_name, funder_id) SELECT user_id, 'admin', 'Chan Zuckerberg Initiative', 'https://ror.org/02qenvm24' FROM stash_engine_roles where role_object_type = 'StashEngine::Funder' + SQL def change create_table :stash_engine_funders do |t| t.string :name @@ -22,13 +40,9 @@ def change add_foreign_key :stash_engine_roles, :stash_engine_users, column: :user_id reversible do |dir| dir.up do - StashEngine::JournalRole.all.each do |r| - StashEngine::Role.create(user_id: r.user_id, role: 'admin', role_object: StashEngine::Journal.find(r.journal_id)) if r.journal_id.present? && StashEngine::Journal.exists?(r.journal_id) - StashEngine::Role.create(user_id: r.user_id, role: 'admin', role_object: StashEngine::JournalOrganization.find(r.journal_organization_id)) if r.journal_organization_id.present? && StashEngine::JournalOrganization.exists?(r.journal_organization_id) - end - StashEngine::FunderRole.all.each do |r| - StashEngine::Role.create(user_id: r.user_id, role: r.role, role_object: StashEngine::Funder.find_by(name: r.funder_name)) if StashEngine::Funder.find_by(name: r.funder_name).present? - end + execute JOURNAL_ROLE + execute JOURNAL_ORG_ROLE + execute FUNDER_ROLE StashEngine::User.where("role != 'user'").each do |u| case u.role when 'limited_curator' @@ -45,16 +59,9 @@ def change end end dir.down do - StashEngine::Role.journal_roles.each do |r| - StashEngine::JournalRole.create(user_id: r.user_id, role: 'admin', journal_id: r.role_object_id) - end - StashEngine::Role.journal_org_roles.each do |r| - StashEngine::JournalRole.create(user_id: r.user_id, role: 'org_admin', journal_organization_id: r.role_object_id) - end - StashEngine::Role.funder_roles.each do |r| - funder = StashEngine::Funder.find(r.role_object_id) - StashEngine::FunderRole.create(user_id: r.user_id, role: 'admin', funder_name: funder.name, funder_id: funder.ror_id) - end + execute ROLE_JOURNAL + execute ROLE_JOURNAL_ORG + execute ROLE_FUNDER StashEngine::Role.system_roles.admin.each {|r| StashEngine::User.find(r.user_id).update(role: 'limited_curator') if StashEngine::User.exists?(r.user_id)} StashEngine::Role.system_roles.curator.each {|r| StashEngine::User.find(r.user_id).update(role: 'curator') if StashEngine::User.exists?(r.user_id)} StashEngine::Role.superuser.each {|r| StashEngine::User.find(r.user_id).update(role: 'superuser') if StashEngine::User.exists?(r.user_id)} diff --git a/db/migrate/20240625190621_drop_journal_column.rb b/db/migrate/20240625190621_drop_journal_column.rb new file mode 100644 index 0000000000..64cc96369e --- /dev/null +++ b/db/migrate/20240625190621_drop_journal_column.rb @@ -0,0 +1,5 @@ +class DropJournalColumn < ActiveRecord::Migration[7.0] + def change + remove_column :stash_engine_journals, :issn, :string + end +end