useful rails plugins

23
Rails Plugins Navjeet Chabbewal Javaah Enterprises navjeet@javaah. com

Upload: navjeet

Post on 19-May-2015

7.071 views

Category:

Technology


3 download

DESCRIPTION

This is a presentation I made at my local Northern Virginia Ruby Users Group. It discusses the install, setup and use of a few useful Ruby on Rails plugins.

TRANSCRIPT

Page 1: Useful Rails Plugins

Rails Plugins

Navjeet Chabbewal

Javaah Enterprises

[email protected]

Page 2: Useful Rails Plugins

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

Page 3: Useful Rails Plugins

So What are Rails Plugins

► Self contained libraries made specifically for Rails

► Reuse code, yours or somebody else’s

Page 4: Useful Rails Plugins

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

Page 5: Useful Rails Plugins

Installation Contd.

► Installs in vendor/plugins sub folder of Rails app root directory

Page 6: Useful Rails Plugins

Remove Plugin

► Remove folder under vendor/plugins ► ./script/plugin remove tabnav ► Unlink with "svn propdel svn:externals

vendor/plugins"

Page 7: Useful Rails 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 

Page 8: Useful Rails Plugins

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

Page 9: Useful Rails Plugins

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

Page 10: Useful Rails Plugins

Tabnav Contd.

Tab screenshot - http://localhost:3001/dashboard

Page 11: Useful Rails Plugins

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.

Page 12: Useful Rails Plugins

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."

Page 13: Useful Rails Plugins

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.

Page 14: Useful Rails Plugins

AAA Contd.

Extend the basic plugin To set up email notifications refer to wiki

(http://technoweenie.stikipad.com/plugins/show/Mailer+Setup).

Can be used with authorization plugin by Bill Katz permit “railists or wanna_be_railist”, :except => :public

Page 15: Useful Rails Plugins

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. "

Page 16: Useful Rails Plugins

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

Page 17: Useful Rails Plugins

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  endend

Page 18: Useful Rails Plugins

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 :imageend

Update view (e.g. add product page) <%= file_column_field "product", "image" %>

Page 19: Useful Rails Plugins

file_column contd.

display uploaded images in your view (e.g. in view product page) <%= image_tag

url_for_file_column("product", "image") %>

image from http://www.cafepress.com/rubyonrailsshop

Page 20: Useful Rails Plugins

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>

Page 21: Useful Rails Plugins

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

Page 22: Useful Rails Plugins

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

Page 23: Useful Rails Plugins

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

plugins-part-i (Geoffry Grosenbach ) http://blog.seesaw.it/articles/2006/07/23/the-easiest-way-to-add-tabbed-

navigation-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-state-

machine http://elitists.textdriven.com/reminder-fsm.rb - Example for

acts_as_state_machine