From 21dca99ddf7eadb929bc9fe40a6b3f9fd90b2f41 Mon Sep 17 00:00:00 2001 From: Philip Arndt Date: Sat, 28 Aug 2010 18:15:53 +1200 Subject: wrote rails engine for later, added ability to add comments and see ones already posted. --- app/controllers/blog_posts_controller.rb | 21 ++++++++++++++- app/models/blog_comment.rb | 12 ++++++++- app/views/admin/blog/comments/index.html.erb | 12 ++++++--- app/views/blog_posts/show.html.erb | 37 ++++++++++++++++++++++++++ config/locales/en.yml | 1 + config/routes.rb | 39 ++++++++++++++++------------ lib/refinerycms-blog.rb | 18 +++++++++++++ public/stylesheets/refinerycms-blog.css | 3 +++ 8 files changed, 120 insertions(+), 23 deletions(-) diff --git a/app/controllers/blog_posts_controller.rb b/app/controllers/blog_posts_controller.rb index 7ddb7b8..caaa336 100644 --- a/app/controllers/blog_posts_controller.rb +++ b/app/controllers/blog_posts_controller.rb @@ -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 diff --git a/app/models/blog_comment.rb b/app/models/blog_comment.rb index 28139e7..cc890fa 100644 --- a/app/models/blog_comment.rb +++ b/app/models/blog_comment.rb @@ -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'} diff --git a/app/views/admin/blog/comments/index.html.erb b/app/views/admin/blog/comments/index.html.erb index c5e911b..940ba47 100644 --- a/app/views/admin/blog/comments/index.html.erb +++ b/app/views/admin/blog/comments/index.html.erb @@ -3,24 +3,28 @@ <% if searching? %>

<%= t('shared.admin.search.results_for', :query => params[:search]) %>

<% if @blog_comments.any? %> - <%= will_paginate @blog_comments %> + <%=# will_paginate @blog_comments + %> - <%= will_paginate @blog_comments %> + <%=# will_paginate @blog_comments + %> <% else %>

<%= t('admin.search_no_results') %>

<% 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 %>

<%= t('.no_items_yet', diff --git a/app/views/blog_posts/show.html.erb b/app/views/blog_posts/show.html.erb index 2a6fa12..cf51e0c 100644 --- a/app/views/blog_posts/show.html.erb +++ b/app/views/blog_posts/show.html.erb @@ -3,6 +3,8 @@ <% content_for :body_content_left do %> <%= @blog_post.body %> +
+ <% if (categories = @blog_post.categories).any? %>
<%= t('.filed_in') %> @@ -15,6 +17,41 @@
<% end %> + +
+ <% if (comments = @blog_post.comments.approved).any? %> +

<%= t('.comments') %>

+ <% comments.each do |comment| %> +
+

+ <%= comment.message.to_s.gsub("\r\n\r\n", "

").gsub("\r\n", "
") %> +

+
+

+ <%= t('.comment_by', :who => comment.name) %> + <%= ", #{time_ago_in_words(comment.created_at)}".html_safe %> +

+ <% end %> +
+ <% end %> + + <% form_for [:blog_post, @blog_comment] do |f| %> +
+ <%= f.label :name %> + <%= f.text_field :name %> +
+
+ <%= f.label :email %> + <%= f.text_field :email %> +
+
+ <%= f.label :message %> + <%= f.text_area :message, :rows => 6 %> +
+
+ <%= f.submit t('.submit') %> +
+ <% end %> <% end %> <% content_for :body_content_right do %> diff --git a/config/locales/en.yml b/config/locales/en.yml index c37ba53..c6d75e6 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -59,3 +59,4 @@ en: filed_in: Filed in created_at_title: Publishing Date created_at: Posted on {{when}} + submit: Send comment diff --git a/config/routes.rb b/config/routes.rb index 36e80f9..fabb925 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 \ No newline at end of file diff --git a/lib/refinerycms-blog.rb b/lib/refinerycms-blog.rb index 461e75c..c322faf 100644 --- a/lib/refinerycms-blog.rb +++ b/lib/refinerycms-blog.rb @@ -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} diff --git a/public/stylesheets/refinerycms-blog.css b/public/stylesheets/refinerycms-blog.css index d961487..cef953a 100644 --- a/public/stylesheets/refinerycms-blog.css +++ b/public/stylesheets/refinerycms-blog.css @@ -10,4 +10,7 @@ margin: 0px; padding: 0px; display: inline; +} +.field.message_field label { + vertical-align: top; } \ No newline at end of file -- cgit v1.2.3