Rails-3-app-auto-generator-20100817

1,491 views
1,425 views

Published on

This slide reminds some issues you may want to know about rails 3 generator.

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

No Downloads
Views
Total views
1,491
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
5
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Rails-3-app-auto-generator-20100817

  1. 1. Rails 3 app auto-generator Tse-Ching Ho 2010/08/17 Ruby-Tuesday | ruby.tw
  2. 2. It’s bored to always write the same codes.
  3. 3. Who needs auto generator?
  4. 4. NO, Rails Newbies!! Before you do it, You must understand what you are doing. You should play on local branch (git/mercurial). You should know each pieces of codes (Don’t generate trash). You need consistency between projects. You want be a time saver (Stop copy, paste and verify). You type codes as slow as me :(
  5. 5. What should we do?
  6. 6. Modulate your codes Maintainability is a critical thing for long life projects. Reusability is valuable for programmers. Modules are everywhere in rails 3. Use it!
  7. 7. Gemify your modules Be a ruby gem producer http://railscasts.com/episodes/218-making-generators-in-rails-3 jeweler --rspec --gemcutter my_awesome_gem bundle gem my_awesome_gem create my_awesome_gem/Gemfile create my_awesome_gem/Rakefile create my_awesome_gem/.gitignore create my_awesome_gem/my_awesome_gem.gemspec create my_awesome_gem/lib/my_awesome_gem.rb create my_awesome_gem/lib/my_awesome_gem/version.rb rake build rake install rake push
  8. 8. Update to Rails 3 Be familiar with APIs of thor and rails 3 generator gsub_file 'public/stylesheets/blueprint/screen.css', /src/grid.png/, 'grid.png' inject_into_file 'config/application.rb', "n config.middleware.use Rack::Tidyn", :before => " end nendn" Keep updated by force/skip overwrite option rails generate responders:install -f
  9. 9. Live Demo
  10. 10. Issues
  11. 11. Bundler Gemfile.lock should always keep updated in generators. gem 'simple_form' run 'bundle install' run 'bundle update' generate 'simple_form:install', '-s' Use gems, don’t use git branch. You should commit Gemfile.lock if you need consistency between development and production environment.
  12. 12. JQuery get 'http://github.com/rails/jquery-ujs/raw/master/src/rails.js', 'public/ javascripts/rails.js' inject_into_file 'config/application.rb', " config.action_view.javascript_expansions[:defaults] = %w(jquery jquery-ui rails) n", :after => "# config.action_view.javascript_expansions[:defaults] = %w(jquery rails)n", :verbose => false jqueryui.com only keeps the latest one zip file! http://jqueryui.com/download/jquery-ui-1.8.4.custom.zip
  13. 13. Customized templates Rails.root/lib/templates/erb/scaffold/index.html.erb config.generators.scaffold_controller = :responders_controller config.generators do |g| g.orm :active_record g.template_engine :erb g.integration_tool :rspec g.test_framework :rspec g.scaffold :stylesheets => false end
  14. 14. Migration files migration_template 'create_roles.rb', 'db/migrate/create_roles.rb' # Implement the required interface for Rails::Generators::Migration. def self.next_migration_number(dirname) #:nodoc: @next_migration_number = if @next_migration_number (@next_migration_number.to_i + 1).to_s elsif ActiveRecord::Base.timestamped_migrations Time.now.utc.strftime("%Y%m%d%H%M%S") else "%.3d" % (current_migration_number(dirname) + 1) end end def migration_template(source, destination=nil, config={}) super rescue Rails::Generators::Error => e puts e end
  15. 15. What else? File.exist? File path source_root destination_root Rails.root Customized configuration files (config/xxx.rb)
  16. 16. Make every practices as generator!
  17. 17. Q&A
  18. 18. About me Tse-Ching Ho (http://github.com/tsechingho) Use Rails 2 since 2008 Senior R&D programmer in techbang.tw (T ) now
  19. 19. - END -

×