Skip to content

Commit

Permalink
Validates the uniqueness of group name and set default values for group
Browse files Browse the repository at this point in the history
  • Loading branch information
allenwq committed May 29, 2015
1 parent 9768a4e commit 460cc62
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions app/models/course/group.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
class Course::Group < ActiveRecord::Base
stampable

before_validation :set_defaults, if: :new_record?

validates :name, uniqueness: { scope: :course_id }

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

0 comments on commit 460cc62

Please sign in to comment.