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 16, 2015
1 parent 0c5c067 commit bc8ec5f
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
10 changes: 10 additions & 0 deletions app/models/course/group_user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,15 @@ 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:
unless user.courses.include?(group.course)
errors.add(:user, I18n.translate('activerecord.errors.models.course_group_user.not_enrolled'))
end
end
end
5 changes: 5 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
# available at http://guides.rubyonrails.org/i18n.html.

en:
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(: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 bc8ec5f

Please sign in to comment.