Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Rails Templates

Rails Templates

Jeff Felchner

June 09, 2011
Tweet

More Decks by Jeff Felchner

Other Decks in Technology

Transcript

  1. Our Awesomeness Our Awesomeness ‣ We Maintain... ‣ ruby-progressbar ‣

    chicken_soup ‣ apple_cart ‣ validates_truthiness
  2. Our Awesomeness Our Awesomeness ‣ Contributed To... ‣ rails ‣

    rspec ‣ cucumber-rails ‣ bundler ‣ simple_form ‣ capybara ‣ chronic ‣ wizardly ‣ fuubar ‣ fuubar-cucumber ‣ rspec-rails-mocha ‣ thor ‣ useragent
  3. Overview ‣ Rails Templates ‣ The process of auto-generating a

    Rails app ‣ Template Files ‣ A specific method of auto-generating a Rails app Terminology
  4. Template Files ‣ Straightforward ‣ Resembles a Simple Script ‣

    Been Around the Block ‣ Since Rails 1.0 Template Files
  5. Template Files ‣ Extremely Popular ‣ Used By Majority of

    Template Gems ‣ Suspenders ‣ Prologue ‣ Hobo < 1.3 ‣ Apple Cart < 1.0 Template Files
  6. Template Files ‣ Rails::Generators::Action Helper Methods ‣ gem ‣ generate

    ‣ git ‣ initializer ‣ lib ‣ log ‣ plugin ‣ rake ‣ rakefile ‣ readme ‣ route ‣ vendor ‣ empty_directory_with_gitkeep
  7. Template Files ‣ Thor::Actions Helper Methods ‣ yes?/no? ‣ file_collision

    ‣ mute ‣ say (with feeling) ‣ create_file ‣ get ‣ insert_into_file ‣ comment/uncomment lines ‣ pick items from a list
  8. $ rails new my_cool_app # from the command line Template

    Files How Will This Work? $ rails new my_cool_app ➥ -m http://gist.github.com/my_template.rb -m my_template.rb
  9. if( yes? "Would you like me to .example your .yml

    file?" ) end # my_template_2.rb Template Files Intermediate Example copy_file "config/database.yml", "config/database.yml.example" remove_file "config/database.yml" link_file "some_path/database.yml.postgres", "config/database.yml" append_to_file ".gitignore" do "config/database.yml" end
  10. test_framework = ask "Which testing framework would you like?" #

    my_template_3.rb Template Files Advanced Example if test_framework == "rspec" elsif test_framework == "foo" ... gem "rspec-rails", :group => :test run "rvm use ruby-1.9.2@#{app_name}" run "bundle install" copy_file ".rspec", :force => true generate "rspec:install" directory "spec/support" end
  11. Template Files ‣ Never do manually what a generator will

    do for you. ‣ Keep things script-like. Tips
  12. App Builders ‣ Hooks directly into rails new instead of

    post-processing ‣ Helps to modularize your application generation ‣ More useful for major Rails customizations App Builders
  13. App Builders ‣ AppGenerator contains pass-through methods Background $ rails

    new def create_database build(:database_yaml) end def database_yaml copy_file "database.yml" end ‣ Argument becomes the method called on the AppBuilder ➤ Rails::Generators::AppGenerator.start
  14. $ rails new my_cool_app # from the command line App

    Builders How Will This Work? -m my_template.rb -b my_builder.rb
  15. Revisit .example... Example App Builders if( yes? "Would you like

    me to .example your .yml file?" ) copy_file "config/database.yml", "config/database.yml.example" remove_file "config/database.yml" link_file "some_path/database.yml.postgres", "config/database.yml" append_to_file ".gitignore" do "config/database.yml" end end
  16. Revisit .example... Example App Builders if( yes? "Would you like

    me to .example your .yml file?" ) copy_file "config/database.yml", "config/database.yml.example" remove_file "config/database.yml" link_file "some_path/database.yml.postgres", "config/database.yml" append_to_file ".gitignore" do "config/database.yml" end end class MyAppBuilder < Rails::AppBuilder def database_yml end end template "config/database.#{@options[:database]}.yml" "config/database.yml.example"
  17. Revisit .example... Example App Builders if( yes? "Would you like

    me to .example your .yml file?" ) link_file "some_path/database.yml.postgres", "config/database.yml" append_to_file ".gitignore" do "config/database.yml" end end class MyAppBuilder < Rails::AppBuilder def database_yml end end template "config/database.#{@options[:database]}.yml" "config/database.yml.example" copy_file "#{dest_root}/config/database.yml.example", "#{dest_root}/config/database.yml"
  18. Revisit .example... Example App Builders if( yes? "Would you like

    me to .example your .yml file?" ) append_to_file ".gitignore" do "config/database.yml" end end class MyAppBuilder < Rails::AppBuilder def database_yml end end template "config/database.#{@options[:database]}.yml" "config/database.yml.example" unless @options[:skip_git] end copy_file "#{dest_root}/config/database.yml.example", "#{dest_root}/config/database.yml"
  19. Revisit .example... Example App Builders if( yes? "Would you like

    me to .example your .yml file?" ) append_to_file ".gitignore" do "config/database.yml" end end class MyAppBuilder < Rails::AppBuilder def database_yml end end template "config/database.#{@options[:database]}.yml" "config/database.yml.example" unless @options[:skip_git] end copy_file "#{dest_root}/config/database.yml.example", "#{dest_root}/config/database.yml" else super
  20. def finish_template build(:leftovers) end # app_generator.rb:296 App Builders Playing With

    Your Leftovers ‣ Not implemented in AppBuilder class MyAppBuilder < AppBuilder def leftovers end end # Do some stuff here... maybe copy RSpec support files? # Create an .rvmrc file?
  21. $ rails new my_cool_app -m my_template.rb -b my_builder.rb # from

    the command line App Builders Combine Them
  22. Getting Started Getting Started ‣ Helpful Resources ‣ guides.rubyonrails.org ‣

    railswizard.org ‣ youvegotrails.com (2.3 templates) ‣ suspenders ‣ prologue ‣ apple_cart ‣ Hobo (hobocentral.net)