You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
					
					
						
							112 lines
						
					
					
						
							3.0 KiB
						
					
					
				
			
		
		
	
	
							112 lines
						
					
					
						
							3.0 KiB
						
					
					
				| #coding=utf-8
 | |
| 
 | |
| class AtMessage < ActiveRecord::Base
 | |
|   belongs_to :user
 | |
|   belongs_to :sender, class_name: "User", foreign_key: "sender_id"
 | |
|   attr_accessible :at_message, :container, :viewed, :user_id, :sender_id
 | |
|   belongs_to :at_message, polymorphic: true
 | |
|   belongs_to :container, polymorphic: true
 | |
| 
 | |
|   has_many :message_alls, :class_name => 'MessageAll',:as =>:message, :dependent => :destroy
 | |
|   validates :user_id, :sender_id, :at_message_id, :at_message_type, presence: true
 | |
| 
 | |
|   after_create :add_user_message
 | |
| 
 | |
|   scope :unviewed, ->(type, id){
 | |
|     where(at_message_type: type, at_message_id:id, viewed: false)
 | |
|   }
 | |
| 
 | |
|   def viewed!
 | |
|     update_attribute :viewed, true
 | |
|   end
 | |
| 
 | |
|   def at_valid?
 | |
|     return true if at_message_type == 'Issue'
 | |
|     return true if 'Journal' == at_message_type
 | |
|     return true if 'JournalsForMessage' == at_message_type
 | |
|     return true if 'Message' == at_message_type
 | |
|     false
 | |
|   end
 | |
| 
 | |
|   def add_user_message
 | |
|     if MessageAll.where(message_type: self.class.name,message_id: self.id).empty?
 | |
|       self.message_alls << MessageAll.new(:user_id => self.user_id)
 | |
|     end
 | |
|   end
 | |
| 
 | |
|   def subject
 | |
|     case at_message_type
 | |
|       when "Issue"
 | |
|         "新建问题: " + at_message.subject
 | |
|       when "Journal"
 | |
|         "问题留言: " + at_message.journalized.subject
 | |
|       when 'Message'
 | |
|         if(at_message.topic?)
 | |
|           "发布新帖: "
 | |
|         else
 | |
|           "回复帖子: "
 | |
|         end +  at_message.subject
 | |
|       when 'JournalsForMessage'
 | |
|         if at_message.jour_type == 'Principal'
 | |
|           "留言: 在#{at_message.at_user.show_name}主页中留言"
 | |
|         else
 | |
|           "作业: #{at_message.jour.name} 中留言"
 | |
|         end
 | |
|       else
 | |
|         logger.error "error type: #{at_message_type}"
 | |
|     end
 | |
|   end
 | |
| 
 | |
|   def description
 | |
|     case at_message_type
 | |
|       when "Issue"
 | |
|         at_message.description
 | |
|       when "Journal"
 | |
|         at_message.notes
 | |
|       when 'Message'
 | |
|         at_message.content
 | |
|       when "JournalsForMessage"
 | |
|         at_message.notes
 | |
|       else
 | |
|         logger.error "error type: #{at_message_type}"
 | |
|     end
 | |
|   end
 | |
| 
 | |
|   def author
 | |
|     case at_message_type
 | |
|       when "Issue"
 | |
|         at_message.author
 | |
|       when "Journal"
 | |
|         at_message.user
 | |
|       when 'Message'
 | |
|         at_message.author
 | |
|       when 'JournalsForMessage'
 | |
|         at_message.user
 | |
|       else
 | |
|         logger.error "error type: #{at_message_type}"
 | |
|     end
 | |
|   end
 | |
| 
 | |
|   def url
 | |
|     case at_message_type
 | |
|       when "Issue"
 | |
|         {controller: :issues, action: :show, id: at_message}
 | |
|       when "Journal"
 | |
|         {controller: :issues, action: :show, id: at_message.journalized}
 | |
|       when 'Message'
 | |
|         {controller: :boards, action: :show, project_id: at_message.board.project, id: at_message.board}
 | |
|       when 'JournalsForMessage'
 | |
|         if at_message.jour_type == 'Principal'
 | |
|           {controller: :users, action: :user_messages, id: at_message.at_user}
 | |
|         else
 | |
|           {controller: :homework_common, action: :index, course: at_message.jour.course_id}
 | |
|         end
 | |
| 
 | |
|       else
 | |
|         logger.error "error type: #{at_message_type}"
 | |
|     end
 | |
| 
 | |
|   end
 | |
| 
 | |
| end
 |