Rails-3-app-auto-generator-20100817
-
Upload
tse-ching-ho -
Category
Technology
-
view
1.349 -
download
0
description
Transcript of Rails-3-app-auto-generator-20100817
Rails 3 app auto-generator Tse-Ching Ho 何澤清
2010/08/17 Ruby-Tuesday | ruby.tw
It’s bored to always write the same codes.
Who needs auto generator?
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 :(
What should we do?
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!
Gemify your modulesBe 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/Gemfilecreate my_awesome_gem/Rakefilecreate my_awesome_gem/.gitignorecreate my_awesome_gem/my_awesome_gem.gemspeccreate my_awesome_gem/lib/my_awesome_gem.rbcreate my_awesome_gem/lib/my_awesome_gem/version.rb
rake buildrake installrake push
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::Tidy\n", :before => " end\nend\n"
Keep updated by force/skip overwrite option
rails generate responders:install -f
Live Demo
Issues
BundlerGemfile.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.
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
Customized templatesRails.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 => falseend
Migration filesmigration_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) endenddef migration_template(source, destination=nil, config={}) superrescue Rails::Generators::Error => e puts eend
What else?
File.exist?
File path
source_root
destination_root
Rails.root
Customized configuration files (config/xxx.rb)
Make every practices as generator!
Q & A
About me
何澤清 Tse-Ching Ho (http://github.com/tsechingho)
Use Rails 2 since 2008
Senior R&D programmer in techbang.tw (T 客邦) now
- END -