Always create a default study group for new consumers

This commit is contained in:
Sebastian Serth
2022-09-20 16:21:49 +02:00
committed by Sebastian Serth
parent 521626275f
commit cb1b163b30
3 changed files with 20 additions and 1 deletions

View File

@ -12,6 +12,13 @@ class Consumer < ApplicationRecord
validates :oauth_key, presence: true, uniqueness: true validates :oauth_key, presence: true, uniqueness: true
validates :oauth_secret, presence: true validates :oauth_secret, presence: true
after_create :generate_internal_study_group
def generate_internal_study_group
StudyGroup.create!(consumer: self, name: "Default Study Group for #{name}", external_id: nil)
end
private :generate_internal_study_group
def to_s def to_s
name name
end end

View File

@ -5,10 +5,17 @@ class StudyGroupPolicy < AdminOnlyPolicy
admin? || teacher? admin? || teacher?
end end
%i[show? destroy? edit? update? stream_la?].each do |action| %i[show? edit? update? stream_la?].each do |action|
define_method(action) { admin? || (@user.teacher? && @record.present? && @user.study_groups.exists?(@record.id)) } define_method(action) { admin? || (@user.teacher? && @record.present? && @user.study_groups.exists?(@record.id)) }
end end
def destroy?
# A default study group should not get deleted without the consumer
return no_one if @record.external_id.blank?
admin? || teacher_in_study_group?
end
class Scope < Scope class Scope < Scope
def resolve def resolve
if @user.admin? if @user.admin?

View File

@ -4,6 +4,7 @@ require 'rails_helper'
describe Consumer do describe Consumer do
let(:consumer) { described_class.create } let(:consumer) { described_class.create }
let(:valid_consumer) { create(:consumer) }
it 'validates the presence of a name' do it 'validates the presence of a name' do
expect(consumer.errors[:name]).to be_present expect(consumer.errors[:name]).to be_present
@ -21,4 +22,8 @@ describe Consumer do
it 'validates the presence of an OAuth secret' do it 'validates the presence of an OAuth secret' do
expect(consumer.errors[:oauth_secret]).to be_present expect(consumer.errors[:oauth_secret]).to be_present
end end
it 'creates a study group after creation' do
expect(valid_consumer.study_groups.count).to eq 1
end
end end