Transcript Document

Rails Plugins
Navjeet Chabbewal
Javaah Enterprises
[email protected]
Agenda

Brief Introduction to Rails Plugins


Installation, removal, install directory
Discuss usage of some useful plugins




tabnav
acts_as_authenticated
file_column (file upload)
acts_as_state_machine
So What are Rails Plugins
►
►
Self contained libraries made specifically for
Rails
Reuse code, yours or somebody else’s
Installation
►
►
►
►
►
Installed per Rails app
First run ./script/plugin discover to add new plugin
repositories
(on windows => ruby script/plugin discover)
./script/plugin install plugin_name
./script/plugin install svn://svn.seesaw.it/tabnav
./script/plugin install -x svn://svn.seesaw.it/tabnav
Installation Contd.
►
Installs in vendor/plugins sub folder of Rails
app root directory
Remove Plugin
►
►
►
Remove folder under vendor/plugins
./script/plugin remove tabnav
Unlink with "svn propdel svn:externals
vendor/plugins"
Tabnav



Provides tabbed navigation
./script/plugin install
svn://svn.seesaw.it/tabnav
./script/generate tabnav Main

Generated two files


app/models/main_tabnav.rb
app/views/tabnav/_main_tabnav.rhtml
Tabnav Contd.

app/models/main_tabnav.rb

Modify this file to add tabs
class MainTabnav < Tabnav::Base
add_tab do
named 'Dashboard'
links_to :controller => 'dashboard'
end
add_tab do
named 'My Projects' # name assigned in the tab
titled 'Summary of project status'
links_to :controller => 'project', :action => 'list'
end
add_tab do
named 'Admin'
links_to :controller => 'admin'
show_if "params[:admin] == true"
end
end
Tabnav Contd.

app/views/tabnav/_main_tabnav.rhtml



partial that generates tabs defined in
main_tabnav.rb
also has default stylesheet code
Add this to layout/view
<%= start_tabnav :main %>
<%= @content_for_layout %>
<%= end_tabnav %>
Tabnav Contd.

Tab screenshot - http://localhost:3001/dashboard
Tabnav contd.



The titled method gives your tabs a html ‘title’
attribute so you can have tooltips over your
tab link.
The links_to method creates the tab’s link.
You can use the same options as the usual
ActionView’s url_for.
You can conditionally choose to show or not
show a tab given a certain condition with the
show_if method.
acts_as_authenticated

Simple authentication generator plugin
Quote from plugin website
"The whole idea behind the plugin is that
you generate the code once and add in your
application specific authentication actions.
This makes no attempt to decide how your
application's authorizing works."
AAA contd.

./script/generate authenticated user account




creates model user and it's migration (which can be
skipped)
creates account controller that has methods like login,
signup, logout etc.
include AuthenticatedSystem in application.rb
Add the line
before_filter :login_required
in every controller you want protected.
AAA Contd.

Extend the basic plugin


To set up email notifications refer to wiki
(http://technoweenie.stikipad.com/plugins/show/M
ailer+Setup).
Can be used with authorization plugin by Bill Katz

permit “railists or wanna_be_railist”, :except => :public
file_column

Quote from plugin website
" This library makes handling of uploaded files in
Ruby on Rails as easy as it should be. It helps you
to not repeat yourself and write the same file
handling code all over the place while providing you
with nice features like keeping uploads during
form redisplays, nice looking URLs for your
uploaded files and easy integration with RMagick
to resize uploaded images and create thumb-nails.
Files are stored in the filesystem and the filename in
the database. "
file_plugin contd.



/script/plugin install
http://opensvn.csie.org/rails_file_column/
plugins/file_column/trunk
rename trunk to file_column
create migration for <products> table
file_column contd.

class CreateProducts < ActiveRecord::Migration
def self.up
create_table :products do |t|
t.column :name, :string
t.column :price, :float
t.column :image, :string # stores uploaded file name
end
end
def self.down
drop_table :products
end
end
file_column contd.

Created a scaffold for <product> model

Just make the "image" column ready for handling
uploaded files
class Product < ActiveRecord::Base
file_column :image
end

Update view (e.g. add product page)

<%= file_column_field "product", "image" %>
file_column contd.

display uploaded
images in your view
(e.g. in view product
page)

<%= image_tag
url_for_file_column("prod
uct", "image") %>
image from
http://www.cafepress.com/rubyonrailsshop
acts_as_state_machine



install from
http://elitists.textdriven.com/svn/plugins/acts_
as_state_machine/trunk
rename trunk to acts_as_state_machine
Create a model <Project> and table
<projects>
AASM contd.

Migration script example
class CreateProjects < ActiveRecord::Migration
def self.up
create_table :projects do |t|
t.column :name, :string
t.column :state, :string
end
end
def self.down
drop_table :projects
end
end
AASM contd.

Update model <Project>
class Project < ActiveRecord::Base
acts_as_state_machine :initial => :created
# can specify :column => 'column_name' default column name is state
# specify different states
state :created
state :estimating
state :bid
state :won, :enter => Proc.new {|project| Mailer.spread_the_good_news(project)}
state :executing
# specify events that change states
event :estimate do
transitions :to => :estimating, :from => :created
end
event :submit_bid do
transitions :to => :bid, :from => :estimating
end
end
Resources








http://wiki.rubyonrails.org/rails/pages/Plugins - List of Rails Plugins
http://www.agilewebdevelopment.com/plugins - List of Rails Plugins
http://nubyonrails.com/articles/2006/05/04/the-complete-guide-to-railsplugins-part-i (Geoffry Grosenbach )
http://blog.seesaw.it/articles/2006/07/23/the-easiest-way-to-add-tabbednavigation-to-your-rails-app
http://technoweenie.stikipad.com/plugins/show/Acts+as+Authenticated
http://www.kanthak.net/opensource/file_column/
http://lunchroom.lunchboxsoftware.com/2006/1/21/acts-as-statemachine
http://elitists.textdriven.com/reminder-fsm.rb - Example for
acts_as_state_machine