From 2e92500a73f45b7fdd9012927b42c378ff7a2dd4 Mon Sep 17 00:00:00 2001 From: Oleksii Kriuchykhin Date: Mon, 15 Nov 2021 21:36:06 +0100 Subject: [PATCH] Fix seeds to work with new UserRoles, fix Templates creation job [SCI-6270] --- app/controllers/api/v1/projects_controller.rb | 2 +- app/models/user_role.rb | 2 ++ app/services/templates_service.rb | 2 +- db/seeds.rb | 4 ++++ 4 files changed, 8 insertions(+), 2 deletions(-) diff --git a/app/controllers/api/v1/projects_controller.rb b/app/controllers/api/v1/projects_controller.rb index eed317d08..994f37963 100644 --- a/app/controllers/api/v1/projects_controller.rb +++ b/app/controllers/api/v1/projects_controller.rb @@ -29,7 +29,7 @@ module Api project = @team.projects.build(project_params.merge!(created_by: current_user)) if project.visible? # set default viewer role for public projects - project.default_public_user_role = UserRole.find_by(name: I18n.t('user_roles.predefined.viewer')) + project.default_public_user_role = UserRole.predefined.find_by(name: I18n.t('user_roles.predefined.viewer')) end project.save! diff --git a/app/models/user_role.rb b/app/models/user_role.rb index 6361f523a..6974e00e6 100644 --- a/app/models/user_role.rb +++ b/app/models/user_role.rb @@ -15,6 +15,8 @@ class UserRole < ApplicationRecord belongs_to :last_modified_by, class_name: 'User', optional: true has_many :user_assignments, dependent: :destroy + scope :predefined, -> { where(predefined: true) } + def self.owner_role new( name: I18n.t('user_roles.predefined.owner'), diff --git a/app/services/templates_service.rb b/app/services/templates_service.rb index 11f2722d2..78a4d20fe 100644 --- a/app/services/templates_service.rb +++ b/app/services/templates_service.rb @@ -21,9 +21,9 @@ class TemplatesService name: Constants::TEMPLATES_PROJECT_NAME, visibility: :visible, template: true, + default_public_user_role: UserRole.predefined.find_by(name: I18n.t('user_roles.predefined.viewer')), created_by: team.created_by ) - tmpl_project.user_projects.create!(user: team.created_by, role: 'owner') end end owner = tmpl_project.user_projects diff --git a/db/seeds.rb b/db/seeds.rb index ae67f6d2d..d1c7fcaf4 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -1,5 +1,9 @@ include UsersGenerator +if ActiveRecord::Base.connection.migration_context.needs_migration? + raise "There are pending migrations. Run 'rails db:migrate' first." +end + MyModuleStatusFlow.ensure_default if User.count.zero?