Useful Rails Plugins


Published on

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.

Published in: Technology, Art & Photos
1 Comment
  • Hay man you should have made it downloadable. Not making downloadable is against the open source concept... But any way great tutorial..........
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • Useful Rails Plugins

    1. 1. Rails Plugins Navjeet Chabbewal Javaah Enterprises [email_address]
    2. 2. Agenda <ul><li>Brief Introduction to Rails Plugins </li></ul><ul><ul><li>Installation, removal, install directory </li></ul></ul><ul><li>Discuss usage of some useful plugins </li></ul><ul><ul><li>tabnav </li></ul></ul><ul><ul><li>acts_as_authenticated </li></ul></ul><ul><ul><li>file_column (file upload) </li></ul></ul><ul><ul><li>acts_as_state_machine </li></ul></ul>
    3. 3. So What are Rails Plugins <ul><li>Self contained libraries made specifically for Rails </li></ul><ul><li>Reuse code, yours or somebody else’s </li></ul>
    4. 4. Installation <ul><li>Installed per Rails app </li></ul><ul><li>First run ./script/plugin discover to add new plugin repositories    (on windows => ruby script/plugin discover) </li></ul><ul><li>./script/plugin install plugin_name </li></ul><ul><li>./script/plugin install svn:// </li></ul><ul><li>./script/plugin install -x svn:// </li></ul>
    5. 5. Installation Contd. <ul><li>Installs in vendor/plugins sub folder of Rails app root directory </li></ul>
    6. 6. Remove Plugin <ul><li>Remove folder under vendor/plugins </li></ul><ul><li>./script/plugin remove tabnav </li></ul><ul><li>Unlink with &quot; svn propdel svn:externals vendor/plugins &quot; </li></ul>
    7. 7. Tabnav <ul><li>Provides tabbed navigation </li></ul><ul><li>./script/plugin install svn:// </li></ul><ul><li>./script/generate tabnav Main </li></ul><ul><ul><li>Generated two files </li></ul></ul><ul><ul><ul><li>app/models/main_tabnav.rb </li></ul></ul></ul><ul><ul><ul><li>app/views/tabnav/_main_tabnav.rhtml   </li></ul></ul></ul>
    8. 8. Tabnav Contd. <ul><li>app/models/main_tabnav.rb </li></ul><ul><ul><li>Modify this file to add tabs </li></ul></ul><ul><ul><ul><li>class MainTabnav < Tabnav::Base     add_tab do       named 'Dashboard'       links_to :controller => 'dashboard'     end </li></ul></ul></ul><ul><ul><ul><li>   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 &quot;params[:admin] == true&quot; end </li></ul></ul></ul><ul><ul><ul><li>end </li></ul></ul></ul>
    9. 9. Tabnav Contd. <ul><li>app/views/tabnav/_main_tabnav.rhtml   </li></ul><ul><ul><li>partial that generates tabs defined in main_tabnav.rb </li></ul></ul><ul><ul><li>also has default stylesheet code </li></ul></ul><ul><li>Add this to layout/view </li></ul><ul><ul><li><%= start_tabnav :main %> </li></ul></ul><ul><ul><li><%= @content_for_layout %> </li></ul></ul><ul><ul><li><%= end_tabnav %> </li></ul></ul>
    10. 10. Tabnav Contd. <ul><li>Tab screenshot - http://localhost:3001/dashboard </li></ul>
    11. 11. Tabnav contd. <ul><li>The titled method gives your tabs a html ‘title’ attribute so you can have tooltips over your tab link. </li></ul><ul><li>The links_to method creates the tab’s link. You can use the same options as the usual ActionView’s url_for . </li></ul><ul><li>You can conditionally choose to show or not show a tab given a certain condition with the show_if method. </li></ul>
    12. 12. acts_as_authenticated <ul><li>Simple authentication generator plugin      Quote from plugin website      &quot;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.&quot; </li></ul>
    13. 13. AAA contd. <ul><li>./script/generate authenticated user account </li></ul><ul><ul><li>creates model user and it's migration (which can be skipped) </li></ul></ul><ul><ul><li>creates account controller that has methods like login, signup, logout etc. </li></ul></ul><ul><li>include AuthenticatedSystem in application.rb </li></ul><ul><li>Add the line   before_filter :login_required in every controller you want protected. </li></ul>
    14. 14. AAA Contd. <ul><li>Extend the basic plugin </li></ul><ul><ul><li>To set up email notifications refer to wiki ( </li></ul></ul><ul><ul><li>Can be used with authorization plugin by Bill Katz </li></ul></ul><ul><ul><ul><li>permit “railists or wanna_be_railist”, :except => :public </li></ul></ul></ul>
    15. 15. file_column <ul><li>Quote from plugin website &quot; 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. &quot; </li></ul>
    16. 16. file_plugin contd. <ul><li>/script/plugin install plugins/file_column/trunk </li></ul><ul><li>rename trunk to file_column </li></ul><ul><li>create migration for <products> table </li></ul>
    17. 17. file_column contd. <ul><li>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 </li></ul>
    18. 18. file_column contd. <ul><li>Created a scaffold for <product> model </li></ul><ul><ul><li>Just make the &quot;image&quot; column ready for handling uploaded files </li></ul></ul><ul><ul><li>class Product < ActiveRecord::Base file_column :image end </li></ul></ul><ul><li>Update view (e.g. add product page) </li></ul><ul><ul><li><%= file_column_field &quot;product&quot;, &quot;image&quot; %> </li></ul></ul>
    19. 19. file_column contd. <ul><li>display uploaded images in your view (e.g. in view product page) </li></ul><ul><ul><li><%= image_tag url_for_file_column(&quot;product&quot;, &quot;image&quot;) %> </li></ul></ul>image from
    20. 20. acts_as_state_machine <ul><li>install from </li></ul><ul><li>rename trunk to acts_as_state_machine </li></ul><ul><li>Create a model < Project> and table < projects> </li></ul>
    21. 21. AASM contd. <ul><li>Migration script example </li></ul><ul><ul><li>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 </li></ul></ul><ul><ul><li>end </li></ul></ul>
    22. 22. AASM contd. <ul><li>Update model <Project> </li></ul><ul><ul><li>class Project < ActiveRecord::Base    acts_as_state_machine :initial => :created  </li></ul></ul><ul><ul><li># can specify :column => 'column_name' default column name is state    # specify different states    state :created    state :estimating    state :bid    state :won, :enter => {|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 </li></ul></ul>
    23. 23. Resources <ul><li> - List of Rails Plugins </li></ul><ul><li> - List of Rails Plugins </li></ul><ul><li> (Geoffry Grosenbach ) </li></ul><ul><li>  </li></ul><ul><li> </li></ul><ul><li> </li></ul><ul><li> </li></ul><ul><li> - Example for acts_as_state_machine </li></ul>