wrote rails engine for later, added ability to add comments and see ones already posted.

This commit is contained in:
Philip Arndt 2010-08-28 18:15:53 +12:00
parent e6fead06dc
commit 21dca99ddf
8 changed files with 120 additions and 23 deletions

View file

@ -2,6 +2,7 @@ class BlogPostsController < ApplicationController
before_filter :find_all_blog_posts, :find_all_blog_categories
before_filter :find_page
before_filter :find_blog_post, :only => [:show, :comment]
def index
# you can use meta fields from your model instead (e.g. browser_title)
@ -10,15 +11,33 @@ class BlogPostsController < ApplicationController
end
def show
@blog_post = BlogPost.live.find(params[:id])
@blog_comment = BlogComment.new
# you can use meta fields from your model instead (e.g. browser_title)
# by swapping @page for @blogs in the line below:
present(@page)
end
def comment
if (@blog_comment = BlogComment.create(params[:blog_comment])).valid?
if BlogComment::Moderation.enabled?
flash[:notice] = t('.thank_you_moderated')
redirect_back_or_default blog_post_url(params[:id])
else
redirect_to blog_post_url(params[:id],
:anchor => "comment-#{@blog_comment.to_param}")
end
else
render :action => 'show'
end
end
protected
def find_blog_post
@blog_post = BlogPost.live.find(params[:id])
end
def find_all_blog_posts
unless params[:category_id].present?
@blog_posts = BlogPost.live

View file

@ -1,8 +1,18 @@
class BlogComment < ActiveRecord::Base
filters_spam :author_field => :name,
:email_field => :email,
:message_field => :message
belongs_to :post, :class_name => 'BlogPost'
acts_as_indexed :fields => [:name, :email, :body]
acts_as_indexed :fields => [:name, :email, :message]
alias_attribute :message, :body
validates_presence_of :name, :message
validates_format_of :email,
:with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i
named_scope :unmoderated, :conditions => {:state => nil}
named_scope :approved, :conditions => {:state => 'approved'}

View file

@ -3,24 +3,28 @@
<% if searching? %>
<h2><%= t('shared.admin.search.results_for', :query => params[:search]) %></h2>
<% if @blog_comments.any? %>
<%= will_paginate @blog_comments %>
<%=# will_paginate @blog_comments
%>
<ul>
<%= render :partial => "blog_comments",
:collection => @blog_comments %>
</ul>
<%= will_paginate @blog_comments %>
<%=# will_paginate @blog_comments
%>
<% else %>
<p><%= t('admin.search_no_results') %></p>
<% end %>
<% else %>
<% if @blog_comments.any? %>
<%= will_paginate @blog_comments %>
<%=# will_paginate @blog_comments
%>
<%= render :partial => "sortable_list" %>
<%= will_paginate @blog_comments %>
<%=# will_paginate @blog_comments
%>
<% else %>
<h3>
<%= t('.no_items_yet',

View file

@ -3,6 +3,8 @@
<% content_for :body_content_left do %>
<%= @blog_post.body %>
<hr />
<% if (categories = @blog_post.categories).any? %>
<div class='post_categories'>
<span class='filed_in'><%= t('.filed_in') %></span>
@ -15,6 +17,41 @@
</ul>
</div>
<% end %>
<hr />
<% if (comments = @blog_post.comments.approved).any? %>
<h2><%= t('.comments') %></h2>
<% comments.each do |comment| %>
<div class='blog_comment_message'>
<p>
<%= comment.message.to_s.gsub("\r\n\r\n", "</p><p>").gsub("\r\n", "<br/>") %>
</p>
</div>
<p class='blog_comment_author'>
<%= t('.comment_by', :who => comment.name) %>
<%= ", #{time_ago_in_words(comment.created_at)}".html_safe %>
</p>
<% end %>
<hr />
<% end %>
<% form_for [:blog_post, @blog_comment] do |f| %>
<div class='field'>
<%= f.label :name %>
<%= f.text_field :name %>
</div>
<div class='field'>
<%= f.label :email %>
<%= f.text_field :email %>
</div>
<div class='field message_field'>
<%= f.label :message %>
<%= f.text_area :message, :rows => 6 %>
</div>
<div class='field form-actions'>
<%= f.submit t('.submit') %>
</div>
<% end %>
<% end %>
<% content_for :body_content_right do %>

View file

@ -59,3 +59,4 @@ en:
filed_in: Filed in
created_at_title: Publishing Date
created_at: Posted on {{when}}
submit: Send comment

View file

@ -1,20 +1,25 @@
ActionController::Routing::Routes.draw do |map|
map.blog_post '/blog', :controller => :blog_posts, :action => :index
map.blog_post '/blog/:id', :controller => :blog_posts, :action => :show
map.blog_category '/blog/categories/:category_id', :controller => :blog_posts, :action => :index
if Rails.version < '3.0.0'
ActionController::Routing::Routes.draw do |map|
map.blog_post '/blog', :controller => :blog_posts, :action => :index
map.blog_post '/blog/:id', :controller => :blog_posts, :action => :show
map.blog_category '/blog/categories/:category_id', :controller => :blog_posts, :action => :index
map.blog_post_blog_comments '/blog/:id/comments', :controller => :blog_posts, :action => :comment
map.namespace(:admin, :path_prefix => 'refinery') do |admin|
admin.namespace :blog do |blog|
blog.resources :posts
blog.resources :categories
blog.resources :comments, :collection => {
:approved => :get,
:rejected => :get
}
blog.resources :settings, :collection => {
:notification_recipients => [:get, :post],
:moderation => :get
}
map.namespace(:admin, :path_prefix => 'refinery') do |admin|
admin.namespace :blog do |blog|
blog.resources :posts
blog.resources :categories
blog.resources :comments, :collection => {
:approved => :get,
:rejected => :get
}
blog.resources :settings, :collection => {
:notification_recipients => [:get, :post],
:moderation => :get
}
end
end
end
end
else
# route for rails3 here.
end

View file

@ -2,6 +2,24 @@ require 'filters_spam' if defined?(Bundler)
module Refinery
module Blog
class Engine < Rails::Engine
initializer 'blog serves assets' do
app.middleware.insert_after ::ActionDispatch::Static, ::ActionDispatch::Static, "#{root}/public"
end
config.after_initialize do
Refinery::Plugin.register do |plugin|
plugin.name = "refinerycms_blog"
plugin.url = {:controller => '/admin/blog/posts', :action => 'index'}
plugin.menu_match = /^\/?(admin|refinery)\/blog\/?(posts|comments|categories)?/
plugin.activity = {
:class => BlogPost
}
end
end
end if defined?(Rails::Engine)
class << self
def version
%q{0.9.8.0.rc1}

View file

@ -10,4 +10,7 @@
margin: 0px;
padding: 0px;
display: inline;
}
.field.message_field label {
vertical-align: top;
}