Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

The Joy of Gems: Cooking up Rails Plugins

6,113 views

Published on

Published in: Technology, Education

The Joy of Gems: Cooking up Rails Plugins

  1. 1. The Joy ofGems:Cooking up http://www.flickr.com/photos/globetrotter1937/130805247
  2. 2. As of Rails 3.2,vendor/plugins is http://www.flickr.com/photos/editor/172690560
  3. 3. 1. No Versioning2. Encourages LocalModification
  4. 4. 1. Fork on Github http://en.wikipedia.org/wiki/File:Acharia_stimulea_0795036.jpg
  5. 5. 2. Create a gem$ bundle gem csv_import create csv_import/Gemfile conflict csv_import/RakefileOverwrite /Users/pm/github/csv_import/Rakefile? (enter"h" for help) [Ynaqdh] force csv_import/Rakefile create csv_import/.gitignore create csv_import/csv_import.gemspec conflict csv_import/lib/csv_import.rbOverwrite /Users/pm/github/csv_import/lib/csv_import.rb?(enter "h" for help) [Ynaqdh] n skip csv_import/lib/csv_import.rb create csv_import/lib/csv_import/version.rbInitializating git repo in /Users/pm/github/csv_import http://en.wikipedia.org/wiki/File:Monarch_Butterfly_Danaus_plexippus_Vertical_Caterpillar_2000px.jpg
  6. 6. 3. Use Rails 3 Stylerequire railsmodule CsvImport class Engine < Rails::Engine endend http://en.wikipedia.org/wiki/File:CH_Caterpillar.jpg
  7. 7. Rails::Railtie Rails::Engine Text Rails::ApplicationDoorkeeper::Application
  8. 8. Rails::RailtieRailtie is the core of theRails framework andprovides several hooks toextend Rails and/ormodify the initializationprocess.
  9. 9. Rails::Railtieclass MyRailtie < Rails::Railtie initializer "my_railtie.config" do |app| app.middleware.use MyRailtie::Middleware endend http://www.flickr.com/photos/stuckincustoms/211239773/
  10. 10. Rails::EngineRails::Engine allows youshare functionality withother applications.Since Rails 3.0, everyRails::Application is justan engine. http://www.flickr.com/photos/stuckincustoms/1221214740/
  11. 11. Rails::ApplicationRails::Application is anEngine with theresponsibility ofcoordinating the wholeboot process. http://www.flickr.com/photos/seangreenberg/2513414034
  12. 12. 4. IntegrationActiveRecord::Base.configurations = {test => {adapter: sqlite3, database: :memory:}}ActiveRecord::Base.establish_connection(test)app = Class.new(Rails::Application)app.config.root = File.dirname(__FILE__)app.initialize!app.routes.draw { resources :members }Class.new(ActiveRecord::Migration) do def self.up create_table(:members) do |t| t.string :email; t.string :name end add_index :members, :email, :unique => true endend.up http://en.wikipedia.org/wiki/File:Baby_Caterpillars_crop2.jpg
  13. 13. Capybara!feature "import csv" do scenario "with only utf-8" do visit members_path attach_file csv, fixtures/utf-8.csv click_button "Import" page.find("td:first").text.should == " " endend
  14. 14. 4. Test withgem csv_builder, path: ~/csv_builder http://www.flickr.com/photos/vickisnature/6126452631
  15. 15. 5. Release!$ rake -Trake build # Build csv_import-0.0.1.gem into the pkg directoryrake install # Build and install csv_import-0.0.1.gem into system gemsrake release # Create tag v0.0.1 and build and push csv_import-0.0.1.gem to Rubygems http://www.flickr.com/photos/spettacolopuro/3906679719/
  16. 16. 6. Update Gemfile gem csv_builderhttp://www.flickr.com/photos/amaynez/3355325172
  17. 17. 7. Pull Request http://en.wikipedia.org/wiki/File:Charaxes_brutus_natalensis.jpg
  18. 18. Thanks!Paul http://www.flickr.com/photos/axlefoley/3825217644/ @pwi

×