Migrate User to abstract class

This commit is contained in:
Sebastian Serth
2018-11-25 20:22:43 +01:00
parent 4fd128b31b
commit 141450a840
4 changed files with 36 additions and 36 deletions

View File

@ -1,32 +0,0 @@
module User
extend ActiveSupport::Concern
ROLES = %w(admin teacher)
included do
belongs_to :consumer
has_many :exercises, as: :user
has_many :file_types, as: :user
has_many :submissions, as: :user
has_many :participations, through: :submissions, source: :exercise, as: :user
has_many :user_proxy_exercise_exercises, as: :user
has_many :user_exercise_interventions, as: :user
has_many :interventions, through: :user_exercise_interventions
accepts_nested_attributes_for :user_proxy_exercise_exercises
scope :with_submissions, -> { where('id IN (SELECT user_id FROM submissions)') }
end
ROLES.each do |role|
define_method("#{role}?") { try(:role) == role }
end
[ExternalUser, InternalUser].each do |klass|
define_method("#{klass.name.underscore}?") { is_a?(klass) }
end
def to_s
displayname
end
end

View File

@ -1,5 +1,4 @@
class ExternalUser < ApplicationRecord class ExternalUser < User
include User
validates :consumer_id, presence: true validates :consumer_id, presence: true
validates :external_id, presence: true validates :external_id, presence: true

View File

@ -1,5 +1,4 @@
class InternalUser < ApplicationRecord class InternalUser < User
include User
authenticates_with_sorcery! authenticates_with_sorcery!

34
app/models/user.rb Normal file
View File

@ -0,0 +1,34 @@
class User < ApplicationRecord
self.abstract_class = true
ROLES = %w(admin teacher)
belongs_to :consumer
has_many :exercises, as: :user
has_many :file_types, as: :user
has_many :submissions, as: :user
has_many :participations, through: :submissions, source: :exercise, as: :user
has_many :user_proxy_exercise_exercises, as: :user
has_many :user_exercise_interventions, as: :user
has_many :interventions, through: :user_exercise_interventions
accepts_nested_attributes_for :user_proxy_exercise_exercises
scope :with_submissions, -> { where('id IN (SELECT user_id FROM submissions)') }
ROLES.each do |role|
define_method("#{role}?") { try(:role) == role }
end
def internal_user?
is_a?(InternalUser)
end
def external_user?
is_a?(ExternalUser)
end
def to_s
displayname
end
end