Jul 5 2006

Graphs Rails Plugin

If you need to need to generate bar graphs in your Ruby on Rails application you can use the CSS Graphs plugin. This plugin can create three styles of CSS bar graphs. To install this plugin using the plugin script as follows:

script/plugin install http://topfunky.net/svn/plugins/css_graphs

As soon as you install this plugin you can start using the bar_graph, horizontal_bar_graph, and complex_bar_graph methods introduced to the ActionView by this plugin. To create a simple graph you can use the following code in your rhtml view:

<%= bar_graph ['One', 40],
  ['Two', 20],
  ['Tree', 60],
  ['Four', 30],
  ['Five', 80]
%>

You can use the same data with the horizontal_bar_graph and complex_bar_graph methods. That said, on Firefox on the OS X I was unable to display the complex_bar_graph. I also noticed that the bar_graph has a limit of five bars. And also on the bar_graph on the OS X I was not able to have spaces in my labels.

There is a different plugin by the name of Gruff that allows you to make image graphics. To use Gruff you will need to install and congifure ImageMagick and Ruby’s binding for ImageMagick. I have not had a need to use Gruff or the opportunity to tinker with it but that looks like a more robust solution for generating graphics.

Technorati Tags: , , , , , , ,


Jul 5 2006

Calendar Helper Plugin

This is a view plugin that can help developers with the creation of a simple calendar. Don’t think Google Calendar or Outlook but this will get you started. To install the plugin use the plugin install script:

script/plugin install http://topfunky.net/svn/plugins/calendar_helper

The plugin has a generate script that will create three CSS styles for the calendar.

script/generate calendar_styles

This CSS files will be saved in your rails app public/images/stylesheets/calendar directory. You will need to add one of these style sheets in your controller’s layout rthml file:

<%= stylesheet_link_tag 'calendar/blue/style' %>

Once you have added the right style sheet you can produce the HTML for a stylish calendar by adding the following line to an action’s rhtml view file.

<%= calendar :year => Time.now.year, :month => Time.now.month %>

The Calendar Helper plugin supports a few other options in how and what is displays as for the days of the month. For example, to display each day of the month as link you can do the following:

<%=
calendar(:year => Time.now.year, :month => Time.now.month) do |d|
  link_to d.mday, :action => ...
end
%>

The d variable is a Date instance so you will have the month, year, wday, yday, and other month functions at your disposal. Now, if you want to apply a CSS class on certains days you can replace the code in the block with something like this:

<%=
calendar(:year => Time.now.year, :month => Time.now.month) do |d|
  [link_to(d.mday, :action => ...), {:class => 'cssClass'}]
end
%>

Technorati Tags: , , , , , ,


Jul 5 2006

Rails PDF Plugin

This is one the the most simple and useful plugins you can use. To get started you will need to install the PDF::Writer gem.

gem install pdf-writer

Install the Rails PDF Plugin.

script/plugin install svn://rubyforge.org//var/svn/railspdfplugin/railspdf

To create a PDF document for an action, you need to change the action’s view extension from rhtml to rpdf. In the rpdf view you can insert PDF::Writer code. For example, to create a Hello, World PDF use just these two lines of code in the rpdf view:

pdf.select_font "Times-Roman"
pdf.text "Hello, #{@name}.",
  :font_size => 72, :justification => :center

Artima has a great article that covers in great detail the PDF::Writer library. With PDF::Writer you can create PDF documents with embedded images, business charts, tables, etc. With this plugin you can create PDF reports and documents from rails applications. You can create a PDF document for all of your blog posts.

Technorati Tags: , , , , , , ,


Jul 5 2006

Riff Rails Plugin

Riff is a handy diff tool for ActiveRecord models. The Riff plugin adds just three methods to the ActiveRecord class, diff?, diff, and diff_against_attributes. You install Riff like any other plugin using the script/plugin install command:

script/plugin install http://tfletcher.com/svn/rails-plugins/riff/

Once installed all you have to do to use riff is to create model records.

firstPost = Post.new(:title => 'Rails Plugin')
secondPost = Post.new(:title => 'Plugin Rails')

To check if two objects are different from each other you can use the diff? method.

firstPost.diff?(secondPost) # true

To check what the diferences are use the diff method. The differences are returned as a hash of attributes with the two distinct values for the attribute as an array. Let’s see if the code can explain this a bit further:

diff_hash = one.diff(two)
diff_hash.each { |key, value|
  # value is an array
  value.each { |diff_value|
    logger << "\n + #{diff_value}"
  }
}

The key for the diff_hash is a symbol for the attribute that differs.

You can also diff a model instance against what is saved in the database by using the diff? and diff methods without a parameter. Here is an example of that.

post = Post.find(params[:id])
post.title = 'Rails Rocks'
if post.diff?
  # post is diff dan that in da db
end

Technorati Tags: , , , , , ,


Jul 5 2006

Acts As Bookmarkable Plugin

This is my latest installment on Ruby on Rails plugins. Here is a rails plugin which can help your user manage bookmarks of model records. A bookmarkable model can be any ActiveRecord class so you can use the acts_as_bookmarkable to bookmark other users in a friends list, or bookmark posts in a favorites list, etc. And following the Web 2.0 trends, bookmarks themselves can act as taggable so that you can label bookmarks with tags. You need to install the acts_as_taggable plugin separately, or you can remove one line in the bookmark.rb file to disable tags for bookmarks.

To get started download the plugin from its repository:

script/plugin install http://juixe.com/svn/acts_as_bookmarkable

The installation process will add several ruby files in the vendor/plugins directory. Create a new rails migration and cut and past the following self.up and self.down methods:

def self.up
  create_table :bookmarks, :force => true do |t|
    t.column :title, :string, :limit => 50, :default => ""
    t.column :created_at, :datetime, :null => false
    t.column :bookmarkable_type, :string,
      :limit => 15, :default => "", :null => false
    t.column :bookmarkable_id, :integer, :default => 0, :null => false
    t.column :user_id, :integer, :default => 0, :null => false
  end

  add_index :bookmarks, ["user_id"], :name => "fk_bookmarks_user"
end

def self.down
  drop_table :bookmarks
end

Once you have the acts_as_bookmarkable plugin installed you can make your ActiveRecord classes act as models that can be bookmarkable by calling the acts_as_bookmakble method.

class Post < ActiveRecord::Base
  acts_as_bookmarkable
end

In your controller you can bookmark a post with just a few lines of code:

post = Post.find(params[:id])
bmark = Bookmark.new(:title => someTitle)
post.bookmarks << bmark

And like my other plugins you can assign a bookmark to a user by using the user_id property. Here is an example of how to assign a bookmark to a user model.

bmark = Bookmark.new(:title => someTitle, :user_id => session[:user].id)

You have two options to retrieve the bookmarks for a given user. You can look up all the bookmarks for the given user that are of a certain bookmarkable type. Lets say I want to find all posts that have been bookmarked by the logged in user. To accomplish this I can do the following:

bmarks = Post.find_bookmarks_by_user(session[:user])

If you want all bookmarks for a given user, regardless of the type, then you can use the find_bookmarks_by_user method available from the Bookmark class.

bmarks = Bookmark.find_bookmarks_by_user(session[:user])

Technorati Tags: , , , , , ,


Jul 5 2006

Acts As Versioned Plugin

The acts as versioned plugin allows you to manage different versions of a model record so that you can revert to a previous version or create new ones. To install execute the following command:

script/plugin install http://svn.techno-weenie.net/projects/plugins/acts_as_versioned

Make your model act as versioned.

class Post < ActiveRecord::Base
  acts_as_versioned
end

Versioned post are saved in a separate database table. You can create the versioned table that will contained older posts by invoking the following statement.

Post.create_versioned_table

Normally you can create and drop (via the drop_versioned_table function) the version tables in a ActiveRecord Migration. A side effect of invoking the create_versioned_table is that a new version integer column will be added to your original posts table. The version column will be added automatically by the create_versioned_table.

There is a slight issue with this plugin that I should note. If you have a table posts, you can’t have a post column, you will get a ActiveRecord::AssociateionTypeMismatch error if you do. This has been an issue with my models that I haven’t used it as much as I would like. I don’t want to use this plugin until I, or most likely someone else, solves this. If you want to or need to use this plugin you will need to rename the post column to something else.

So once you have your tables correctly setup, you can start creating versions of your post just by saving your them.

post = Post.new(:txt_post => 'My Post')
post.save

Every time you save a record of your model a copy of that instance will be stored in the post_versioned table, even if no attributes or date has been modified from the current version.

To find the current version of a post you can use the version propterty.

post.version

To revert to a previous version use the revert_to method on a post instance.

post.revert_to(1) # original post

You can save a previous version as the current on by using the save method. The save on a reverted posts will just create a new version.

Technorati Tags: , , , , , ,