合并user_score属性

This commit is contained in:
yanxd 2014-06-06 16:17:27 +08:00
parent 174c5556a0
commit db61ecc255
3 changed files with 22 additions and 38 deletions

View File

@ -137,7 +137,7 @@ class User < Principal
has_one :user_extensions,:dependent => :destroy has_one :user_extensions,:dependent => :destroy
## end ## end
default_scope -> { includes(:user_extensions, :user_score) } # default_scope -> { includes(:user_extensions, :user_score) }
scope :teacher, -> { scope :teacher, -> {
joins(:user_extensions).where('user_extensions.identity = ?', UserExtensions::TEACHER) joins(:user_extensions).where('user_extensions.identity = ?', UserExtensions::TEACHER)
} }
@ -201,6 +201,7 @@ class User < Principal
# ====================================================================== # ======================================================================
# 集中处理 User 扩展表为空的问题 # 集中处理 User 扩展表为空的问题
# 合并 user_score 属性
validate :valid_user_extensions validate :valid_user_extensions
after_save :save_user_extensions after_save :save_user_extensions
@ -208,21 +209,32 @@ class User < Principal
self.user_extensions ||= UserExtensions.new self.user_extensions ||= UserExtensions.new
end end
def user_score_attr
self.user_score ||= UserScore.new
end
alias_method :ori_respond_to?, :respond_to?
def respond_to?(m, include_private = false) def respond_to?(m, include_private = false)
flag = UserExtensions.instance_methods.include? m.to_sym flag = false
if flag flag = ori_respond_to? m.to_sym unless flag
flag flag = UserExtensions.new.respond_to? m.to_sym unless flag
else flag = UserScore.new.respond_to? m.to_sym unless flag
super
end flag
end end
def method_missing m, *args, &block def method_missing m, *args, &block
if extensions.respond_to? m.to_sym if extensions.respond_to? m.to_sym
self.class.send :define_method, m.to_sym, *args do self.class.send(:define_method, "_meta_#{m}".to_sym) do |*args, &block|
self.extensions.__send__ m.to_sym, *args self.extensions.__send__ m.to_sym, *args
end end
__send__ m.to_sym, *args, &block __send__ "_meta_#{m}".to_sym, *args, &block
elsif user_score_attr.respond_to? m.to_sym
self.class.send(:define_method, "_meta_#{m}".to_sym) do |*args, &block|
self.user_score_attr.__send__ m.to_sym, *args
end
__send__ "_meta_#{m}".to_sym, *args, &block
else else
super super
@ -241,6 +253,7 @@ class User < Principal
def save_user_extensions def save_user_extensions
self.extensions.save self.extensions.save
self.user_score_attr.save
end end
# 集中处理 User 扩展表为空的问题 < end # 集中处理 User 扩展表为空的问题 < end
# ====================================================================== # ======================================================================

View File

@ -1,9 +1,7 @@
<!-- added by bai --> <!-- added by bai -->
<% unless @user.id == 1%>
<% messages_count = @user.messages.count %> <% messages_count = @user.messages.count %>
<% messages_score = messages_count * 0.05%> <% messages_score = messages_count * 0.05%>
<% finall_messages_score = messages_score %> <% finall_messages_score = messages_score %>
<% end %>
<% journals_count = @user.journals.count %> <% journals_count = @user.journals.count %>
<% journals_score = journals_count * 0.1 %> <% journals_score = journals_count * 0.1 %>
@ -73,31 +71,6 @@
</div> </div>
<!-- <div class="inf_user_image">
<ul>
<li>
<%= link_to l(:label_user_score), {:controller => 'users', :action => 'score_index', :remote => true} %> :
<%= format("%.2f" , finall_user_score).to_f %>
</li>
<li>
<%= link_to l(:label_user_score_of_topic), {:controller => 'users', :action => 'topic_score_index', :remote => true} %> :
<%= format("%.2f" , finall_messages_score).to_f %>
</li>
<li>
<%= link_to l(:label_user_score_of_project), {:controller => 'users', :action => 'project_score_index', :remote => true} %> :
<%= format("%.2f" , finall_user_project_score).to_f %>
</li>
<li>
<%= link_to l(:label_user_score_of_activity), {:controller => 'users', :action => 'activity_score_index', :remote => true} %> :
<%= format("%.2f" , finall_activity_score).to_f %>
</li>
<li>
<%= link_to l(:label_user_score_of_influence), {:controller => 'users', :action => 'influence_score_index', :remote => true} %>
<%= format("%.2f" , finall_influence_score).to_f %>
</li>
</ul>
</div> -->
<div id="show_score_detail", style="padding-left: 6px"> <div id="show_score_detail", style="padding-left: 6px">
<%= render :partial => 'users/score_index', :locals => {:index => 0 } %> <%= render :partial => 'users/score_index', :locals => {:index => 0 } %>
</div> </div>
<!-- end -->

View File

@ -1,5 +1,3 @@
<!-- added by bai -->
$('#ajax-modal').html('<%= escape_javascript(render :partial => 'users/show_score') %>'); $('#ajax-modal').html('<%= escape_javascript(render :partial => 'users/show_score') %>');
showModal('ajax-modal', '400px'); showModal('ajax-modal', '400px');
$('#ajax-modal').addClass('new-watcher'); $('#ajax-modal').addClass('new-watcher');
<!-- end -->