Commit 64fabc9a authored by Taico Aerts's avatar Taico Aerts
Browse files

Merge branch 'development' into 'master'

Project Forum Release 2.8.1

See merge request !776
parents 22696700 eede4776
......@@ -12,6 +12,8 @@ module UserConcerns
enum role: { role_company: 0, role_student: 1, role_staff: 2, role_admin: 3 }
enum student_type: { regular: 0, tnw_double_bachelor: 1, tnw_1_bridging: 2, tnw_2_bridging: 3 }
has_many :users_roles, dependent: :delete_all
before_validation :assign_dynamic_attributes
after_initialize :assign_dynamic_attributes
......
......@@ -13,6 +13,12 @@ class ExperimentProject < ApplicationRecord
has_many :images, as: :imageable, dependent: :destroy
accepts_nested_attributes_for :images, allow_destroy: true
belongs_to :original_experiment_project, optional: true, inverse_of: :copies
has_many :copies, inverse_of: :original_experiment_project,
class_name: 'ExperimentProject',
foreign_key: 'original_experiment_project_id',
dependent: :nullify
scope :sort_by_priority, (lambda { |user, course_edition|
subquery = ExperimentPreference.where(user: user)
# TODO: The ExperimentPreference should be linked to the course edition, and this query should be updated accordingly as well
......
......@@ -23,7 +23,11 @@ class Project < ApplicationRecord
has_many :images, as: :imageable, dependent: :destroy
accepts_nested_attributes_for :images, allow_destroy: true
belongs_to :original_project, optional: true, class_name: 'Project'
belongs_to :original_project, optional: true, class_name: 'Project', inverse_of: :copies
has_many :copies, inverse_of: :original_project,
class_name: 'Project',
foreign_key: :original_project_id,
dependent: :nullify
# ===== Groups and members =====
......
class Role < ApplicationRecord
scopify
# This is a fake relation which is emulated by the rolify gem. Adding a dependent breaks deletion.
# rubocop:disable Rails/HasManyOrHasOneDependent
has_many :users_roles
# rubocop:enable Rails/HasManyOrHasOneDependent
# The users_roles table does not have ids, which prevents normal cascading deletion with dependent destroy.
# Instead, we use delete_all, which will drop all the associated records directly from the database.
# This relies on users_role not having any callbacks.
has_many :users_roles, dependent: :delete_all
has_many :users, through: :users_roles
belongs_to :resource,
......
......@@ -62,7 +62,7 @@ class User < ApplicationRecord
has_many :courses, through: :course_editions
has_many :memberships, dependent: :nullify
has_many :memberships, dependent: :destroy
has_many :groups, through: :memberships
has_many :participating_projects,
through: :groups,
......
......@@ -11,9 +11,10 @@ class UserCourseSpecificRole < ApplicationRecord
after_destroy :remove_coach_role
after_save :add_coach_role
# Handle roles assigned to unregistered users
# Rename the belongs_to :user to real_user to make it accessible.
alias real_user user
# Handle roles assigned to unregistered users
def user
return real_user if real_user
......
......@@ -3,4 +3,8 @@ class UsersRole < ApplicationRecord
belongs_to :role
validates :role, uniqueness: { scope: :user }
# IMPORTANT: users_role does not have a primary key (id). Deletion of a role does a destroy_all
# on the associated users_roles, which skips running callbacks. Therefore, do not
# define any callbacks here which should trigger after deletion.
end
##
# Creates the user table.
class DeviseCreateUsers < ActiveRecord::Migration[5.1]
def change
create_table :users do |t|
## Database authenticatable
t.string :email, null: false, default: ''
t.string :encrypted_password, null: false, default: ''
## Recoverable
t.string :reset_password_token
t.datetime :reset_password_sent_at
## Rememberable
t.datetime :remember_created_at
## Trackable
t.integer :sign_in_count, default: 0, null: false
t.datetime :current_sign_in_at
t.datetime :last_sign_in_at
t.string :current_sign_in_ip
t.string :last_sign_in_ip
## Confirmable
t.string :confirmation_token
t.datetime :confirmed_at
t.datetime :confirmation_sent_at
t.string :unconfirmed_email
## Lockable
t.integer :failed_attempts, default: 0, null: false
t.string :unlock_token
t.datetime :locked_at
t.timestamps null: false
end
add_index :users, :email, unique: true
add_index :users, :reset_password_token, unique: true
add_index :users, :confirmation_token, unique: true
add_index :users, :unlock_token, unique: true
end
end
##
# Creates Rolify roles.
class RolifyCreateRoles < ActiveRecord::Migration[5.1]
def change
create_table(:roles) do |t|
t.string :name
t.references :resource, polymorphic: true
t.timestamps
end
create_table(:users_roles, id: false) do |t|
t.references :user
t.references :role
end
add_index(:roles, :name)
add_index(:roles, %i[name resource_type resource_id])
add_index(:users_roles, %i[user_id role_id])
end
end
##
# Add column +first_name+ +last_name+ and +authorization_level+ to User table.
class AddPersonalInformationToUser < ActiveRecord::Migration[5.1]
def change
add_column :users, :first_name, :string
add_column :users, :last_name, :string
add_column :users, :authorization_level, :integer
end
end
##
# Creates CourseConfiguration table.
class CreateCourseConfigurations < ActiveRecord::Migration[5.1]
def change
create_table :course_configurations do |t|
t.integer :course_configurable_id
t.string :course_configurable_type
t.integer :min_group_size, null: false, default: 1
t.integer :max_group_size, null: false, default: 1
t.integer :min_number_of_groups, null: false, default: 1
t.integer :max_number_of_groups, null: false, default: 1
t.timestamps
end
add_index :course_configurations,
%i[course_configurable_id course_configurable_type],
name: 'index_course_configurations_on_course_configurable',
unique: true
end
end
##
# Creates Course table.
class CreateCourses < ActiveRecord::Migration[5.1]
def change
create_table :courses do |t|
t.string :name, null: false
t.timestamps
end
end
end
##
# Creates CourseEdition table.
class CreateCourseEditions < ActiveRecord::Migration[5.1]
def change
create_table :course_editions do |t|
t.string :name
t.references :course, index: true
t.integer :status
t.date :starts_at
t.date :ends_at
t.timestamps
t.text :description
end
end
end
##
# Creates CourseParticipation table.
class CreateCourseParticipations < ActiveRecord::Migration[5.1]
def change
create_table :course_participations do |t|
t.references :user, index: true
t.references :course_edition, index: true
t.integer :status
t.timestamps
end
add_index :course_participations, %i[user_id course_edition_id]
end
end
##
# Creates CrouseParticipationRequest table
class CreateCourseParticipationRequests < ActiveRecord::Migration[5.1]
def change
create_table :course_participation_requests do |t|
t.references :course_participation, index: { unique: true }
t.text :description
t.text :circumstances
t.text :planning
t.timestamps
end
end
end
##
# Creates Company table.
class CreateCompanies < ActiveRecord::Migration[5.1]
def change
create_table :companies do |t|
t.string :name
t.text :description
t.string :email
t.string :phone
t.string :city
t.string :street
t.string :house_number
t.string :postal_code
t.string :country
t.string :website_url
t.string :linkedin_url
t.timestamps
end
end
end
##
# Creates Project table.
class CreateProjects < ActiveRecord::Migration[5.1]
def change
create_table :projects do |t|
t.string :name
t.text :description
t.integer :status
t.references :company, index: true
t.references :course_edition, index: true
t.string :presentation_location
t.datetime :presented_at
t.timestamps
end
end
end
##
# Creates Group table.
class CreateGroups < ActiveRecord::Migration[5.1]
def change
create_table :groups do |t|
t.integer :status
t.references :project_edition, index: true
t.references :project, index: true
t.timestamps
end
end
end
##
# Creates Membership table.
class CreateMemberships < ActiveRecord::Migration[5.1]
def change
create_table :memberships do |t|
t.references :user, index: true
t.references :group, index: true
t.integer :role
t.integer :status
t.timestamps
end
end
end
##
# Changes values to have a default.
class FixEnumDefaults < ActiveRecord::Migration[5.1]
def up
change_column_default :users, :authorization_level, 0
change_column_default :course_editions, :status, 0
change_column_default :course_participations, :status, 0
change_column_default :groups, :status, 0
change_column_default :memberships, :role, 0
change_column_default :projects, :status, 0
end
def down
change_column_default :users, :authorization_level, nil
change_column_default :course_editions, :status, nil
change_column_default :course_participations, :status, nil
change_column_default :groups, :status, nil
change_column_default :memberships, :role, nil
change_column_default :projects, :status, nil
end
end
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment