个人主页留言动态的实现

This commit is contained in:
cxt 2015-10-13 16:51:44 +08:00
parent 78e552ddee
commit b9b7f6fa9e
9 changed files with 191 additions and 14 deletions

View File

@ -56,6 +56,7 @@ class JournalsForMessage < ActiveRecord::Base
acts_as_attachable
has_many :acts, :class_name => 'Activity', :as => :act, :dependent => :destroy
has_many :principal_acts, :class_name => 'PrincipalActivity',:as =>:principal_act ,:dependent => :destroy
# 课程动态
has_many :course_acts, :class_name => 'CourseActivity',:as =>:course_act ,:dependent => :destroy
# 消息关联
@ -133,6 +134,11 @@ class JournalsForMessage < ActiveRecord::Base
end
end
#用户动态公共表记录
def act_as_principal_activity
self.principal_acts << PrincipalActivity.new(:user_id => self.id,:principal_id => self.id)
end
def reset_counters!
self.class.reset_counters!(self)
end

View File

@ -0,0 +1,34 @@
class PrincipalActivity < ActiveRecord::Base
attr_accessible :principal_act_id, :principal_act_type, :principal_id, :user_id
#虚拟关联
belongs_to :principal_act ,:polymorphic => true
belongs_to :user
validates :user_id,presence: true
validates :principal_id,presence: true
validates :principal_act_id,presence: true
validates :principal_act_type, presence: true
has_many :user_acts, :class_name => 'UserAcivity',:as =>:act
after_save :add_user_activity
before_destroy :destroy_user_activity
#在个人动态里面增加当前动态
def add_user_activity
user_activity = UserActivity.where("act_type = '#{self.principal_act_type.to_s}' and act_id = '#{self.principal_act_id}'").first
if user_activity
user_activity.save
else
user_activity = UserActivity.new
user_activity.act_id = self.principal_act_id
user_activity.act_type = self.principal_act_type
user_activity.container_type = "Principal"
user_activity.container_id = self.principal_id
user_activity.user_id = self.user_id
user_activity.save
end
end
def destroy_user_activity
user_activity = UserActivity.where("act_type = '#{self.forge_act_type.to_s}' and act_id = '#{self.forge_act_id}'")
user_activity.destroy_all
end
end

View File

@ -114,6 +114,7 @@ class User < Principal
has_many :students_for_courses
#has_many :courses, :through => :students_for_courses, :source => :project
has_many :acts, :class_name => 'Activity', :as => :act, :dependent => :destroy
has_many :principal_acts, :class_name => 'PrincipalActivity',:as =>:principal_act ,:dependent => :destroy
has_many :file_commit, :class_name => 'Attachment', :foreign_key => 'author_id', :conditions => "container_type = 'Project' or container_type = 'Version'"
####
# added by bai
@ -1006,6 +1007,11 @@ class User < Principal
self.acts << Activity.new(:user_id => self.id)
end
#用户动态公共表记录
def act_as_principal_activity
self.principal_acts << PrincipalActivity.new(:user_id => self.id,:principal_id => self.id)
end
# 注册用户的时候消息默认点击时间为用户创建时间
def add_onclick_time
if OnclickTime.where("user_id =?" , self.id).first.nil?

View File

@ -5,7 +5,7 @@
</div>
<div class="homepagePostDes">
<div class="homepagePostTo break_word">
<% if @ctivity.try(:user).try(:realname) == ' ' %>
<% if activity.try(:user).try(:realname) == ' ' %>
<%= link_to activity.try(:user), user_path(activity.user_id), :class => "newsBlue mr15" %>
<% else %>
<%= link_to activity.try(:user).try(:realname), user_path(activity.user_id), :class => "newsBlue mr15" %>
@ -30,7 +30,7 @@
<div class="homepagePostReply">
<div class="topBorder" style="display: <%= count>0 ? 'none': '' %>"></div>
<div class="homepagePostReplyBanner" style="display: <%= count>0 ? '': 'none' %>">
<div class="homepagePostReplyBannerCount" onclick="expand_reply_input('#reply_input_<%= user_activity_id %>');">
<div class="homepagePostReplyBannerCount">
回复(<%= count %>
</div>
<div class="homepagePostReplyBannerTime"></div>
@ -84,7 +84,7 @@
<% end %>
<%= format_time reply.created_on %>
</div>
<div class="homepagePostReplyContent break_word list_style upload_img" id="reply_content_<%= user_activity_id %>">
<div class="homepagePostReplyContent break_word list_style upload_img" style="max-width: 580px;" id="reply_content_<%= user_activity_id %>">
<%= reply.notes.html_safe %></div>
<div class="cl"></div>
</div>

View File

@ -0,0 +1,101 @@
<div class="resources mt10" id="user_activity_<%= user_activity_id%>">
<div class="homepagePostBrief">
<div class="homepagePostPortrait">
<%= link_to image_tag(url_to_avatar(activity.user), :width => "50", :height => "50"), user_path(activity.user_id), :alt => "用户头像" %>
</div>
<div class="homepagePostDes">
<div class="homepagePostTo break_word">
<% if activity.try(:user).try(:realname) == ' ' %>
<%= link_to activity.try(:user), user_path(activity.user_id), :class => "newsBlue mr15" %>
<% else %>
<%= link_to activity.try(:user).try(:realname), user_path(activity.user_id), :class => "newsBlue mr15" %>
<% end %> TO
<% if @user.try(:realname) == ' '%>
<% name = @user.login.to_s %>
<% else %>
<% name = @user.try(:realname).to_s %>
<% end %>
<%= link_to name+""+@user.login.to_s+")的留言", feedback_path(@user, :host=> Setting.host_user), :class => "newsBlue ml15" %>
</div>
<div class="homepagePostTitle break_word list_style upload_img">
<% if activity.parent %>
<%= link_to activity.parent.notes.html_safe, feedback_path(@user, :host=> Setting.host_user), :class => "postGrey" %>
<% else %>
<%= link_to activity.notes.html_safe, feedback_path(@user, :host=> Setting.host_user), :class => "postGrey" %>
<% end %>
</div>
<div class="homepagePostDate">
留言时间:<%= format_time(activity.created_on) %>
</div>
</div>
<div class="cl"></div>
</div>
<% count=fetch_user_leaveWord_reply(activity).count %>
<div class="homepagePostReply">
<div class="topBorder" style="display: <%= count>0 ? 'none': '' %>"></div>
<div class="homepagePostReplyBanner" style="display: <%= count>0 ? '': 'none' %>">
<div class="homepagePostReplyBannerCount" onclick="expand_reply_input('#reply_input_<%= user_activity_id %>');">
回复(<%= count %>
</div>
<div class="homepagePostReplyBannerTime"></div>
<%if count>2 %>
<div class="homepagePostReplyBannerMore">
<a id="reply_btn_<%=user_activity_id%>" onclick="expand_reply('#reply_div_<%= user_activity_id %> li','#reply_btn_<%=user_activity_id%>')" data-count="<%= count %>" data-init="0" class=" replyGrey" href="javascript:void(0)" value="show_help" >
展开更多
</a>
</div>
<% end %>
</div>
<% replies_all_i = 0 %>
<% if count > 0 %>
<div class="" id="reply_div_<%= user_activity_id %>">
<ul>
<% fetch_user_leaveWord_reply(activity).reorder("created_on desc").each do |comment| %>
<% replies_all_i = replies_all_i + 1 %>
<li class="homepagePostReplyContainer" nhname="reply_rec" style="display:<%= replies_all_i > 2 ? 'none' : '' %>">
<div class="homepagePostReplyPortrait">
<%= link_to image_tag(url_to_avatar(comment.user), :width => "33", :height => "33", :class =>"mt8"), user_path(comment.user_id), :alt => "用户头像" %>
</div>
<div class="homepagePostReplyDes">
<div class="homepagePostReplyPublisher">
<% if comment.try(:user).try(:realname) == ' ' %>
<%= link_to comment.try(:user), user_path(comment.user_id), :class => "newsBlue mr10 f14" %>
<% else %>
<%= link_to comment.try(:user).try(:realname), user_path(comment.user_id), :class => "newsBlue mr10 f14" %>
<% end %>
<%= format_time(comment.created_on) %>
</div>
<div class="homepagePostReplyContent break_word list_style upload_img" id="reply_content_<%= user_activity_id %>">
<%= comment.notes.html_safe %></div>
<% end %>
</div>
<div class="cl"></div>
</li>
</ul>
</div>
<% end %>
<div class="homepagePostReplyContainer borderBottomNone minHeight48">
<div class="homepagePostReplyPortrait mr15 imageFuzzy" id="reply_image_<%= user_activity_id%>"><%= link_to image_tag(url_to_avatar(User.current), :width => "33", :height => "33"), :alt => "用户头像" %></div>
<div class="homepagePostReplyInputContainer mb10">
<div nhname='new_message_<%= user_activity_id%>' style="display:none;">
<%= form_for('new_form',:url => {:controller => 'words', :action => 'create_reply', :id => activity.id},:method => "post", :remote => true) do |f|%>
<%= hidden_field_tag 'reference_id', params[:reference_id], :value => activity.id %>
<%= hidden_field_tag 'reference_user_id', params[:reference_user_id], :value => activity.user.id %>
<%= hidden_field_tag 'reference_message_id', params[:reference_message_id], :value => activity.id %>
<%= hidden_field_tag 'show_name',params[:show_name],:value =>true %>
<%= hidden_field_tag 'user_activity_id',params[:user_activity_id],:value =>user_activity_id %>
<textarea placeholder="有问题或有建议,请直接给我留言吧!" style="display: none" nhname='new_message_textarea_<%= user_activity_id%>' name="user_notes"></textarea>
<div nhname='toolbar_container_<%= user_activity_id%>' style="float:left;padding-top:3px; margin-left: 5px;"></div>
<a id="new_message_submit_btn_<%= user_activity_id%>" href="javascript:void(0)" class="blue_n_btn fr" style="display:none;margin-top:6px;">发送</a>
<div class="cl"></div>
<p nhname='contentmsg_<%= user_activity_id%>'></p>
<% end%>
</div>
<div class="cl"></div>
</div>
<div class="cl"></div>
</div>
</div>
</div>

View File

@ -0,0 +1,12 @@
class CreatePrincipalActivities < ActiveRecord::Migration
def change
create_table :principal_activities do |t|
t.integer :user_id
t.integer :principal_id
t.integer :principal_act_id
t.string :principal_act_type
t.timestamps
end
end
end

View File

@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20150917022239) do
ActiveRecord::Schema.define(:version => 20151013081912) do
create_table "activities", :force => true do |t|
t.integer "act_id", :null => false
@ -575,6 +575,8 @@ ActiveRecord::Schema.define(:version => 20150917022239) do
t.integer "viewed"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "secret_key"
t.integer "status"
end
create_table "forums", :force => true do |t|
@ -783,16 +785,6 @@ ActiveRecord::Schema.define(:version => 20150917022239) do
add_index "journal_details", ["journal_id"], :name => "journal_details_journal_id"
create_table "journal_details_copy", :force => true do |t|
t.integer "journal_id", :default => 0, :null => false
t.string "property", :limit => 30, :default => "", :null => false
t.string "prop_key", :limit => 30, :default => "", :null => false
t.text "old_value"
t.text "value"
end
add_index "journal_details_copy", ["journal_id"], :name => "journal_details_journal_id"
create_table "journal_replies", :id => false, :force => true do |t|
t.integer "journal_id"
t.integer "user_id"
@ -911,6 +903,7 @@ ActiveRecord::Schema.define(:version => 20150917022239) do
t.datetime "updated_on", :null => false
t.boolean "locked", :default => false
t.integer "sticky", :default => 0
t.integer "reply_id"
end
add_index "messages", ["author_id"], :name => "index_messages_on_author_id"
@ -1093,6 +1086,15 @@ ActiveRecord::Schema.define(:version => 20150917022239) do
t.datetime "updated_at", :null => false
end
create_table "principal_activities", :force => true do |t|
t.integer "user_id"
t.integer "principal_id"
t.integer "principal_act_id"
t.string "principal_act_type"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "project_infos", :force => true do |t|
t.integer "project_id"
t.integer "user_id"
@ -1376,6 +1378,7 @@ ActiveRecord::Schema.define(:version => 20150917022239) do
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.text "description"
t.string "subject"
end
create_table "taggings", :force => true do |t|

View File

@ -0,0 +1,10 @@
# Read about factories at https://github.com/thoughtbot/factory_girl
FactoryGirl.define do
factory :principal_activity do
user_id 1
principal_id 1
principal_act_id 1
principal_act_type "MyString"
end
end

View File

@ -0,0 +1,5 @@
require 'rails_helper'
RSpec.describe PrincipalActivity, :type => :model do
pending "add some examples to (or delete) #{__FILE__}"
end