Your SlideShare is downloading. ×
0
With	
  a	
  Mighty	
  Hammer
bensco'ield	
  –	
  viget	
  labs
red	
  dirt	
  rubyconf
6	
  may	
  2010
Changes
Model
[rails2] > ./script/generate model Viking name:string
      exists app/models/
      exists test/unit/
      exists test/f...
[rails3] > rails g model Viking name:string
      invoke active_record
      create    db/migrate/20100505095405_create_vi...
[rails3] > rails g model Viking name:string
      invoke active_record
      create    db/migrate/20100505095405_create_vi...
Controller
[rails2] > ./script/generate controller Vikings new create
      exists app/controllers/
      exists app/helpers/
      c...
[rails3] > rails g controller Vikings new create
      create app/controllers/vikings_controller.rb
       route get "viki...
[rails3] > rails g controller Vikings new create
      create app/controllers/vikings_controller.rb
       route get "viki...
Resource
[rails2] > ./script/generate resource Viking name:string
      exists app/models/
      exists app/controllers/
      exis...
[rails3] > rails g resource Viking name:string
      invoke active_record
      create    db/migrate/20100505095729_create...
[rails3] > rails g resource Viking name:string
      invoke active_record
      create    db/migrate/20100505095729_create...
Options
[rails2] > ./script/generate model
Options:
         --skip-timestamps
         --skip-migration
         --skip-fixture

...
[rails3] > rails g model
Options:
  -o, --orm=NAME

Runtime options:
  -q, [--quiet]
  -p, [--pretend]
  -f, [--force]
  -...
Additions
[rails3] > rails g model
Options:
  -o, --orm=NAME

Runtime options:
  -q, [--quiet]
  -p, [--pretend]
  -f, [--force]
  -...
[rails3] > rails g resource
Options:
      [--singleton]
  -c, --resource-controller=NAME
  -o, --orm=NAME
  -a, [--action...
Hooks
[rails3] > rails g model
Options:
  -o, --orm=NAME

Runtime options:
  ...

TestUnit options:
      [--fixture]
  -r, [--f...
[rails3] > rails g resource
Options:
      [--singleton]
  -c, --resource-controller=NAME
  -o, --orm=NAME
  -a, [--action...
Replacements
On	
  the	
  Fly
[rails3] > rails g model Ninja name:string -o mongo_mapper

[rails3] > rails g model Ninja name:string -t shoulda

[rails3...
[rails3] > rails g model Ninja name:string -o mongo_mapper
       error mongo_mapper [not found]

[rails3] > rails g model...
[rails3] > rails g generator FactoryGirl
    create lib/generators/factory_girl
    create lib/generators/factory_girl/fac...
module FactoryGirl
  class ModelGenerator < Rails::Generators::NamedBase
    argument :attributes, :type => :array, :defau...
Factory.define :<%= singular_name %> do |f|
<% attributes.each do |attribute| -%>
<% if attribute.type == :references -%>
...
[rails3] > rails g model Ninja name:string clan:references
           -r factory_girl
      invoke active_record
      cre...
Factory.define :ninja do |f|
  f.name "MyString"
  f.association :clan
end
Changing	
  the	
  Default
module TestApp
  class Application < Rails::Application
    config.generators do |g|
      g.test_framework :test_unit,
  ...
[rails3] > rails g model Ninja name:string clan:references
      invoke active_record
      create    db/migrate/201005051...
Hooking
Typus
[rails3] > rails g resource Cowboy name:string
      invoke active_record
      create    db/migrate/20100506031815_create...
[rails3] > rails g resource Cowboy name:string
      invoke active_record
      create    db/migrate/20100506031815_create...
module Typus
  class ResourceGenerator < Rails::Generators::NamedBase
    def initialize(args, *options) #:nodoc:
      ar...
# continued ...

    hook_for(:controller,
             :as => :controller,
             :in => :rails) do |instance, cont...
module TestApp
  class Application < Rails::Application
    config.generators do |g|
      g.resource_controller :typus, :...
[rails3] > rails g resource Cowboy name:string
      invoke active_record
      create    db/migrate/20100505231238_create...
bensco'ield
@bsco'ield
ben.sco'ield@viget.com
http:/    /spkr8.com/bsco'ield
http:/    /viget.com/extend
http:	
   /bensco...
With a Mighty Hammer
With a Mighty Hammer
With a Mighty Hammer
Upcoming SlideShare
Loading in...5
×

With a Mighty Hammer

3,745

Published on

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

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
3,745
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
18
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Transcript of "With a Mighty Hammer"

  1. 1. With  a  Mighty  Hammer bensco'ield  –  viget  labs red  dirt  rubyconf 6  may  2010
  2. 2. Changes
  3. 3. Model
  4. 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. 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. 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. 7. Controller
  8. 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. 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. 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. 11. Resource
  12. 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. 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. 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. 15. Options
  16. 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. 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. 18. Additions
  19. 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. 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. 21. Hooks
  22. 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. 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. 24. Replacements
  25. 25. On  the  Fly
  26. 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. 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. 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. 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. 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. 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. 32. Factory.define :ninja do |f| f.name "MyString" f.association :clan end
  33. 33. Changing  the  Default
  34. 34. module TestApp class Application < Rails::Application config.generators do |g| g.test_framework :test_unit, :fixture_replacement => :factory_girl end end end
  35. 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. 36. Hooking
  37. 37. Typus
  38. 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. 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. 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. 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. 42. module TestApp class Application < Rails::Application config.generators do |g| g.resource_controller :typus, :controller => :controller end end end
  43. 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. 44. bensco'ield @bsco'ield ben.sco'ield@viget.com http:/ /spkr8.com/bsco'ield http:/ /viget.com/extend http:   /bensco'ield.com /
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×