2016-12-22 10:01:37 +08:00
|
|
|
class ContestMember < ActiveRecord::Base
|
2016-12-22 11:17:13 +08:00
|
|
|
attr_accessible :is_collect, :user_id, :contest_id
|
|
|
|
|
2016-12-22 10:01:37 +08:00
|
|
|
belongs_to :user
|
|
|
|
belongs_to :contest
|
|
|
|
|
|
|
|
belongs_to :principal, :foreign_key => 'user_id'
|
|
|
|
has_many :contest_member_roles, :dependent => :destroy
|
|
|
|
has_many :roles, :through => :contest_member_roles
|
|
|
|
|
|
|
|
validates_presence_of :principal
|
|
|
|
validates_uniqueness_of :user_id, :scope => [:contest_id]
|
2016-12-22 15:08:15 +08:00
|
|
|
#validate :validate_role
|
2016-12-22 10:01:37 +08:00
|
|
|
|
2016-12-24 16:29:10 +08:00
|
|
|
def deletable?
|
|
|
|
user != contest.user
|
|
|
|
end
|
|
|
|
|
2016-12-22 11:17:13 +08:00
|
|
|
def role
|
|
|
|
end
|
|
|
|
|
|
|
|
def role=
|
|
|
|
end
|
|
|
|
|
|
|
|
def name
|
|
|
|
self.user.name
|
|
|
|
end
|
|
|
|
|
2016-12-22 17:13:10 +08:00
|
|
|
# alias :base_role_ids= :role_ids=
|
|
|
|
# def role_ids=(arg)
|
|
|
|
# ids = (arg || []).collect(&:to_i) - [0]
|
|
|
|
#
|
|
|
|
# new_role_ids = ids - role_ids
|
|
|
|
# if (new_role_ids.include?(14) || new_role_ids.include?(13)) && role_ids.include?(15)
|
|
|
|
# contest_member_roles.where("role_id = 15").first.update_column('is_current', 0)
|
|
|
|
# end
|
|
|
|
# # Add new roles
|
|
|
|
# if new_role_ids.include?(14) && new_role_ids.include?(13)
|
|
|
|
# contest_member_roles << ContestMemberRole.new(:role_id => 14)
|
|
|
|
# contest_member_roles << ContestMemberRole.new(:role_id => 15, :is_current => 0)
|
|
|
|
# elsif new_role_ids.include?(13) && new_role_ids.include?(15)
|
|
|
|
# contest_member_roles << ContestMemberRole.new(:role_id => 13)
|
|
|
|
# contest_member_roles << ContestMemberRole.new(:role_id => 15, :is_current => 0)
|
|
|
|
# else
|
|
|
|
# new_role_ids.each {|id| contest_member_roles << ContestMemberRole.new(:role_id => id) }
|
|
|
|
# end
|
|
|
|
# # Remove roles (Rails' #role_ids= will not trigger MemberRole#on_destroy)
|
|
|
|
# member_roles_to_destroy = contest_member_roles.select {|mr| !ids.include?(mr.role_id)}
|
|
|
|
# if member_roles_to_destroy.any?
|
|
|
|
# member_roles_to_destroy.each(&:destroy)
|
|
|
|
# end
|
|
|
|
# end
|
2016-12-22 11:17:13 +08:00
|
|
|
|
2016-12-22 10:01:37 +08:00
|
|
|
protected
|
|
|
|
|
|
|
|
def validate_role
|
|
|
|
errors.add_on_empty :role if contest_member_roles.empty? && roles.empty?
|
|
|
|
end
|
|
|
|
end
|