Skip to content

Commit

Permalink
Add: 新建问题推荐相关话题功能
Browse files Browse the repository at this point in the history
  • Loading branch information
huacnlee committed May 17, 2011
1 parent a28db0b commit c9b1ca4
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 0 deletions.
2 changes: 2 additions & 0 deletions app/controllers/asks_controller.rb
Expand Up @@ -53,6 +53,8 @@ def show
@relation_asks = Ask.normal.any_in(:topics => @ask.topics).excludes(:id => @ask.id).limit(10).desc("$natural")
# 被邀请回答的用户
@invites = @ask.ask_invites.includes(:user)
# 推荐话题,如果没有设置话题的话
@suggest_topics = AskSuggestTopic.find_by_ask(@ask)
set_seo_meta(@ask.title)

respond_to do |format|
Expand Down
23 changes: 23 additions & 0 deletions app/models/ask_suggest_topic.rb
@@ -0,0 +1,23 @@
# coding: utf-8
class AskSuggestTopic
include Mongoid::Document

belongs_to :ask
field :topics, :type => Array, :default => []

def self.find_by_ask(ask)
return [] if !ask.topics.blank?
item = self.find_or_initialize_by(:ask_id => ask.id)
return item.topics if !item.topics.blank?

# 生成内容
words = Ask.mmseg_text(ask.title)
topics = Topic.any_in(:name => words)
topics.sort { |a,b| b.followers_count <=> a.followers_count }
topics_array = topics.collect { |t| t.name }

item.topics = topics_array
item.save
return item.topics
end
end
6 changes: 6 additions & 0 deletions app/views/asks/show.html.erb
Expand Up @@ -2,6 +2,7 @@
<script type="text/javascript">
var ask_id = '<%= @ask.id %>';
var ask_redirected = false;

</script>
<% end %>
<%= render 'base' %>
Expand All @@ -16,6 +17,11 @@
Asks.redirected_tip('<%= @r_ask.title %>', '<%= @r_ask.id %>','nr', '<%= @ask.id %>');
</script>
<% end %>
<% if !@suggest_topics.blank? %>
<script type="text/javascript">
Asks.showSuggestTopics(<%=raw @suggest_topics.to_json %>);
</script>
<% end %>
<div class="ask the-ask" id="ask_<%= @ask.id %>">
<div class="topics">
<div class="item_list">
Expand Down
11 changes: 11 additions & 0 deletions public/javascripts/application.js
Expand Up @@ -171,6 +171,17 @@ var App = {
return false;
},

/**
* Get Rails CSRF key and value
* result:
* { key : "", value : "" }
*/
getCSRF : function(){
key = $("meta[name=csrf-param]").attr("content");
value = $("meta[name=csrf-token]").attr("content");
return { key : key, value : value };
},

varsion : function(){
return "1.0";
}
Expand Down
38 changes: 38 additions & 0 deletions public/javascripts/asks.js
Expand Up @@ -530,6 +530,44 @@ var Asks = {
return false;
},

showSuggestTopics : function(topics){
html = '<div id="ask_suggest_topics" class="ask"><div class="container"><label>根据您的问题,我们推荐了一些话题(点击添加):</label>';
for(var i=0;i<topics.length;i++) {
html += '<a href="#" class="topic" onclick="return Asks.addSuggestTopic(this,\''+topics[i]+'\');">'+topics[i]+'</a>';
}
html += '<a class="gray_button small" href="#" onclick="return Asks.closeSuggestTopics();">完成</a>';
html += "</div></div>";
$("#main").before(html);
},

addSuggestTopic : function(el,name){
App.loading();
var csrf = App.getCSRF();
$.ajax({
url : "/asks/"+ask_id+"/update_topic.js?"+ csrf.key + "=" + csrf.value,
data : {
name : name,
add : 1
},
dataType : "text",
type : "post",
success : function(res){
App.loading(false);
Asks.addTopic(name);
$(el).remove();
if($("#ask_suggest_topics a.topic").length == 0){
$("#ask_suggest_topics").remove();
}
}
});
return false;
},

closeSuggestTopics : function(){
$("#ask_suggest_topics").fadeOut("fast",function(){ $(this).remove(); });
return false;
},

version : function(){
}

Expand Down
5 changes: 5 additions & 0 deletions public/stylesheets/application.css
Expand Up @@ -261,6 +261,11 @@ address, blockquote { font-style: normal; padding: 0 0 0 10px; border-left: 3px
padding: 5px 10px; z-index: 101;}
#redirected_tip label { margin-left:70px; }

#ask_suggest_topics { background: #FFC; border-bottom: 3px solid #CC9;
font-size: 12px; padding: 5px 10px; z-index: 101;}
#ask_suggest_topics label { margin-left:70px; }
#ask_suggest_topics a.topic { margin:0 5px; font-weight:bold; }

#ask_invited_users { }
#ask_invited_users .invite { background: #F5F5F5; border-bottom: 1px solid #E0E0E0; float: left; margin: 3px 3px 0 0;
-moz-border-radius: 3px; -webkit-border-radius: 3px;
Expand Down

0 comments on commit c9b1ca4

Please sign in to comment.