1 / 23

Rails Plugins

Rails Plugins. Navjeet Chabbewal Javaah Enterprises navjeet@javaah.com. 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.

moral
Télécharger la présentation

Rails Plugins

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

  3. So What are Rails Plugins • Self contained libraries made specifically for Rails • Reuse code, yours or somebody else’s

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

  5. Installation Contd. • Installs in vendor/plugins sub folder of Rails app root directory

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

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

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

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

  10. Tabnav Contd. • Tab screenshot - http://localhost:3001/dashboard

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

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

  13. 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 linebefore_filter :login_requiredin every controller you want protected.

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

  15. 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 RMagickto resize uploaded images and create thumb-nails. Files are stored in the filesystem and the filename in the database. "

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

  17. file_column contd. • class CreateProducts < ActiveRecord::Migration  def self.up    create_table :products do |t|      t.column :name, :string      t.column :price, :floatt.column :image, :string    # stores uploaded file name    end  end  def self.down    drop_table :products  endend

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

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

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

  21. AASM contd. • Migration script example class CreateProjects < ActiveRecord::Migration  def self.up    create_table :projects do |t|      t.column :name, :stringt.column :state, :string    end  end  def self.down    drop_table :projects  end end

  22. AASM contd. • Update model <Project> class Project < ActiveRecord::Baseacts_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   endend

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

More Related