With a Mighty Hammer
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share

With a Mighty Hammer

  • 5,155 views
Uploaded on

Presentation on Rails 3 generators, given at Red Dirt Ruby Conference in Oklahoma City on May 6, 2010

Presentation on Rails 3 generators, given at Red Dirt Ruby Conference in Oklahoma City on May 6, 2010

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
5,155
On Slideshare
5,108
From Embeds
47
Number of Embeds
3

Actions

Shares
Downloads
17
Comments
0
Likes
1

Embeds 47

http://coderwall.com 24
http://www.slideshare.net 22
http://localhost:3000 1

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. With  a  Mighty  Hammer bensco'ield  –  viget  labs red  dirt  rubyconf 6  may  2010
  • 2. Changes
  • 3. Model
  • 4. [rails2] > ./script/generate model Viking name:string exists app/models/ exists test/unit/ exists test/fixtures/ create app/models/viking.rb create test/unit/viking_test.rb create test/fixtures/vikings.yml exists db/migrate create db/migrate/20100505095300_create_vikings.rb
  • 5. [rails3] > rails g model Viking name:string invoke active_record create db/migrate/20100505095405_create_vikings.rb create app/models/viking.rb invoke test_unit create test/unit/viking_test.rb create test/fixtures/vikings.yml
  • 6. [rails3] > rails g model Viking name:string invoke active_record create db/migrate/20100505095405_create_vikings.rb create app/models/viking.rb invoke test_unit create test/unit/viking_test.rb create test/fixtures/vikings.yml
  • 7. Controller
  • 8. [rails2] > ./script/generate controller Vikings new create exists app/controllers/ exists app/helpers/ create app/views/vikings exists test/functional/ exists test/unit/helpers/ create app/controllers/vikings_controller.rb create test/functional/vikings_controller_test.rb create app/helpers/vikings_helper.rb create test/unit/helpers/vikings_helper_test.rb create app/views/vikings/new.html.erb create app/views/vikings/create.html.erb
  • 9. [rails3] > rails g controller Vikings new create create app/controllers/vikings_controller.rb route get "vikings/create" route get "vikings/new" invoke erb create app/views/vikings create app/views/vikings/new.html.erb create app/views/vikings/create.html.erb invoke test_unit create test/functional/vikings_controller_test.rb invoke helper create app/helpers/vikings_helper.rb invoke test_unit create test/unit/helpers/vikings_helper_test.rb
  • 10. [rails3] > rails g controller Vikings new create create app/controllers/vikings_controller.rb route get "vikings/create" route get "vikings/new" invoke erb create app/views/vikings create app/views/vikings/new.html.erb create app/views/vikings/create.html.erb invoke test_unit create test/functional/vikings_controller_test.rb invoke helper create app/helpers/vikings_helper.rb invoke test_unit create test/unit/helpers/vikings_helper_test.rb
  • 11. Resource
  • 12. [rails2] > ./script/generate resource Viking name:string exists app/models/ exists app/controllers/ exists app/helpers/ create app/views/vikings exists test/functional/ exists test/unit/ exists test/unit/helpers/ dependency model exists app/models/ exists test/unit/ exists test/fixtures/ create app/models/viking.rb create test/unit/viking_test.rb create test/fixtures/vikings.yml exists db/migrate create db/migrate/20100505095850_create_vikings.rb create app/controllers/vikings_controller.rb create test/functional/vikings_controller_test.rb create app/helpers/vikings_helper.rb create test/unit/helpers/vikings_helper_test.rb route map.resources :vikings
  • 13. [rails3] > rails g resource Viking name:string invoke active_record create db/migrate/20100505095729_create_vikings.rb create app/models/viking.rb invoke test_unit create test/unit/viking_test.rb create test/fixtures/vikings.yml invoke controller create app/controllers/vikings_controller.rb invoke erb create app/views/vikings invoke test_unit create test/functional/vikings_controller_test.rb invoke helper create app/helpers/vikings_helper.rb invoke test_unit create test/unit/helpers/vikings_helper_test.rb route resources :vikings
  • 14. [rails3] > rails g resource Viking name:string invoke active_record create db/migrate/20100505095729_create_vikings.rb create app/models/viking.rb invoke test_unit create test/unit/viking_test.rb create test/fixtures/vikings.yml invoke controller create app/controllers/vikings_controller.rb invoke erb create app/views/vikings invoke test_unit create test/functional/vikings_controller_test.rb invoke helper create app/helpers/vikings_helper.rb invoke test_unit create test/unit/helpers/vikings_helper_test.rb route resources :vikings
  • 15. Options
  • 16. [rails2] > ./script/generate model Options: --skip-timestamps --skip-migration --skip-fixture Rails Info: -v, --version -h, --help General Options: -p, --pretend -f, --force -s, --skip -q, --quiet -t, --backtrace -c, --svn -g, --git
  • 17. [rails3] > rails g model Options: -o, --orm=NAME Runtime options: -q, [--quiet] -p, [--pretend] -f, [--force] -s, [--skip] TestUnit options: [--fixture] -r, [--fixture-replacement=NAME] ActiveRecord options: [--migration] [--parent=PARENT] [--timestamps] -t, [--test-framework=NAME]
  • 18. Additions
  • 19. [rails3] > rails g model Options: -o, --orm=NAME Runtime options: -q, [--quiet] -p, [--pretend] -f, [--force] -s, [--skip] TestUnit options: [--fixture] -r, [--fixture-replacement=NAME] ActiveRecord options: [--migration] [--parent=PARENT] [--timestamps] -t, [--test-framework=NAME]
  • 20. [rails3] > rails g resource Options: [--singleton] -c, --resource-controller=NAME -o, --orm=NAME -a, [--actions=ACTION ACTION] [--force-plural] Runtime options: ... TestUnit options: ... ActiveRecord options: ... Controller options: -e, [--template-engine=NAME] [--helper] -t, [--test-framework=NAME]
  • 21. Hooks
  • 22. [rails3] > rails g model Options: -o, --orm=NAME Runtime options: ... TestUnit options: [--fixture] -r, [--fixture-replacement=NAME] ActiveRecord options: [--migration] [--parent=PARENT] [--timestamps] -t, [--test-framework=NAME]
  • 23. [rails3] > rails g resource Options: [--singleton] -c, --resource-controller=NAME -o, --orm=NAME -a, [--actions=ACTION ACTION] [--force-plural] Runtime options: ... TestUnit options: ... ActiveRecord options: ... Controller options: -e, [--template-engine=NAME] [--helper] -t, [--test-framework=NAME]
  • 24. Replacements
  • 25. On  the  Fly
  • 26. [rails3] > rails g model Ninja name:string -o mongo_mapper [rails3] > rails g model Ninja name:string -t shoulda [rails3] > rails g model Ninja name:string -r factory_girl
  • 27. [rails3] > rails g model Ninja name:string -o mongo_mapper error mongo_mapper [not found] [rails3] > rails g model Ninja name:string -t shoulda invoke active_record create db/migrate/20100505102902_create_ninjas.rb create app/models/ninja.rb error shoulda [not found] [rails3] > rails g model Ninja name:string -r factory_girl invoke active_record create db/migrate/20100505102902_create_ninjas.rb create app/models/ninja.rb invoke test_unit create test/unit/ninja_test.rb error factory_girl [not found]
  • 28. [rails3] > rails g generator FactoryGirl create lib/generators/factory_girl create lib/generators/factory_girl/factory_girl_generator.rb create lib/generators/factory_girl/USAGE O create lib/generators/factory_girl/templates N
  • 29. module FactoryGirl class ModelGenerator < Rails::Generators::NamedBase argument :attributes, :type => :array, :default => [], :banner => "field:type field:type" def create_fixture_file template 'factory.rb', File.join('test', 'factories', "#{singular_name}.rb") end def self.source_root @source_root ||= File.expand_path('../templates', __FILE__) end end end
  • 30. Factory.define :<%= singular_name %> do |f| <% attributes.each do |attribute| -%> <% if attribute.type == :references -%> f.association :<%= attribute.name %> <% else -%> f.<%= attribute.name %> <%= attribute.default.inspect %> <% end -%> <% end -%> end
  • 31. [rails3] > rails g model Ninja name:string clan:references -r factory_girl invoke active_record create db/migrate/20100505102902_create_ninjas.rb create app/models/ninja.rb invoke test_unit create test/unit/ninja_test.rb invoke factory_girl create test/factories/ninja.rb
  • 32. Factory.define :ninja do |f| f.name "MyString" f.association :clan end
  • 33. Changing  the  Default
  • 34. module TestApp class Application < Rails::Application config.generators do |g| g.test_framework :test_unit, :fixture_replacement => :factory_girl end end end
  • 35. [rails3] > rails g model Ninja name:string clan:references invoke active_record create db/migrate/20100505102902_create_ninjas.rb create app/models/ninja.rb invoke test_unit create test/unit/ninja_test.rb invoke factory_girl create test/factories/ninja.rb
  • 36. Hooking
  • 37. Typus
  • 38. [rails3] > rails g resource Cowboy name:string invoke active_record create db/migrate/20100506031815_create_cowboys.rb create app/models/cowboy.rb invoke test_unit create test/unit/cowboy_test.rb invoke factory_girl create test/factories/cowboy.rb invoke controller create app/controllers/cowboys_controller.rb invoke erb create app/views/cowboys invoke test_unit create test/functional/cowboys_controller_test.rb invoke helper create app/helpers/cowboys_helper.rb invoke test_unit create test/unit/helpers/cowboys_helper_test.rb route resources :cowboys
  • 39. [rails3] > rails g resource Cowboy name:string invoke active_record create db/migrate/20100506031815_create_cowboys.rb create app/models/cowboy.rb invoke test_unit create test/unit/cowboy_test.rb invoke factory_girl create test/factories/cowboy.rb invoke controller create app/controllers/cowboys_controller.rb invoke erb create app/views/cowboys invoke test_unit create test/functional/cowboys_controller_test.rb invoke helper create app/helpers/cowboys_helper.rb invoke test_unit create test/unit/helpers/cowboys_helper_test.rb route resources :cowboys
  • 40. module Typus class ResourceGenerator < Rails::Generators::NamedBase def initialize(args, *options) #:nodoc: args[0] = args[0].dup if args[0].is_a?(String) && args[0].frozen? args[0] = "Admin::#{args[0]}" super end def create_controller template 'typus_controller.rb', File.join('app', 'controllers', class_path, "#{file_name}_controller.rb") end hook_for :template_engine, :as => :controller hook_for :test_framework, :as => :controller # continued ...
  • 41. # continued ... hook_for(:controller, :as => :controller, :in => :rails) do |instance, controller| instance.instance_eval do @_invocations.delete(Erb::Generators::ControllerGener...) @_invocations.delete(TestUnit::Generators::Controller...) end instance.invoke controller, [instance.name.demodulize] end def self.source_root @source_root ||= File.expand_path('../templates', __FILE__) end end end
  • 42. module TestApp class Application < Rails::Application config.generators do |g| g.resource_controller :typus, :controller => :controller end end end
  • 43. [rails3] > rails g resource Cowboy name:string invoke active_record create db/migrate/20100505231238_create_cowboys.rb create app/models/cowboy.rb invoke test_unit create test/unit/cowboy_test.rb invoke factory_girl create test/factories/cowboy.rb invoke typus create app/controllers/admin/cowboys_controller.rb invoke erb create app/views/admin/cowboys invoke test_unit create test/functional/admin/cowboys_controller_test.rb invoke controller create app/controllers/cowboys_controller.rb invoke erb create app/views/cowboys invoke test_unit create test/functional/cowboys_controller_test.rb invoke helper create app/helpers/cowboys_helper.rb invoke test_unit create test/unit/helpers/cowboys_helper_test.rb route resources :cowboys
  • 44. bensco'ield @bsco'ield ben.sco'ield@viget.com http:/ /spkr8.com/bsco'ield http:/ /viget.com/extend http:   /bensco'ield.com /