Permalink
Browse files

added paginate , rspec for models, and removing (posting topics and f…

…orums) by admin
  • Loading branch information...
1 parent 1a09e44 commit b6826f5d80f3d1e32010cd8f2a1f0871c214a8d0 @chrisZingel chrisZingel committed Apr 5, 2011
View
@@ -13,10 +13,11 @@ begin
gem.summary = "Ruby on Rails forum engine for RefineryCMS"
gem.description = "A port of forem to refinerycms"
gem.email = "tim@uckun.com"
- gem.authors = ["Tim Uckun", "Ryan Bigg"]
- gem.files = Dir["{lib}/**/*", "{app}/**/*", "{public}/**/*", "{config}/**/*"]
+ gem.authors = ["Tim Uckun", "Chris Zingel", "Ryan Bigg"]
+ gem.files = Dir["{lib}/**/*", "{app}/**/*", "{public}/**/*", "{config}/**/*","{spec}/**/*"]
gem.add_dependency 'refinerycms', '>= 0.9.9'
- gem.add_dependency 'simple_form'
+ gem.add_dependency 'simple_form'
+ gem.add_dependency "will_paginate", "~> 3.0.pre2"
end
Jeweler::GemcutterTasks.new
rescue
@@ -4,8 +4,23 @@ class ForumsController < Forem::ApplicationController
#crudify Forem::Forum,:title_attribute => :title
before_filter :authenticate_forem_admin, :only => [:new, :create]
- def index
- @forums = Forem::Forum.all
+
+ def destroy
+ @forum = Forem::Forum.find(params[:id])
+ if current_user&&@forum.destroy
+ flash[:notice] = t("forem.forum.deleted")
+ else
+ flash[:error] = t("forem.forum.cannot_delete")
+ end
+ respond_to do |format|
+ format.html { redirect_to forums_path }
+ format.js
+ end
+ end
+
+
+ def index
+ @forums = Forem::Forum.paginate( :page => params[:page], :per_page => 3)
end
def show
@@ -4,7 +4,7 @@ class PostsController < Forem::ApplicationController
before_filter :find_topic
def index
- @posts = find_topic.posts
+ @posts = find_topic.posts.paginate( :page => params[:page], :per_page => 3)
end
def new
@@ -22,17 +22,21 @@ def create
render :action => "new"
end
end
+
- def destroy
+ def destroy
@post = @topic.posts.find(params[:id])
- if current_user.login == @post.user.login
- @post.destroy
+ if current_user&&@post.destroy
+ # Recalculate topic and post counts
+ Forem::Service.recalulate_post_topic_counts(@topic.forum)
flash[:notice] = t("forem.post.deleted")
else
flash[:error] = t("forem.post.cannot_delete")
end
-
- redirect_to [@topic.forum, @topic]
+ respond_to do |format|
+ format.html { redirect_to [@topic.forum, @topic] }
+ format.js
+ end
end
private
@@ -4,6 +4,7 @@ class TopicsController < Forem::ApplicationController
before_filter :find_forum
def index
+ @topics = find_forum.topics.paginate( :page => params[:page], :per_page => 3)
# @forums = Forem::Forum.all
end
@@ -19,6 +20,24 @@ def new
@post=@topic.posts.build
render :layout => request.xhr? ? false : true
end
+
+ def destroy
+ @topic = @forum.topics.find(params[:id])
+ if current_user&&@topic.destroy
+ # Recalculate topic and post counts
+ Forem::Service.recalulate_post_topic_counts(@topic.forum)
+ flash[:notice] = t("forem.topic.deleted")
+ else
+ flash[:error] = t("forem.topic.cannot_delete")
+ end
+ respond_to do |format|
+ format.html { redirect_to forum_topics_path(@topic.forum)}
+ format.js
+ end
+ end
+
+
+
def create
#TODO - I am currently allowing anonymous creation of topics (probably need to change at at later stage)
View
@@ -1,7 +1,7 @@
module Forem
class Post < ActiveRecord::Base
set_table_name 'forem_posts'
- belongs_to :topic
+ belongs_to :topic
belongs_to :user
belongs_to :reply_to, :class_name => "Post"
@@ -11,13 +11,12 @@ class Post < ActiveRecord::Base
validates :body, :presence => true
+
class << self
def recent_post
order(:created_at).last
end
end
-
-
end
end
@@ -1,5 +1,9 @@
module Forem
- class Service
+ class Service
+ def self.recalulate_post_topic_counts(forum)
+ Forum.update_all("posts_count = #{forum.posts.count},topics_count=#{forum.topics.count} ", :id => forum.id)
+ end
+
def self.create_post(topic_id, post_params, user_id)
topic = Topic.find(topic_id)
raise "Invalid topic" and return if topic.nil?
@@ -1,6 +1,34 @@
-<a name='forum-#{forum.id}'></a>
-<div id='forum_<%= forum.id %>' class='forum'>
- <div class='description'>
- <%= forum.description %>
- </div>
-</div>
+<table width="100%" cellspacing="1" cellpadding="2" border="0" class="forum">
+<%=content_tag :tbody do%>
+ <tr>
+ <th nowrap="nowrap" height="25" colspan="2">&nbsp;Forum&nbsp;</th>
+ <th width="50" nowrap="nowrap">&nbsp;Topics&nbsp;</th>
+ <th width="50" nowrap="nowrap">&nbsp;Posts&nbsp;</th>
+ <th nowrap="nowrap">&nbsp;Last Post&nbsp;</th>
+ </tr>
+
+ <% @forums.each do |forum| %>
+ <%=content_tag :tr,:class=>"#{cycle('odd', 'even')}",:id=>"row_#{forum.id}" do %>
+ <td valign="middle" height="50" align="center"><%=image_tag "email.png",:alt=>"New posts" %></td>
+ <td width="100%" height="50">
+ <h2><%= link_to forum.title, forum_topics_path(forum) %></h2>
+ <h3><%= forum.description %> </h3>
+ </td>
+ <td valign="middle" height="50" align="center"><%= forum.topics_count %></td>
+ <td valign="middle" height="50" align="center"><%= forum.posts_count %></td>
+ <td valign="middle" nowrap="nowrap" height="50" align="center">
+ <%if @recent_post= forum.posts.recent_post%>
+ <%=time_ago_in_words(@recent_post.created_at)%> ago
+ <br>
+ <%=@recent_post.name.nil? || @recent_post.name.empty? ? link_to("Anonymous",forum_topic_posts_path(forum, @recent_post.topic)) : link_to(@recent_post.name,forum_topic_posts_path(forum, @recent_post.topic)) %>
+ <%=link_to(image_tag("icon_latest_reply.gif"),forum_topic_posts_path(forum, @recent_post.topic))%>
+ <%else%>
+ "no posts"
+ <%end%>
+ <%= button_to(t("forem.forum.delete"), forum_path(forum), :id => forum.id, :method => :delete, :remote => true, :confirm => t("forem.forum.delete_question"), :class => "button delete") if current_user%>
+ </td>
+ <%end%>
+ <% end%>
+<%end%>
+ <%= will_paginate @forums %>
+</table>
@@ -0,0 +1,8 @@
+/* Insert a notice between the last comment and the comment form */
+$("#table-notice").html('<div class="flash"><%= escape_javascript(flash[:notice]) + escape_javascript(flash[:error])%></div>');
+$("#table-notice .flash").fadeOut(4000);
+
+/* Eliminate the comment by fading it out --unless there is an error */
+<%if flash[:error].nil?||flash[:error].empty? %>
+$('tr#row_<%= @forum.id %>').fadeOut();
+<%end%>
@@ -1,43 +1,11 @@
-<table width="100%" cellspacing="1" cellpadding="2" border="0" class="forum">
-<%=content_tag :tbody do%>
-<tr>
- <th nowrap="nowrap" height="25" colspan="2">&nbsp;Forum&nbsp;</th>
- <th width="50" nowrap="nowrap">&nbsp;Topics&nbsp;</th>
- <th width="50" nowrap="nowrap">&nbsp;Posts&nbsp;</th>
- <th nowrap="nowrap">&nbsp;Last Post&nbsp;</th>
-</tr>
-
-
- <% @forums.each do |forum| %>
-
-<%=content_tag :tr,:class=>"#{cycle('odd', 'even')}" do %>
-<!-- logic to determine whether messages are new -->
- <td valign="middle" height="50" align="center"><%=image_tag "email.png",:alt=>"New posts" %></td>
- <td width="100%" height="50">
- <h2><%= link_to forum.title, forum_topics_path(forum) %></h2>
- <h3><%= forum.description %> </h3>
- </td>
- <td valign="middle" height="50" align="center"><%= forum.topics_count %></td>
- <td valign="middle" height="50" align="center"><%= forum.posts_count %></td>
- <td valign="middle" nowrap="nowrap" height="50" align="center">
- <%if @recent_post= forum.posts.recent_post%>
-
- <%=time_ago_in_words(@recent_post.created_at)%> ago
- <br>
- <%=@recent_post.name.nil? || @recent_post.name.empty? ? link_to("Anonymous",forum_topic_posts_path(forum, @recent_post.topic)) : link_to(@recent_post.name,forum_topic_posts_path(forum, @recent_post.topic)) %>
- <%=link_to(image_tag("icon_latest_reply.gif"),forum_topic_posts_path(forum, @recent_post.topic))%>
- <%else%>
- "no posts"
- <%end%>
- </td>
-<!-- Last post was TIME AGO by NAME in TOPIC -->
-<%end%>
-<% end%>
-
+<div id="table-notice"> </div>
+<%= render :partial => "forum" %>
+<% content_for :stylesheets, stylesheet_link_tag('refinerycms-forum') %>
+<% content_for :javascript_libraries do %>
+<%=javascript_include_tag "jquery#{"-min" if Rails.env.production?}" %>
+<%= javascript_include_tag 'rails.js' %>
<%end%>
-</table>
-<% content_for :stylesheets, stylesheet_link_tag('refinerycms-forum') %>
@@ -0,0 +1,8 @@
+/* Insert a notice between the last comment and the comment form */
+$("#table-notice").html('<div class="flash"><%= escape_javascript(flash[:notice]) + escape_javascript(flash[:error])%></div>');
+$("#table-notice .flash").fadeOut(4000);
+
+/* Eliminate the comment by fading it out --unless there is an error */
+<%if flash[:error].nil?||flash[:error].empty? %>
+$('tr#row_<%= @post.id %>').fadeOut();
+<%end%>
@@ -1,8 +1,11 @@
+<div id="table-notice"> </div>
+
<div id='forum_<%= @topic.id %>' class='topic'>
<div class="topic_name"><%= @topic.subject %></div>
<div class="forum_header-links">
<%= link_to t("forem.post.new"), new_forum_topic_post_path(@topic.forum, @topic) , {:rel=>"facebox", :class=>"button1"} %>
+ <%= link_to "topics", forum_topics_path(@topic.forum) %>
</div>
<table width="100%" cellspacing="1" cellpadding="2" border="0" class="forum">
@@ -11,22 +14,23 @@
<th nowrap="nowrap" height="25" >&nbsp;Message&nbsp;</th>
<th width="50" nowrap="nowrap">&nbsp;Author&nbsp;</th>
</tr>
- <% @topic.posts.each do |post| %>
- <%=content_tag :tr,:class=>"#{cycle('odd', 'even')}" do %>
+ <% @posts.each do |post| %>
+ <%=content_tag :tr,:class=>"#{cycle('odd', 'even')}",:id=>"row_#{post.id}" do %>
<!-- logic to determine whether messages are new -->
<td width="100%" height="50">
<%=post.body %>
</td>
<td valign="middle" nowrap="nowrap" height="50" align="center">
- <h3><%=post.name.nil?|| post.name.empty? ? "Anonymous" : post.name %> </h3>
- <p> <%=time_ago_in_words(post.created_at)%> ago </p>
- <%=image_tag("users.png")%>
-
+ <h3><%=post.name.nil?|| post.name.empty? ? "Anonymous" : post.name %> </h3>
+ <p> <%=time_ago_in_words(post.created_at)%> ago </p>
+ <%= button_to(t("forem.post.delete"),
+ forum_topic_post_path(post.topic.forum.id,post.topic.id,post.id), :id => post.id, :method => :delete, :remote => true, :confirm => t("forem.post.delete_question"), :class => "button delete") if current_user%>
</td>
<%end%>
<%end%>
<%end%>
-</table>
+</table>
+<%= will_paginate @posts %>
</div>
<% content_for :stylesheets do %>
@@ -35,7 +39,8 @@
<%end%>
<% content_for :javascript_libraries do %>
- <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript"></script>
+ <%=javascript_include_tag "jquery#{"-min" if Rails.env.production?}" %>
+ <%= javascript_include_tag 'rails.js' %>
<%end%>
<% content_for :after_javascript_libraries do %>
@@ -46,4 +51,4 @@
loadingImage : '/facebox/loading.gif',
closeImage : '/facebox/closelabel.png'})})
</script>
-<% end %>
+<% end %>
@@ -0,0 +1,8 @@
+/* Insert a notice between the last comment and the comment form */
+$("#table-notice").html('<div class="flash"><%= escape_javascript(flash[:notice]) + escape_javascript(flash[:error])%></div>');
+$("#table-notice .flash").fadeOut(4000);
+
+/* Eliminate the comment by fading it out --unless there is an error */
+<%if flash[:error].nil?||flash[:error].empty? %>
+$('tr#row_<%= @topic.id %>').fadeOut();
+<%end%>
@@ -1,9 +1,11 @@
+<div id="table-notice"> </div>
+
<div id='forum_<%= @forum.id %>' class='forum'>
<div class="forum_name"><%= @forum.title %></div>
<div class='forum_description'> <%= @forum.description %> </div>
<div class="forum_header-links">
- <%= link_to t("forem.topic.create"), new_forum_topic_path(@forum) , {:rel=>"facebox", :class=>"button1"} %>
+ <%= link_to t("forem.topic.create"), new_forum_topic_path(@forum) , {:rel=>"facebox", :class=>"button1"} %>
</div>
<table width="100%" cellspacing="1" cellpadding="2" border="0" class="forum">
@@ -15,7 +17,7 @@
</tr>
<% @forum.topics.each do |topic| %>
- <%=content_tag :tr,:class=>"#{cycle('odd', 'even')}" do %>
+ <%=content_tag :tr,:class=>"#{cycle('odd', 'even')}",:id=>"row_#{topic.id}" do %>
<!-- logic to determine whether messages are new -->
<td valign="middle" height="50" align="center"><%=image_tag "email.png",:alt=>"New posts" %></td>
<td width="100%" height="50">
@@ -29,15 +31,17 @@
<%=time_ago_in_words(@recent_post.created_at)%>
<br>
<%=@recent_post.name.nil? ||@recent_post.name.empty? ? "Anonymous" : @recent_post.name %>
- <%=link_to(image_tag("icon_latest_reply.gif"),"#")%>
+ <%=link_to(image_tag("icon_latest_reply.gif"),forum_topic_posts_path(topic.forum, topic))%>
<%else%>
"no posts"
<%end%>
+ <%= button_to(t("forem.topic.delete"), forum_topic_path(@forum, topic), :id => topic.id, :method => :delete, :remote => true, :confirm => t("forem.topic.delete_question"), :class => "button delete") if current_user%>
</td>
<!-- Last post was TIME AGO by NAME in TOPIC -->
<%end%>
<%end%>
<%end%>
+ <%= will_paginate @topics %>
</table>
</div>
<% content_for :stylesheets do %>
@@ -46,7 +50,8 @@
<%end%>
<% content_for :javascript_libraries do %>
- <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript"></script>
+ <%=javascript_include_tag "jquery#{"-min" if Rails.env.production?}" %>
+ <%=javascript_include_tag 'rails.js' %>
<%end%>
<% content_for :after_javascript_libraries do %>
@@ -57,4 +62,4 @@
loadingImage : '/facebox/loading.gif',
closeImage : '/facebox/closelabel.png'})})
</script>
-<% end %>
+<% end %>
Oops, something went wrong.

0 comments on commit b6826f5

Please sign in to comment.