Ruby on Rails Plugins - Rich Collins


Published on

Presentation that I gave at the August Ruby Meetup at ODEO in San Francisco. A bit light on content as most of the presentation was a live coding session.

Published in: Technology, Lifestyle
1 Comment
  • how to change the core Rails validation error messages using plugins?

    Give details
    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

Ruby on Rails Plugins - Rich Collins

  1. 1. Rails Plugins Rich Collins r [email_address] 813-391-1080
  2. 2. Plugins Presentation Overview <ul><li>What are plugins? </li></ul><ul><li>Why use them? </li></ul><ul><li>When are they useful? </li></ul><ul><li>How are they used? </li></ul><ul><li>How are they created </li></ul><ul><li>This is an interactive presentation. Stop me frequently to ask questions and see real code! </li></ul>
  3. 3. What are Rails Plugins? <ul><li>A set of files containing ruby code that modifies your Rails application </li></ul><ul><ul><li>Rails Code </li></ul></ul><ul><ul><ul><li>Models, Helpers, Controllers … etc </li></ul></ul></ul><ul><ul><li>Libraries </li></ul></ul><ul><ul><li>Templates for Generators </li></ul></ul><ul><ul><li>Tests! </li></ul></ul>
  4. 4. What are Plugins? <ul><li>Plugins are not just libraries … They often: </li></ul><ul><ul><li>Modify existing Rails classes </li></ul></ul><ul><ul><li>Require database schema modification </li></ul></ul><ul><ul><li>Include code generation scripts </li></ul></ul>
  5. 5. What are Plugins? <ul><li>Plugins are not quite Rails Engines. They usually don’t include: </li></ul><ul><ul><li>View templates </li></ul></ul><ul><ul><li>Entire controllers </li></ul></ul><ul><ul><li>Independent “sub-applications” </li></ul></ul>
  6. 6. Why use Plugins? <ul><li>“ Rails is opinionated software” – DHH </li></ul><ul><ul><li>No authentication </li></ul></ul><ul><ul><li>Enter acts_as_authenticated </li></ul></ul><ul><li>“ Start with no” – Getting Real </li></ul><ul><li>“ Fuck You” – DHH – Canada on Rails </li></ul><ul><ul><li>Good luck getting new features accepted into rails </li></ul></ul>
  7. 7. When to use Plugins <ul><li>You want to abstract common cross-application patterns to re-usable components: </li></ul><ul><ul><li>Tagging of data - acts_as_taggable </li></ul></ul><ul><ul><li>Versioning of data - acts_as_versioned </li></ul></ul><ul><ul><li>Voting on data - acts_as_voteable </li></ul></ul>
  8. 8. When to use Plugins <ul><li>Common Plugin Patterns: </li></ul><ul><ul><li>Models: </li></ul></ul><ul><ul><ul><li>acts_as: voteable, taggable, versioned … etc </li></ul></ul></ul><ul><ul><li>Views: </li></ul></ul><ul><ul><ul><li>Markaby, Google Maps, CSS Graphs, Rails Widgets … etc </li></ul></ul></ul><ul><ul><li>Other: </li></ul></ul><ul><ul><ul><li>Controllers, Tests … etc </li></ul></ul></ul>
  9. 9. How to use Plugins <ul><li>Locating: </li></ul><ul><ul><li>script/plugin list </li></ul></ul><ul><li>Installing: </li></ul><ul><ul><li>script/plugin install acts_as_voteable </li></ul></ul><ul><li>Updating: </li></ul><ul><ul><li>script/plugin update acts_as_voteable </li></ul></ul><ul><li>Removing: </li></ul><ul><ul><li>Script/plugin remove acts_as_voteable </li></ul></ul>
  10. 10. How to use Plugins <ul><li>README, Rdoc: </li></ul><ul><ul><li>/README </li></ul></ul><ul><ul><li>rake doc:plugins </li></ul></ul>
  11. 11. How to use Plugins <ul><li>Run Tests! </li></ul><ul><ul><li>rake test:plugins </li></ul></ul><ul><ul><li>Use test generators if present </li></ul></ul><ul><ul><ul><li>script/generate acts_as_voteable_test post </li></ul></ul></ul><ul><ul><ul><li>rake test </li></ul></ul></ul>
  12. 12. How to use Plugins <ul><li>The common acts_as pattern: </li></ul><ul><li>class User </li></ul><ul><li>acts_as_voter </li></ul><ul><li>end </li></ul><ul><li>class Post </li></ul><ul><li>acts_as_voteable </li></ul><ul><li>end </li></ul>
  13. 13. How to use Plugins <ul><li>The acts_as_voter and acts_as_voteable calls add methods to the User and Post classes: </li></ul><ul><ul><li>User#vote_up </li></ul></ul><ul><ul><li>User#vote_down </li></ul></ul><ul><ul><li>Post#up_votes </li></ul></ul><ul><ul><li>Post#down_votes </li></ul></ul><ul><ul><li>Post.find_top </li></ul></ul>
  14. 14. How to use Plugins <ul><li>Plugins often require database schema modifications </li></ul><ul><ul><li>votes table </li></ul></ul><ul><li>Good plugins make these modifications easy </li></ul><ul><ul><li>script/generate acts_as_voteable migration </li></ul></ul><ul><ul><li>rake migrate </li></ul></ul>
  15. 15. How to use Plugins <ul><li>Plugin Guts – Initialization Files </li></ul><ul><ul><li>install.rb – is executed when plugin is installed </li></ul></ul><ul><ul><ul><li>Generate code, run tests … etc </li></ul></ul></ul><ul><ul><li>init.rb – is executed each time the application is restarted </li></ul></ul><ul><ul><ul><li>Mixin modules, initialize variables … etc </li></ul></ul></ul>
  16. 16. How to use Plugins <ul><li>Plugin Guts – Directories </li></ul><ul><ul><li>/lib </li></ul></ul><ul><ul><ul><li>Modules, models … etc </li></ul></ul></ul><ul><ul><li>/generators </li></ul></ul><ul><ul><ul><li>Generator code and templates </li></ul></ul></ul><ul><ul><li>/test </li></ul></ul><ul><ul><ul><li>Tests for plugin verification </li></ul></ul></ul><ul><ul><li>/tasks </li></ul></ul><ul><ul><ul><li>rake tasks (also see Rakefile) </li></ul></ul></ul>
  17. 17. How to use Plugins <ul><li>Live Coding Session :S </li></ul>
  18. 18. Plugin Recap <ul><li>What are plugins? </li></ul><ul><ul><li>Ruby code & scripts that modify and extend the Rails framework </li></ul></ul><ul><li>Why use them? </li></ul><ul><ul><li>Your features are unlikely to be added to Rails. </li></ul></ul><ul><ul><li>DRY, Modularity & Abstraction </li></ul></ul>
  19. 19. Plugin Recap <ul><li>How do I use them? </li></ul><ul><ul><li>script/install </li></ul></ul><ul><ul><li>rake test:plugins </li></ul></ul><ul><ul><li>script/generate [plugin related code] </li></ul></ul><ul><ul><li>Mixin plugin functionality. (edit models - add acts_as calls) </li></ul></ul>
  20. 20. Contact Info <ul><li>Rich Collins </li></ul><ul><li>[email_address] </li></ul><ul><li> </li></ul><ul><li> </li></ul>