Skip to content

Commit

Permalink
Validate user add to the group are in the same course with group
Browse files Browse the repository at this point in the history
  • Loading branch information
allenwq committed May 27, 2015
1 parent 0aef40b commit e6d5c26
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 0 deletions.
7 changes: 7 additions & 0 deletions app/models/course/group_user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ class Course::GroupUser < ActiveRecord::Base

enum role: { normal: 0, manager: 1 }

validate :user_and_group_in_same_course

belongs_to :user, inverse_of: :course_group_users
belongs_to :course_group, class_name: Course::Group.name, inverse_of: :group_users

Expand All @@ -16,4 +18,9 @@ class Course::GroupUser < ActiveRecord::Base
def set_defaults
self.role ||= :normal
end

def user_and_group_in_same_course #:nodoc:
return if user.courses.include?(group.course)
errors.add(:user, I18n.t('activerecord.errors.models.course_group_user.not_enrolled'))
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
en:
activerecord:
errors:
models:
course_group_user:
not_enrolled: 'user must be enrolled in same course as group'
6 changes: 6 additions & 0 deletions spec/factories/course_group_users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
creator
updater

after(:build) do |group_user|
course = group_user.group.course
user = group_user.user
create(:course_user, course: course, user: user) unless user.courses.include?(course)
end

factory :course_group_student
factory :course_group_manager do
role :manager
Expand Down
10 changes: 10 additions & 0 deletions spec/models/course/group_user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,14 @@
RSpec.describe Course::GroupUser, type: :model do
it { is_expected.to belong_to(:user).inverse_of(:course_group_users) }
it { is_expected.to belong_to(:course_group).inverse_of(:group_users) }

let!(:instance) { create(:instance) }
with_tenant(:instance) do
subject { build(:course_group_user) }

context 'when user is not enrolled in group\'s course' do
before { subject.user.course_users.delete_all }
it { is_expected.not_to be_valid }
end
end
end

0 comments on commit e6d5c26

Please sign in to comment.