-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #161 from coursemology-collab/hanlin/groups-model
Add Course::Group and Course::GroupUser
- Loading branch information
Showing
14 changed files
with
166 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
class Course::Group < ActiveRecord::Base | ||
stampable | ||
|
||
after_initialize :set_defaults, if: :new_record? | ||
before_validation :set_defaults, if: :new_record? | ||
|
||
belongs_to :course, inverse_of: :groups | ||
has_many :group_users, inverse_of: :course_group, dependent: :destroy, | ||
class_name: Course::GroupUser.name, foreign_key: :course_group_id | ||
has_many :users, through: :group_users | ||
|
||
private | ||
|
||
# Set default values | ||
def set_defaults | ||
return unless creator && creator.courses.include?(course) && group_users.empty? | ||
group_users.build(user: creator, role: :manager, creator: creator, updater: updater) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
class Course::GroupUser < ActiveRecord::Base | ||
stampable | ||
|
||
after_initialize :set_defaults, if: :new_record? | ||
|
||
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 | ||
|
||
alias_method :group, :course_group | ||
|
||
private | ||
|
||
# Set default values | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
config/locales/en/activerecord/errors/models/course_group_user.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
class CreateCourseGroups < ActiveRecord::Migration | ||
def change | ||
create_table :course_groups do |t| | ||
t.belongs_to :course, null: false | ||
t.string :name, null: false, default: '' | ||
|
||
t.userstamps null: false, foreign_key: { references: :users } | ||
t.timestamps null: false | ||
|
||
t.index [:course_id, :name], unique: true | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
class CreateCourseGroupUsers < ActiveRecord::Migration | ||
def change | ||
create_table :course_group_users do |t| | ||
t.belongs_to :course_group, null: false | ||
t.belongs_to :user, null: false | ||
t.integer :role, null: false | ||
|
||
t.userstamps null: false, foreign_key: { references: :users } | ||
t.timestamps null: false | ||
|
||
t.index [:user_id, :course_group_id], unique: true | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
FactoryGirl.define do | ||
factory :course_group_user, class: Course::GroupUser.name do | ||
course_group | ||
user | ||
role :normal | ||
creator | ||
updater | ||
|
||
after(:build) do |group_user| | ||
course = group_user.course_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 | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
FactoryGirl.define do | ||
factory :course_group, class: Course::Group.name do | ||
course | ||
sequence(:name) { |n| "Group #{n}" } | ||
creator | ||
updater | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe Course::Group, type: :model do | ||
it { is_expected.to belong_to(:course).inverse_of(:groups) } | ||
it { is_expected.to have_many(:group_users).inverse_of(:course_group).dependent(:destroy) } | ||
it { is_expected.to have_many(:users).through(:group_users) } | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
require 'rails_helper' | ||
|
||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters