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 23, 2015
1 parent c92583a commit 64eb1fb
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
9 changes: 9 additions & 0 deletions app/models/course/group_user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,14 @@ class Course::GroupUser < ActiveRecord::Base
belongs_to :group, class_name: Course::Group.name, inverse_of: :group_users,
foreign_key: :course_group_id

validate :user_and_group_in_same_course

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

private

def user_and_group_in_same_course #:nodoc:
return if user.courses.include?(group.course)
errors.add(:user, I18n.translate('activerecord.errors.models.course_group_user.not_enrolled'))
end
end
7 changes: 7 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ en:
'yes': 'Yes'
'no': 'No'
delete: 'Delete'

activerecord:
errors:
models:
course_group_user:
not_enrolled: 'user must be enrolled in same course as group'

layout:
coursemology: 'Coursemology'
navbar:
Expand Down
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 @@
association :group, factory: :course_group
role :normal

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, parent: :course_group_user
factory :course_group_manager, parent: :course_group_user 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(: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 64eb1fb

Please sign in to comment.