-
Notifications
You must be signed in to change notification settings - Fork 78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Course::Group and Course::GroupUser #161
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. empty name validation, no default owner? |
||
|
||
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 |
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 |
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' |
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. course before the name. |
||
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 |
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. course group before user. |
||
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 |
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| | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this can be cleaned up, I'm not sure. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why not transient do
course_user = create(:course_user)
...
end then create the course_group_user last? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried this, but cannot make it work There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I created course_user in after build now There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. factory :course_group_user, class: Course::GroupUser.name do
role :normal
course_group
user
creator
updater
after(:build) do |group_user|
create(:course_user, course: group_user.course_group.course, user: group_user.user)
end
end |
||
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 |
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 |
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 |
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
has_many :users, through: :group_users...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
=_= Haven't noticed this when refactoring