重构user.find

This commit is contained in:
alan 2016-03-14 10:24:50 +08:00
parent 1f5c90185a
commit ce6d63ccaf
2 changed files with 45 additions and 46 deletions

View File

@ -1064,6 +1064,17 @@ class User < Principal
anonymous_user
end
# refactor User model find function,
# return anonymous user when can not find user id = user_id
def self.find user_id
user = User.new
begin
user = super
rescue
user = self.anonymous
end
user
end
# Salts all existing unsalted passwords
# It changes password storage scheme from SHA1(password) to SHA1(salt + SHA1(password))
# This method is used in the SaltPasswords migration and is to be kept as is

View File

@ -1,31 +1,31 @@
<% unless all_results.nil? || all_results.empty?%>
<% all_results.each do |item|%>
<% case item.type %>
<% when 'user'%>
<ul class="searchContent">
<li class="fl"></li>
<li class="fl searchContentDes">
<ul class="fl">
<li class="f16 mb5"><a href="<%= user_path(item.id)%>" class="fontGrey3 fl">
<%= item.try(:highlight).try(:login) ? item.highlight.login[0].html_safe : item.login %>
<%if item.firstname.present? || item.lastname.present?%>
(<%= item.try(:highlight).try(:lastname) ? item.highlight.lastname[0].html_safe : item.lastname%>
<%= item.try(:highlight).try(:firstname) ? item.highlight.firstname[0].html_safe : item.firstname %>)
<% all_results.each do |item|%>
<% case item.type %>
<% when 'user'%>
<ul class="searchContent">
<li class="fl"></li>
<li class="fl searchContentDes">
<ul class="fl">
<li class="f16 mb5"><a href="<%= user_path(item.id)%>" class="fontGrey3 fl">
<%= item.try(:highlight).try(:login) ? item.highlight.login[0].html_safe : item.login %>
<%if item.firstname.present? || item.lastname.present?%>
(<%= item.try(:highlight).try(:lastname) ? item.highlight.lastname[0].html_safe : item.lastname%>
<%= item.try(:highlight).try(:firstname) ? item.highlight.firstname[0].html_safe : item.firstname %>)
<%end %>
</a>
<div class="mt5 fl"><%= image_tag("search_icon_03.png", :width=>"8", :height=>"16" ,:class=>"fl") %><span class="searchTag"><%= get_user_identity(User.find(item.id).user_extensions.identity) %></span></div>
<div class="cl"></div>
</li>
<li class="fontGrey3 mb5"><%= User.find(item.id).user_extensions && User.find(item.id).user_extensions.brief_introduction.present? ? User.find(item.id).user_extensions.brief_introduction : '这位童鞋很懒,什么也没有留下~'%></li>
<li class="f12 fontGrey2">
<span class="mr30">加入时间:<%= format_date( User.find(item.id).created_on)%></span><span class="mr30">
</a>
<div class="mt5 fl"><%= image_tag("search_icon_03.png", :width=>"8", :height=>"16" ,:class=>"fl") %><span class="searchTag"><%= get_user_identity(User.find(item.id).user_extensions.identity) %></span></div>
<div class="cl"></div>
</li>
<li class="fontGrey3 mb5"><%= User.find(item.id).user_extensions && User.find(item.id).user_extensions.brief_introduction.present? ? User.find(item.id).user_extensions.brief_introduction : '这位童鞋很懒,什么也没有留下~'%></li>
<li class="f12 fontGrey2">
<span class="mr30">加入时间:<%= format_date( User.find(item.id).created_on)%></span><span class="mr30">
最后登陆时间:<%= format_date( User.find(item.id).last_login_on)%></span><span class="mr30">
<%= User.find(item.id).user_extensions.occupation.present? ? '单位:'+User.find(item.id).user_extensions.occupation : ''%></span></li>
</ul>
</li>
<div class="cl"></div>
</ul>
</li>
<div class="cl"></div>
</ul>
<% when 'course'%>
<% when 'course'%>
<ul class="searchContent">
<li class="fl">
</li>
@ -38,24 +38,15 @@
</li>
<li class="fontGrey3 mb5"><%= item.try(:highlight).try(:description) ? item.highlight.description[0].html_safe : item.description %></li>
<li class="f12 fontGrey2">
<%= course_teacher = User.where("id = ?", item.tea_id).first if item.tea_id? %>
<% if course_teacher.nil? %>
<span class="mr30">教师:无</span>
<span class="mr30">授课时间:<%= item.time.to_s + item.term%></span>
<span class="mr30">更新时间:<%= format_date(item.updated_at)%></span>
<span class="mr30">单位:无</span>
<%= else %>
<span class="mr30">教师:<%= User.find(item.tea_id).realname %></span>
<span class="mr30">授课时间:<%= item.time.to_s + item.term%></span>
<span class="mr30">更新时间:<%= format_date(item.updated_at)%></span>
<span class="mr30"><%= course_teacher.user_extensions.occupation.present? ? '单位:'+ course_teacher.user_extensions.occupation : ''%></span>
<% end %>
</li>
<span class="mr30">教师:<%= User.find(item.tea_id).realname %></span>
<span class="mr30">授课时间:<%= item.time.to_s + item.term%></span>
<span class="mr30">更新时间:<%= format_date(item.updated_at)%></span>
<span class="mr30"><%= User.find(item.tea_id).user_extensions.occupation.present? ? '单位:'+User.find(item.tea_id).user_extensions.occupation : ''%></span></li>
</ul>
</li>
<div class="cl"></div>
</ul>
<% when 'attachment'%>
<% when 'attachment'%>
<ul class="searchContent">
<li class="fl">
</li>
@ -65,8 +56,7 @@
<div class="mt5 fl"><%= image_tag("search_icon_03.png", :width=>"8", :height=>"16" ,:class=>"fl") %><span class="searchTag">资源</span></div>
<div class="cl"></div>
</li>
<%= item_author = User.where("id = ?", item.author_id).first if item.author_id? %>
<li class="f12 fontGrey2"><span class="mr30">发布者:<%= item_author.nil? ? '无' : item_author.login%><%= item_author.realname unless item_author.nil? %></span>
<li class="f12 fontGrey2"><span class="mr30">发布者:<%= User.find(item.author_id).login%><%= User.find(item.author_id).realname%></span>
<!--<span class="mr30">职称:<%#= get_technical_title User.find(item.author_id) %></span>-->
<span class="mr30">发布时间:<%= format_date(item.created_on)%></span>
<span class="mr30">下载次数:<%= item.downloads%>次</span>
@ -75,7 +65,7 @@
</li>
<div class="cl"></div>
</ul>
<% when 'project'%>
<% when 'project'%>
<ul class="searchContent">
<li class="fl">
</li>
@ -86,8 +76,7 @@
<div class="cl"></div>
</li>
<li class="fontGrey3 mb5"><%= item.try(:highlight).try(:description) ? item.highlight.description[0].html_safe : item.description%></li>
<%= project_admin = User.where("id = ?", item.user_id).first if item.user_id? %>
<li class="f12 fontGrey2"><span class="mr30">管理人员:<%= project_admin.nil? ? '无' : project_admin.login %></span><span class="mr30">更新时间:<%= date_format_local( Project.find(item.id).updated_on) %></span></li>
<li class="f12 fontGrey2"><span class="mr30">管理人员:<%= item.user_id ? User.find(item.user_id).login : '无' %></span><span class="mr30">更新时间:<%= date_format_local( Project.find(item.id).updated_on) %></span></li>
</ul>
</li>
<div class="cl"></div>
@ -103,14 +92,13 @@
<div class="cl"></div>
</li>
<li class="fontGrey3 mb5"><%= item.try(:highlight).try(:content) ? item.highlight.content[0].html_safe : item.content.html_safe%></li>
<%= memo_author = User.where("id = ?", item.author_id).first if item.author_id? %>
<li class="f12 fontGrey2"><span class="mr30">发帖人:<%= memo_author.nil? ? '无' : memo_author.login %></span><span class="mr30">更新时间:<%= format_date( item.updated_at) %></span></li>
<li class="f12 fontGrey2"><span class="mr30">发帖人:<%= item.author_id ? User.find(item.author_id).login : '无' %></span><span class="mr30">更新时间:<%= format_date( item.updated_at) %></span></li>
</ul>
</li>
<div class="cl"></div>
</ul>
<%end %>
<% end %>
<%end %>
<% end %>
<div class="pageRoll">
<%= paginate all_results,:params => {:controller => 'welcome', :action => 'search',:search_type=>'all'}%>
</div>