Skip to content
This repository has been archived by the owner on May 9, 2022. It is now read-only.

Commit

Permalink
Added tagging support via Acts as Taggable On.
Browse files Browse the repository at this point in the history
Misc view tweaks.
  • Loading branch information
mghaught committed Jan 19, 2010
1 parent 1eeb3a1 commit ba90ca7
Show file tree
Hide file tree
Showing 15 changed files with 118 additions and 56 deletions.
5 changes: 3 additions & 2 deletions app/controllers/posts_controller.rb
Expand Up @@ -18,8 +18,7 @@ def index
end

def search
conditions = params[:q].blank? ? nil : Post.send(:sanitize_sql, ["LOWER(#{Post.table_name}.body) LIKE ?", "%#{params[:q]}%"])
@posts = Post.paginate @@query_options.merge(:conditions => conditions, :page => params[:page], :count => {:select => "#{Post.table_name}.id"}, :order => post_order)
@posts = Post.search(params[:q], :page => params[:page])
@users = User.find(:all, :select => 'distinct *', :conditions => ['id in (?)', @posts.collect(&:user_id).uniq]).index_by(&:id)
render_posts_or_xml :index
end
Expand Down Expand Up @@ -58,6 +57,7 @@ def create
end
@forum = @topic.forum
@post = @topic.posts.build(params[:post])
@post.body_list = params[:post]['body_list'] if params[:post]['body_list']
@post.user = current_user
@post.save!
respond_to do |format|
Expand Down Expand Up @@ -85,6 +85,7 @@ def edit

def update
@post.attributes = params[:post]
@post.body_list = params[:post]['body_list'] if params[:post]['body_list']
@post.save!
rescue ActiveRecord::RecordInvalid
flash[:bad_reply] = 'An error occurred'[:error_occured_message]
Expand Down
7 changes: 6 additions & 1 deletion app/controllers/topics_controller.rb
Expand Up @@ -50,7 +50,12 @@ def create
Topic.transaction do
@topic = @forum.topics.build(params[:topic])
assign_protected
@post = @topic.posts.build(params[:topic])
@post = @topic.posts.build(params[:topic])

# The tags don't seem to set properly with a build call, so we check
# for them manually and set them if found
@post.body_list = params[:topic]['body_list'] if params[:topic]['body_list']

@post.topic = @topic
@post.user = current_user
# only save topic if post is valid so in the view topic will be a new record if there was an error
Expand Down
24 changes: 23 additions & 1 deletion app/models/post.rb
@@ -1,5 +1,7 @@
class Post < ActiveRecord::Base
def self.per_page() 25 end
def self.per_page
10
end

belongs_to :forum
belongs_to :user
Expand All @@ -12,7 +14,27 @@ def self.per_page() 25 end

validates_presence_of :user_id, :body, :topic
attr_accessible :body

acts_as_taggable_on :bodies

def self.search(text, options = {})
query = text.downcase
page = options[:page] || 1

# Using acts_as_taggable_on to return posts that have been tagged with the query string
tagged_posts = tagged_with(query)

conditions = "lower(posts.body) LIKE :query"
conditions << " OR posts.id IN (:post_ids)" unless tagged_posts.empty?

paginate(
:page => page,
:order => "posts.created_at DESC",
:conditions => [conditions, {:query => "%#{query}%", :post_ids => tagged_posts}],
:include => [:topic, :forum]
)
end

def editable_by?(user)
user && (user.id == user_id || user.admin? || user.moderator_of?(forum_id))
end
Expand Down
10 changes: 6 additions & 4 deletions app/models/topic.rb
Expand Up @@ -19,10 +19,12 @@ class Topic < ActiveRecord::Base
belongs_to :replied_by_user, :foreign_key => "replied_by", :class_name => "User"

attr_accessible :title
# to help with the create form

# To help with the create form
attr_accessor :body

def hit!
attr_accessor :body_list

def hit!
self.class.increment_counter :hits, id
end

Expand Down Expand Up @@ -83,7 +85,7 @@ def update_forum_counter_cache
forum_conditions << Post.count(:id, :conditions => {:forum_id => forum_id})
end
# User doesn't have update_posts_count method in SB2, as reported by Ryan
#@voices.each &:update_posts_count if @voices
#@voices.each &:update_posts_count if @voices
Forum.update_all forum_conditions, ['id = ?', forum_id]
@old_forum_id = @voices = nil
end
Expand Down
10 changes: 6 additions & 4 deletions app/views/forums/index.html.erb
Expand Up @@ -8,12 +8,14 @@
<% end %>

<h1 style="margin-top:0;"><%= 'Forums'[:forums_title] %></h1>

<!-- No RSS for now
<p class="subtitle">
<%= feed_icon_tag "Recent Posts"[:recent_posts], all_posts_path(:format => 'rss') %>
<%= '{count} topic(s)'[(count=Topic.count)==1 ? :topic_count : :topics_count, number_with_delimiter(count)] %>,
<%= '{count} post(s)'[(count=Post.count)==1 ? :post_count : :posts_count, number_with_delimiter(count)] %>, <%= '{count} voice(s)'[(count=User.count(:conditions => "posts_count > 0"))==1 ? :voice_count : :voices_count, number_with_delimiter(count)] %>

<%# feed_icon_tag "Recent Posts"[:recent_posts], all_posts_path(:format => 'rss') %>
<%# '{count} topic(s)'[(count=Topic.count)==1 ? :topic_count : :topics_count, number_with_delimiter(count)] %>,
<%# '{count} post(s)'[(count=Post.count)==1 ? :post_count : :posts_count, number_with_delimiter(count)] %>, <%# '{count} voice(s)'[(count=User.count(:conditions => "posts_count > 0"))==1 ? :voice_count : :voices_count, number_with_delimiter(count)] %>
</p>
-->

<table border="0" cellspacing="0" cellpadding="0" class="wide forums">
<tr>
Expand Down
8 changes: 5 additions & 3 deletions app/views/forums/show.html.erb
Expand Up @@ -29,11 +29,13 @@
<%= h @forum.name %>
</h1>

<!-- No RSS for now
<p class="subtitle">
<%= feed_icon_tag @forum.name, forum_posts_path(@forum, :format => :rss) %>
<%= '{count} topic(s)'[(count=@forum.topics.size)==1 ? :topic_count : :topics_count, number_with_delimiter(count)] %>,
<%= '{count} post(s)'[(count=@forum.posts.size)==1 ? :post_count : :posts_count, number_with_delimiter(count)] %>
<%# feed_icon_tag @forum.name, forum_posts_path(@forum, :format => :rss) %>
<%# '{count} topic(s)'[(count=@forum.topics.size)==1 ? :topic_count : :topics_count, number_with_delimiter(count)] %>,
<%# '{count} post(s)'[(count=@forum.posts.size)==1 ? :post_count : :posts_count, number_with_delimiter(count)] %>
</p>
-->

<% if @topics.total_pages > 1 -%>
<% if logged_in? %>
Expand Down
4 changes: 3 additions & 1 deletion app/views/layouts/_head.html.erb
Expand Up @@ -10,7 +10,9 @@
<%= javascript_include_tag "prototype", "effects" %>
<% unless @feed_icons.blank? -%>
<% @feed_icons.each do |feed| -%>
<%= auto_discovery_link_tag :rss, feed[:url], :title => "Subscribe to '#{feed[:title]}'" %>
<!-- No RSS for now
<%# auto_discovery_link_tag :rss, feed[:url], :title => "Subscribe to '#{feed[:title]}'" %>
-->
<% end -%>
<% end -%>
<%= head_extras %>
Expand Down
4 changes: 4 additions & 0 deletions app/views/posts/_edit.html.erb
Expand Up @@ -7,6 +7,10 @@
<tr>
<td rowspan="2" width="70%">
<%= f.text_area :body, :rows => 10, :id => "edit_post_body", :tabindex => 1 %>
<div id='tags'>
<label>Tags</label>
</div>
<%= f.text_field :body_list, :id => "edit_post_body_list", :tabindex => 2 %>
</td>
<td valign="top">

Expand Down
6 changes: 3 additions & 3 deletions app/views/posts/index.html.erb
Expand Up @@ -39,8 +39,8 @@
</td>
<td class="body entry-content">
<p class="topic">
<%= "Topic"[:topic_title] %>: <%= link_to h(post.forum_name), forum_path(post.forum_id) %> /
<%= link_to h(post.topic_title), forum_topic_path(post.forum_id, post.topic_id) %>
<%= "Topic"[:topic_title] %>: <%= link_to h(post.forum.name), forum_path(post.forum_id) %> /
<%= link_to h(post.topic.title), forum_topic_path(post.forum_id, post.topic_id) %>
</p>

<%= post.body_html %>
Expand All @@ -50,4 +50,4 @@
<% end %>
</table>

<%= will_paginate @posts %>
<%= will_paginate @posts %>
9 changes: 6 additions & 3 deletions app/views/posts/monitored.html.erb
Expand Up @@ -12,10 +12,13 @@
<% end -%>

<h1><%= @page_title %></h1>

<!-- No RSS for now
<p class="subtitle">
<%= feed_icon_tag @page_title, monitored_posts_path(:user_id => @user, :format => 'rss') %>
<%= '{count} post(s) found'[(count=@posts.total_entries)==1 ? :post_count_found : :posts_count_found, number_with_delimiter(count)] %>
<%# feed_icon_tag @page_title, monitored_posts_path(:user_id => @user, :format => 'rss') %>
<%# '{count} post(s) found'[(count=@posts.total_entries)==1 ? :post_count_found : :posts_count_found, number_with_delimiter(count)] %>
</p>
-->

<%= will_paginate @posts %>

Expand Down Expand Up @@ -52,4 +55,4 @@
<% end %>
</table>

<%= will_paginate @posts %>
<%= will_paginate @posts %>
45 changes: 25 additions & 20 deletions app/views/topics/_form.html.erb
@@ -1,28 +1,33 @@
<p>
<label for="topic_title"><%= 'Title'[:title_title] %></label><br />
<%= form.text_field :title, :onchange => "/*TopicForm.editNewTitle(this);*/", :class => "primary", :tabindex => 10 %>
<label for="topic_title"><%= 'Title'[:title_title] %></label><br />
<%= form.text_field :title, :onchange => "/*TopicForm.editNewTitle(this);*/", :class => "primary", :tabindex => 10 %>
<% if admin? or current_user.moderator_of?(@topic.forum) %>
<label style="margin-left:1em;">
<%= form.check_box :sticky %> <%= 'Sticky'[:sticky_title] %>
</label>

<label style="margin-left:1em;">
<%= form.check_box :locked %> <%= 'Locked'[:locked_title] %>
</label>

<% end %>
<% if admin? or current_user.moderator_of?(@topic.forum) %>
<label style="margin-left:1em;">
<%= form.check_box :sticky %> <%= 'Sticky'[:sticky_title] %>
</label>

<label style="margin-left:1em;">
<%= form.check_box :locked %> <%= 'Locked'[:locked_title] %>
</label>
<% end %>
</p>

<% if @topic.new_record? %>
<p>
<label for="topic_body"><%= 'Body'[:body_title] %></label><br />
<%= form.text_area :body, :rows => 12, :tabindex => 20 %></p>
<p>
<label for="topic_body"><%= 'Body'[:body_title] %></label><br />
<%= form.text_area :body, :rows => 12, :tabindex => 20 %>
</p>

<p>
<label for='topic_post_tags'>Tags</label><br />
<%= form.text_field :body_list, :tabindex => 30 %>
</p>
<% end %>
<% if admin? and not @topic.new_record? %>
<p id="topic_forum_id">
<label for="topic_forum_id"><%= 'Forum'[:forum_title] %></label><br />
<%= form.select :forum_id, Forum.find(:all, :order => "position").map {|x| [x.name, x.id] } %></p>
</p>
<% end %>
<p id="topic_forum_id">
<label for="topic_forum_id"><%= 'Forum'[:forum_title] %></label><br />
<%= form.select :forum_id, Forum.find(:all, :order => "position").map {|x| [x.name, x.id] } %></p>
</p>
<% end %>
28 changes: 21 additions & 7 deletions app/views/topics/show.html.erb
Expand Up @@ -57,11 +57,13 @@
<% end %>
</h1>

<!-- No RSS for now
<p class="subtitle">
<%= feed_icon_tag @topic.title, forum_topic_path(@forum, @topic, :format => :rss) %>
<%= '{count} post(s)'[(count=@topic.posts.size)==1 ? :post_count : :posts_count, number_with_delimiter(count)] %>,
<%= '{count} voice(s)'[(count=@topic.voices.size)==1 ? :voice_count : :voices_count, number_with_delimiter(count)] %>
<%# feed_icon_tag @topic.title, forum_topic_path(@forum, @topic, :format => :rss) %>
<%# '{count} post(s)'[(count=@topic.posts.size)==1 ? :post_count : :posts_count, number_with_delimiter(count)] %>,
<%# '{count} voice(s)'[(count=@topic.voices.size)==1 ? :voice_count : :voices_count, number_with_delimiter(count)] %>
</p>
-->

<%= will_paginate @posts %>

Expand Down Expand Up @@ -112,7 +114,11 @@
</p>
<% end -%>

<% unless post.body_list.empty? %>
<div id='tags'>
Tags: <%= post.body_list %>
</div>
<% end %>
</td>
<td class="body entry-content" id="post-body-<%= post.id %>">
<!--
Expand Down Expand Up @@ -145,9 +151,17 @@
<% form_for :post, :url => posts_path(:forum_id => @forum, :topic_id => @topic, :page => @topic.last_page) do |f| -%>
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td rowspan="2" width="70%">
<%= f.text_area :body, :rows => 8 %>
<td>
<%= f.text_area :body, :rows => 4 %>
</td>
</tr>
<tr>
<td>
<label>Tags:</label>
<%= f.text_field :body_list %>
</td>
</tr>
<tr>
<td valign="top">


Expand All @@ -167,7 +181,7 @@
</tr>
<tr>
<td valign="bottom" style="padding-bottom:15px;">
<%= submit_tag "Save Reply"[] %><span class="button_or">or <%= link_to_function 'cancel'[], "$('reply').hide()" %></span>
<%= submit_tag "Save Reply"[] %><span class="button_or">or <%= link_to_function 'cancel'[], "$('reply').hide()" %></span>
</td>
</tr>
</table>
Expand Down
2 changes: 1 addition & 1 deletion lib/savage_beast/application_helper.rb
Expand Up @@ -15,7 +15,7 @@ def submit_tag(value = "Save Changes"[], options={} )
=end

def ajax_spinner_for(id, spinner="spinner.gif")
"<img src='/plugin_assets/savage_beast/images/#{spinner}' style='display:none; vertical-align:middle;' id='#{id.to_s}_spinner'> "
"<img src='/plugin_assets/savage-beast/images/#{spinner}' style='display:none; vertical-align:middle;' id='#{id.to_s}_spinner'> "
end

def avatar_for(user, size=32)
Expand Down
4 changes: 2 additions & 2 deletions lib/savage_beast/authentication_system.rb
Expand Up @@ -12,7 +12,7 @@ module SavageBeast::AuthenticationSystem
# this could be a filter for the entire app and keep with it's true meaning, but that
# would just slow things down without any forseeable benefit since we already know
# who is online from the user/session connection
#
#pg
# This is now also used to show which users are online... not at accurate as the
# session based approach, but less code and less overhead.
def update_last_seen_at
Expand Down Expand Up @@ -42,6 +42,6 @@ def logged_in?
end

def admin?
#logged_in? && current_user.admin?
logged_in? && current_user.admin?
end
end
8 changes: 4 additions & 4 deletions public/stylesheets/display.css
Expand Up @@ -257,7 +257,7 @@ ul.talking li span

ul.flat li
{
background:url(/plugin_assets/savage_beast/images/small_circle.gif) no-repeat 5px 6px;
background:url(/plugin_assets/savage-beast/images/small_circle.gif) no-repeat 5px 6px;
padding-left:15px;
}
ul.ways li span
Expand Down Expand Up @@ -520,7 +520,7 @@ label

.photo
{
background:#aaa url(/plugin_assets/savage_beast/images/clearbits/smile.gif) no-repeat 8px 8px;
background:#aaa url(/plugin_assets/savage-beast/images/clearbits/smile.gif) no-repeat 8px 8px;
}

.smallutils
Expand Down Expand Up @@ -848,7 +848,7 @@ p.online {margin-bottom:0;}
img.icon
{
background:#696;
background-image: url(/plugin_assets/savage_beast/images/clearbits/bg_rounded.gif);
background-image: url(/plugin_assets/savage-beast/images/clearbits/bg_rounded.gif);
background-repeat: no-repeat;
vertical-align: bottom;
}
Expand All @@ -867,7 +867,7 @@ img.green { background-color:#0c0;}

div.editbox
{
background:url(/plugin_assets/savage_beast/images/reply_background.png);
background:url(/plugin_assets/savage-beast/images/reply_background.png);
border:2px solid #333;
border-width:2px 0;
position:fixed;
Expand Down

0 comments on commit ba90ca7

Please sign in to comment.