work on forum

This commit is contained in:
albert
2011-03-11 19:24:19 -05:00
parent 7dd345ca75
commit 21cc1cbafa
24 changed files with 343 additions and 1306 deletions

View File

@@ -1,10 +1,10 @@
class ForumPostsController < ApplicationController class ForumPostsController < ApplicationController
respond_to :html, :xml, :json respond_to :html, :xml, :json, :js
before_filter :member_only, :except => [:index, :show] before_filter :member_only, :except => [:index, :show]
rescue_from User::PrivilegeError, :with => "static/access_denied" rescue_from User::PrivilegeError, :with => "static/access_denied"
def new def new
@forum_post = ForumPost.new(:topic_id => params[:topic_id]) @forum_post = ForumPost.new_reply(params)
respond_with(@forum_post) respond_with(@forum_post)
end end
@@ -27,14 +27,14 @@ class ForumPostsController < ApplicationController
def create def create
@forum_post = ForumPost.create(params[:forum_post]) @forum_post = ForumPost.create(params[:forum_post])
respond_with(@forum_post) respond_with(@forum_post, :location => forum_topic_path(@forum_post.topic, :page => @forum_post.topic.last_page))
end end
def update def update
@forum_post = ForumPost.find(params[:id]) @forum_post = ForumPost.find(params[:id])
check_privilege(@forum_post) check_privilege(@forum_post)
@forum_post.update_attributes(params[:forum_post]) @forum_post.update_attributes(params[:forum_post])
respond_with(@forum_post) respond_with(@forum_post, :location => forum_topic_path(@forum_post.topic, :page => @forum_post.topic.last_page))
end end
def destroy def destroy

View File

@@ -1,10 +1,12 @@
class ForumTopicsController < ApplicationController class ForumTopicsController < ApplicationController
respond_to :html, :xml, :json respond_to :html, :xml, :json
before_filter :member_only, :except => [:index, :show] before_filter :member_only, :except => [:index, :show]
before_filter :normalize_search, :only => :index
rescue_from User::PrivilegeError, :with => "static/access_denied" rescue_from User::PrivilegeError, :with => "static/access_denied"
def new def new
@forum_topic = ForumTopic.new @forum_topic = ForumTopic.new
@forum_topic.original_post = ForumPost.new
respond_with(@forum_topic) respond_with(@forum_topic)
end end
@@ -22,6 +24,7 @@ class ForumTopicsController < ApplicationController
def show def show
@forum_topic = ForumTopic.find(params[:id]) @forum_topic = ForumTopic.find(params[:id])
@forum_posts = ForumPost.search(:topic_id_eq => @forum_topic.id).paginate(:page => params[:page])
respond_with(@forum_topic) respond_with(@forum_topic)
end end
@@ -33,6 +36,7 @@ class ForumTopicsController < ApplicationController
def update def update
@forum_topic = ForumTopic.find(params[:id]) @forum_topic = ForumTopic.find(params[:id])
check_privilege(@forum_topic) check_privilege(@forum_topic)
assign_special_attributes(@forum_topic)
@forum_topic.update_attributes(params[:forum_topic]) @forum_topic.update_attributes(params[:forum_topic])
respond_with(@forum_topic) respond_with(@forum_topic)
end end
@@ -45,6 +49,25 @@ class ForumTopicsController < ApplicationController
end end
private private
def assign_special_attributes(forum_topic)
return unless CurrentUser.is_moderator?
forum_topic.is_locked = params[:forum_topic][:is_locked]
forum_topic.is_sticky = params[:forum_topic][:is_sticky]
end
def normalize_search
if params[:title_matches]
params[:search] ||= {}
params[:search][:title_matches] = params.delete(:title_matches)
end
if params[:title]
params[:search] ||= {}
params[:search][:title_eq] = params.delete(:title)
end
end
def check_privilege(forum_topic) def check_privilege(forum_topic)
if !forum_topic.editable_by?(CurrentUser.user) if !forum_topic.editable_by?(CurrentUser.user)
raise User::PrivilegeError raise User::PrivilegeError

View File

@@ -9,6 +9,17 @@ class ForumPost < ActiveRecord::Base
scope :body_matches, lambda {|body| where(["text_index @@ plainto_tsquery(?)", body])} scope :body_matches, lambda {|body| where(["text_index @@ plainto_tsquery(?)", body])}
search_method :body_matches search_method :body_matches
def self.new_reply(params)
if params[:topic_id]
new(:topic_id => params[:topic_id])
elsif params[:post_id]
forum_post = ForumPost.find(params[:post_id])
forum_post.build_response
else
new
end
end
def editable_by?(user) def editable_by?(user)
creator_id == user.id || user.is_moderator? creator_id == user.id || user.is_moderator?
end end
@@ -25,4 +36,10 @@ class ForumPost < ActiveRecord::Base
def initialize_updater def initialize_updater
self.updater_id = CurrentUser.id self.updater_id = CurrentUser.id
end end
def build_response
dup.tap do |x|
x.body = "[quote]\n#{x.body}\n[/quote]\n\n"
end
end
end end

View File

@@ -22,4 +22,12 @@ class ForumTopic < ActiveRecord::Base
def initialize_updater def initialize_updater
self.updater_id = CurrentUser.id self.updater_id = CurrentUser.id
end end
def last_page
(posts.count / Danbooru.config.posts_per_page.to_f).ceil
end
def presenter(forum_posts)
@presenter ||= ForumTopicPresenter.new(self, forum_posts)
end
end end

View File

@@ -0,0 +1,12 @@
class ForumTopicPresenter < Presenter
attr_reader :forum_topic, :forum_posts
def initialize(forum_topic, forum_posts)
@forum_posts = forum_posts
@forum_topic = forum_topic
end
def pagination_html(template)
Paginators::ForumTopic.new(forum_topic, forum_posts).numbered_pagination_html(template)
end
end

View File

@@ -0,0 +1,22 @@
module Paginators
class ForumPost < Base
attr_accessor :forum_posts
def initialize(forum_posts)
@forum_posts = forum_posts
end
protected
def total_pages
forum_posts.total_entries
end
def current_page
forum_posts.current_page
end
def paginated_link(template, page)
template.link_to(page, template.forum_posts_path(:search => template.params[:search], :page => page))
end
end
end

View File

@@ -0,0 +1,23 @@
module Paginators
class ForumTopic < Base
attr_accessor :forum_topic, :forum_posts
def initialize(forum_topic, forum_posts)
@forum_topic = forum_topic
@forum_posts = forum_posts
end
protected
def total_pages
forum_posts.total_pages
end
def current_page
forum_posts.current_page
end
def paginated_link(template, page)
template.link_to(page, template.forum_topic_path(forum_topic, :page => page))
end
end
end

View File

@@ -0,0 +1 @@
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

View File

@@ -0,0 +1,23 @@
<article data-forum-post-id="<%= forum_post.id %>">
<div class="author">
<h1><%= link_to forum_post.creator.name, user_path(forum_post.creator_id) %></h1>
<p>
<time datetime="<%= forum_post.created_at %>">
<%= time_ago_in_words(forum_post.created_at) %> ago
</time>
</p>
</div>
<div class="content">
<div>
<%= format_text(forum_post.body) %>
</div>
<menu>
<li><%= link_to "Reply", new_forum_post_path(:post_id => forum_post.id) %></li>
<% if CurrentUser.user.is_janitor? || CurrentUser.user.id == forum_post.creator_id %>
<li><%= link_to "Delete", forum_post_path(forum_post.id), :confirm => "Do you really want to delete this post?", :method => :delete, :remote => true %></li>
<li><%= link_to "Edit", edit_forum_post_path(forum_post.id) %></li>
<% end %>
</menu>
</div>
<div class="clearfix"></div>
</article>

View File

@@ -0,0 +1,6 @@
<div class="list-of-forum-posts">
<% forum_posts.each do |forum_post| %>
<%= render :partial => "forum_posts/forum_post", :locals => {:forum_post => forum_post} %>
<% end %>
</div>

View File

@@ -0,0 +1 @@
$("article[data-forum-post-id=<%= @forum_post.id %>]").remove();

View File

@@ -1,6 +1,10 @@
<h1>Edit Post</h1> <div id="c-forum-topics">
<div id="a-edit">
<h1>Edit Forum Post</h1>
<%= simple_form_for(@forum_post) do |f| %> <%= simple_form_for(@forum_post) do |f| %>
<%= f.input :body %> <%= f.input :body %>
<%= f.button :submit %> <%= f.button :submit, "Submit" %>
<% end %> <% end %>
</div>
</div>

View File

@@ -1,7 +1,11 @@
<h1>New Post</h1> <div id="c-forum-topics">
<div id="a-new">
<h1>New Forum Post</h1>
<%= simple_form_for(@forum_post) do |f| %> <%= simple_form_for(@forum_post) do |f| %>
<%= f.input :topic_id, :as => :hidden %> <%= f.input :topic_id, :as => :hidden %>
<%= f.input :body %> <%= f.input :body %>
<%= f.button :submit %> <%= f.button :submit, "Submit" %>
<% end %> <% end %>
</div>
</div>

View File

@@ -0,0 +1,3 @@
<div class="paginator">
<%= @forum_topic.presenter(@forum_posts).pagination_html(self) %>
</div>

View File

@@ -1,7 +1,6 @@
<div> <div id="search">
<%= simple_form_for @search do |f| %> <%= form_tag(forum_posts_path, :method => :get) do %>
<%= f.input :title_matches %> <%= text_field_tag "title_matches" %>
<%= f.input :creator_name_equals %> <%= submit_tag "Search" %>
<%= f.button :submit %>
<% end %> <% end %>
</div> </div>

View File

@@ -0,0 +1,17 @@
<% content_for(:secondary_links) do %>
<menu>
<li><%= link_to "Listing", forum_topics_path %></li>
<li><%= link_to "New", new_forum_topic_path %></li>
<li><%= link_to "Help", wiki_pages_path(:title => "help:forum") %></li>
<% if @forum_topic %>
<li>|</li>
<li><%= link_to "Reply", new_forum_post_path(:topic_id => @forum_topic.id) %></li>
<% unless @forum_topic.new_record? %>
<li><%= link_to "Edit", edit_forum_topic_path(@forum_topic) %></li>
<% if CurrentUser.is_moderator? %>
<li><%= link_to "Delete", forum_topic_path(@forum_topic), :remote => true, :method => :delete, :confirm => "Do you want to delete this forum topic?" %></li>
<% end %>
<% end %>
<% end %>
</menu>
<% end %>

View File

@@ -1,12 +1,19 @@
<h1>Edit Topic</h1> <div id="c-forum-topics">
<div id="a-edit">
<h1>Edit Forum Topic</h1>
<%= simple_form_for(@forum_topic) do |f| %> <%= simple_form_for(@forum_topic) do |f| %>
<%= f.input :title %> <%= f.input :title %>
<%= f.simple_fields_for :original_post do |pf| %> <%= f.simple_fields_for :original_post do |pf| %>
<%= text_field_tag "forum_topic[original_post_attributes][topic_id]", @forum_topic.id %> <%= hidden_field_tag "forum_topic[original_post_attributes][topic_id]", @forum_topic.id %>
<%= pf.input :body %> <%= pf.input :body %>
<% end %> <% end %>
<%= f.button :submit %> <%= f.input :is_sticky %>
<% end %> <%= f.input :is_locked %>
<%= f.button :submit, "Submit" %>
<% end %>
</div>
</div>

View File

@@ -2,11 +2,11 @@
<%= render "search" %> <%= render "search" %>
<table> <table width="100%">
<thead> <thead>
<tr> <tr>
<th>Creator</th>
<th>Title</th> <th>Title</th>
<th>Creator</th>
<th>Updated by</th> <th>Updated by</th>
<th>Updated at</th> <th>Updated at</th>
</tr> </tr>
@@ -14,11 +14,13 @@
<tbody> <tbody>
<% @forum_topics.each do |topic| %> <% @forum_topics.each do |topic| %>
<tr> <tr>
<td><%= topic.creator.name %></td>
<td><%= topic.title %></td> <td><%= topic.title %></td>
<td><%= topic.creator.name %></td>
<td><%= topic.updater.name %></td> <td><%= topic.updater.name %></td>
<td><%= compact_time topic.updated_at %></td> <td><%= compact_time topic.updated_at %></td>
</tr> </tr>
<% end %> <% end %>
</tbody> </tbody>
</table> </table>
<%= render "secondary_links" %>

View File

@@ -1,11 +1,23 @@
<h1>New Topic</h1> <div id="c-forum-topics">
<div id="a-new">
<h1>New Forum Topic</h1>
<%= simple_form_for(@forum_topic) do |f| %> <div id="form-content">
<%= f.input :title %> <%= simple_form_for(@forum_topic) do |f| %>
<%= f.input :title %>
<%= f.simple_fields_for :original_post do |pf| %> <%= f.simple_fields_for :original_post do |pf| %>
<%= pf.input :body %> <%= pf.input :body %>
<% end %> <% end %>
<%= f.button :submit %> <%= f.button :submit, "Submit" %>
<% end %> <% end %>
</div>
<div id="dtext-help">
<%= render "dtext/help" %>
</div>
</div>
</div>
<%= render "secondary_links" %>

View File

@@ -0,0 +1,18 @@
<div id="c-forum-topics">
<div id="a-show">
<h1 id="forum-topic-title">
<%= @forum_topic.title %>
<% if @forum_topic.is_locked? %>
<span class="info">(locked)</span>
<% end %>
<% if @forum_topic.is_sticky? %>
<span class="info">(sticky)</span>
<% end %>
</h1>
<%= render :partial => "forum_posts/listing", :locals => {:forum_posts => @forum_posts} %>
<%= render "paginator" %>
</div>
</div>
<%= render "secondary_links" %>

View File

@@ -2,7 +2,7 @@ module Danbooru
class CustomConfiguration < Configuration class CustomConfiguration < Configuration
# Define your custom overloads here # Define your custom overloads here
def app_name def app_name
"f" "Lorem"
end end
def posts_per_page def posts_per_page

File diff suppressed because one or more lines are too long

View File

@@ -134,6 +134,9 @@ table.striped tbody tr:hover {
table.striped tr.even { table.striped tr.even {
background-color: #EEE; } background-color: #EEE; }
div#search {
margin-bottom: 1em; }
div#notice { div#notice {
margin: 1em; margin: 1em;
padding: 1em; padding: 1em;
@@ -259,11 +262,11 @@ div#c-pools div#a-edit ul.ui-sortable {
/*** Comments ***/ /*** Comments ***/
div.comments-for-post div.list-of-comments article { div.comments-for-post div.list-of-comments article {
margin-bottom: 2em; } margin-bottom: 2em; }
div.comments-for-post div.list-of-comments article span.comment-score {
color: #CCC; }
div.comments-for-post div.list-of-comments article div.author { div.comments-for-post div.list-of-comments article div.author {
width: 20%; width: 20%;
float: left; } float: left; }
div.comments-for-post div.list-of-comments article div.author h1 {
font-size: 1.5em; }
div.comments-for-post div.list-of-comments article div.content { div.comments-for-post div.list-of-comments article div.content {
margin-left: 2em; margin-left: 2em;
width: 40em; width: 40em;
@@ -436,3 +439,32 @@ div#c-uploads div#a-new div#upload-guide-notice {
margin-bottom: 2em; } margin-bottom: 2em; }
div#c-uploads div#a-new fieldset.ratings label { div#c-uploads div#a-new fieldset.ratings label {
display: inline; } display: inline; }
/*** Forum ***/
div.list-of-forum-posts article {
margin-bottom: 3em; }
div.list-of-forum-posts article div.author {
width: 20%;
float: left; }
div.list-of-forum-posts article div.author h1 {
font-size: 1.5em; }
div.list-of-forum-posts article div.content {
margin-left: 2em;
width: 40em;
float: left; }
div.list-of-forum-posts article div.content menu {
margin-top: 0.5em; }
div#c-forum-topics div#a-show h1#forum-topic-title {
font-size: 2em; }
div#c-forum-topics span.info {
color: #AAA; }
div#c-forum-topics textarea {
width: 400px;
height: 40em; }
div#c-forum-topics div#form-content {
float: left;
width: 500px; }
div#c-forum-topics div#dtext-help {
float: left;
width: 400px; }

View File

@@ -179,6 +179,10 @@ table.striped {
} }
} }
div#search {
margin-bottom: 1em;
}
div#notice { div#notice {
margin: 1em; margin: 1em;
padding: 1em; padding: 1em;
@@ -394,13 +398,13 @@ div.comments-for-post {
article { article {
margin-bottom: 2em; margin-bottom: 2em;
span.comment-score {
color: #CCC;
}
div.author { div.author {
width: 20%; width: 20%;
float: left; float: left;
h1 {
font-size: 1.5em;
}
} }
div.content { div.content {
@@ -747,3 +751,58 @@ div#c-uploads {
} }
} }
} }
/*** Forum ***/
div.list-of-forum-posts {
article {
margin-bottom: 3em;
div.author {
width: 20%;
float: left;
h1 {
font-size: 1.5em;
}
}
div.content {
margin-left: 2em;
width: 40em;
float: left;
menu {
margin-top: 0.5em;
}
}
}
}
div#c-forum-topics {
div#a-show {
h1#forum-topic-title {
font-size: 2em;
}
}
span.info {
color: #AAA;
}
textarea {
width: 400px;
height: 40em;
}
div#form-content {
float: left;
width: 500px;
}
div#dtext-help {
float: left;
width: 400px;
}
}