Enhancements to Micro-CMS
At the end of the first "A Micro-CMS in Rails " article I suggested some enhancements. Here's an implementation for some of them.
TinyMCE Editor
Lets add an HTML visual editor to our edit page screens. I'll use a popular one called TinyMCE.
Install the editor by downloading it from http://tinymce.moxiecode.com/download.php
and put it under your project's public/javascripts directory. As a shortcut, I keep one copy and symlink it:
$ cd public/javascripts
$ ln -s ~/rails/plugins/tinymce/jscripts/tiny_mce .
In both the pages/edit.rhtml and pages/new/rhtml, add the following lines:
<%= javascript_include_tag "tiny_mce/tiny_mce" %>
<script type="text/javascript" >
tinyMCE.init({
mode:"textareas", editor_selector : "tiny_mce"
});
</script>
and replace
<%#= f.text_area :body %>
with
<%= text_area 'page', 'body' , :cols => "60", :rows => "20", :class => 'tiny_mce' %>
That's all there is to it. Actually the javascript_include_tag could/should go into the <head> section of your layout, but that's only slow down loading other pages that don't use TinyMCE.
Read the TinyMCE docs to see customizations. For example, add the following to your tinyMCE.init line:
<script type="text/javascript" >
theme_advanced_toolbar_location : "top",
theme_advanced_toolbar_align : "left",
tinyMCE.init({
mode:"textareas", editor_selector : "tiny_mce"
});
</script>
Authentication with acts_as_authenticated
You probably want to restrict access to the create, update, and delete actions on your pages to only people who are authorized. Let's use the acts_as_authenticated plugin.
Install
$ script/plugin install http://svn.techno-weenie.net/projects/plugins/acts_as_authenticated
Generate basic user model
$ script/generate authenticated user account
$ rake db:migrate
Move this line from account_controller.rb to application.rb
include AuthenticatedSystem
Start your web server (script/server) and go to http://localhost:3000/account, that should redirect you to http://localhost:3000/account/signup . Add an account.
(Note, if you get an error it may be it can't find the controller from the "any named page" feature added in Part 1. Try commenting out the following lines in routes.rb:
# any named page
#map.connect ":id", :controller => "pages", :action => "show"
#map.connect ":id.html", :controller => "pages", :action => "show"
Then add a before_filter to any controllers that you want to restrict access. Add this line to pages_controller.rb
before_filter :login_required, :only => [ :edit, :update, :destroy ]
Keep Sessions in the Database
Sessions are stored on disk (by default), you can clean them out by deleting the session files under tmp/sessions/
If you prefer to keep sessions in the database,
$ rake db:sessions:create
$ rake db:migrate
Then edit config/environment.rb, uncomment the following line
config.action_controller.session_store = :active_record_store
Restart the server. Note, to clear sessions, run
$ rake db:sessions:clear
Email Notifications when a Page is Edited
Let's say there are multiple users with editing privileges and you want to receive email notices when a page is edited. We can do that with action_mailer.
$ script/generate mailer AdminNotify itemcreated itemupdated itemdeleted
Define an itemupdated message, edit models/admin_notify.rb
def itemupdated(page, sent_at = Time.now)
@subject = 'AdminNotify#itemupdated'
@body = { :page => page }
@recipients = 'linojon@gmail.com'
@from = 'jonathan@linowes.com'
@sent_on = sent_at
@headers = {}
end
and its template, views/admin_notify/itemupdated.rhtml
AdminNotify#itemupdated
A new item was Updated:
Type: <%= @page.class %>
Name: <%= @page.title %>
I needed to setup the outgoing mailer in environments/development.rb, with something like:
config.action_mailer.server_settings = {
:address => "smtp.myisp.net",
:port => 25,
:domain => "www.mydomain.com",
:authentication => :login,
:user_name => "myaccount@myisp.net",
:password => "mypassword"
} In pages_controller.rb, add this before the respond_to (or redirect) call in def update:
AdminNotify.deliver_itemupdated(@page)
Do the same for create and destroy actions. Note, for the destroy action, you need a local copy of page to avoid an error:
def destroy
@page = Pages.find(params[:id])
Page.find(params[:id]).destroy
AdminNotify.deliver_itemdeleted(@page)
redirect_to :action => 'list'
end
There are no comments attached to this item.



