socialforge/app/models/contest_member.rb

60 lines
1.8 KiB
Ruby

class ContestMember < ActiveRecord::Base
attr_accessible :is_collect, :user_id, :contest_id
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]
#validate :validate_role
def deletable?
user != contest.user
end
def role
end
def role=
end
def name
self.user.name
end
# 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
protected
def validate_role
errors.add_on_empty :role if contest_member_roles.empty? && roles.empty?
end
end