SlideShare una empresa de Scribd logo
1 de 19
Descargar para leer sin conexión
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 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
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
Live Demo
Issues
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.
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 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
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
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 -

Más contenido relacionado

La actualidad más candente

The Peanut Butter Cup of Web-dev: Plack and single page web apps
The Peanut Butter Cup of Web-dev: Plack and single page web appsThe Peanut Butter Cup of Web-dev: Plack and single page web apps
The Peanut Butter Cup of Web-dev: Plack and single page web apps
John Anderson
 
Frontend Finesse with Angular & Rails 4
Frontend Finesse with Angular & Rails 4Frontend Finesse with Angular & Rails 4
Frontend Finesse with Angular & Rails 4
undecisive
 

La actualidad más candente (20)

Engines
EnginesEngines
Engines
 
Up & running with ECMAScript6
Up & running with ECMAScript6Up & running with ECMAScript6
Up & running with ECMAScript6
 
I motion
I motionI motion
I motion
 
Ansible -new kid in configuration management world
Ansible -new kid in configuration management worldAnsible -new kid in configuration management world
Ansible -new kid in configuration management world
 
MVC-RS par Grégoire Lhotelier
MVC-RS par Grégoire LhotelierMVC-RS par Grégoire Lhotelier
MVC-RS par Grégoire Lhotelier
 
javascript for backend developers
javascript for backend developersjavascript for backend developers
javascript for backend developers
 
AngularJS performance & production tips
AngularJS performance & production tipsAngularJS performance & production tips
AngularJS performance & production tips
 
A little respect for MVC part 1 par Gegoire Lhotellier
A little respect for MVC part 1 par Gegoire LhotellierA little respect for MVC part 1 par Gegoire Lhotellier
A little respect for MVC part 1 par Gegoire Lhotellier
 
What's New in JHipsterLand - Devoxx Poland 2017
What's New in JHipsterLand - Devoxx Poland 2017What's New in JHipsterLand - Devoxx Poland 2017
What's New in JHipsterLand - Devoxx Poland 2017
 
Front End Development for Back End Developers - UberConf 2017
Front End Development for Back End Developers - UberConf 2017Front End Development for Back End Developers - UberConf 2017
Front End Development for Back End Developers - UberConf 2017
 
Intro to UI-Router/TypeScript
Intro to UI-Router/TypeScriptIntro to UI-Router/TypeScript
Intro to UI-Router/TypeScript
 
Reasons To Love Ruby
Reasons To Love RubyReasons To Love Ruby
Reasons To Love Ruby
 
Performance monitoring measurement angualrjs single page apps with phantomas
Performance monitoring measurement angualrjs single page apps with phantomasPerformance monitoring measurement angualrjs single page apps with phantomas
Performance monitoring measurement angualrjs single page apps with phantomas
 
React for Developers
React for DevelopersReact for Developers
React for Developers
 
React‌ برای دولوپرها
React‌ برای دولوپرهاReact‌ برای دولوپرها
React‌ برای دولوپرها
 
The Peanut Butter Cup of Web-dev: Plack and single page web apps
The Peanut Butter Cup of Web-dev: Plack and single page web appsThe Peanut Butter Cup of Web-dev: Plack and single page web apps
The Peanut Butter Cup of Web-dev: Plack and single page web apps
 
Future of Web Development
Future of Web DevelopmentFuture of Web Development
Future of Web Development
 
Bootiful Development with Spring Boot and Angular - Spring I/O 2017
Bootiful Development with Spring Boot and Angular - Spring I/O 2017Bootiful Development with Spring Boot and Angular - Spring I/O 2017
Bootiful Development with Spring Boot and Angular - Spring I/O 2017
 
Frontend Finesse with Angular & Rails 4
Frontend Finesse with Angular & Rails 4Frontend Finesse with Angular & Rails 4
Frontend Finesse with Angular & Rails 4
 
Sinatra
SinatraSinatra
Sinatra
 

Destacado

Downloading Books from BARD for Your Digital Player
Downloading Books from BARD for Your Digital PlayerDownloading Books from BARD for Your Digital Player
Downloading Books from BARD for Your Digital Player
Nathan Flinchum
 
PHP Deployment With Capistrano
PHP Deployment With CapistranoPHP Deployment With Capistrano
PHP Deployment With Capistrano
jserpieters
 
Pillars.io wake upstartup
Pillars.io wake upstartupPillars.io wake upstartup
Pillars.io wake upstartup
Brian Link
 
20150516 modern web_conf_tw
20150516 modern web_conf_tw20150516 modern web_conf_tw
20150516 modern web_conf_tw
Tse-Ching Ho
 
Ponencia PROFECO CENHIES
Ponencia PROFECO CENHIESPonencia PROFECO CENHIES
Ponencia PROFECO CENHIES
Emmanuel Ameth
 
Gótika 2012 19_berry
Gótika 2012 19_berryGótika 2012 19_berry
Gótika 2012 19_berry
eorsianna
 
Tema 2. Conocimiento
Tema 2. ConocimientoTema 2. Conocimiento
Tema 2. Conocimiento
Carlos-9
 
Excursió a can joan
Excursió a can joanExcursió a can joan
Excursió a can joan
magefra
 
La prehistoria
La prehistoriaLa prehistoria
La prehistoria
noeliask
 
Peras 091015103737-phpapp01
Peras 091015103737-phpapp01Peras 091015103737-phpapp01
Peras 091015103737-phpapp01
04141998
 
Preliminary Task Storyboard
Preliminary Task StoryboardPreliminary Task Storyboard
Preliminary Task Storyboard
HannahCostello
 
Estadisticas de los pueblos
Estadisticas de los pueblosEstadisticas de los pueblos
Estadisticas de los pueblos
rubenm18
 

Destacado (20)

D40 16p
D40 16pD40 16p
D40 16p
 
We Were All N00bs: Learning the Social Web by Using the Social Web
We Were All N00bs: Learning the Social Web by Using the Social WebWe Were All N00bs: Learning the Social Web by Using the Social Web
We Were All N00bs: Learning the Social Web by Using the Social Web
 
Downloading Books from BARD for Your Digital Player
Downloading Books from BARD for Your Digital PlayerDownloading Books from BARD for Your Digital Player
Downloading Books from BARD for Your Digital Player
 
Puruchuco
PuruchucoPuruchuco
Puruchuco
 
PHP Deployment With Capistrano
PHP Deployment With CapistranoPHP Deployment With Capistrano
PHP Deployment With Capistrano
 
Get Hip Libraries And Gangsta Lit
Get Hip   Libraries And Gangsta LitGet Hip   Libraries And Gangsta Lit
Get Hip Libraries And Gangsta Lit
 
ruby e-commerce
ruby e-commerceruby e-commerce
ruby e-commerce
 
Staff Training the Wiki Wacky Way!
Staff Training the Wiki Wacky Way!Staff Training the Wiki Wacky Way!
Staff Training the Wiki Wacky Way!
 
Pillars.io wake upstartup
Pillars.io wake upstartupPillars.io wake upstartup
Pillars.io wake upstartup
 
20150516 modern web_conf_tw
20150516 modern web_conf_tw20150516 modern web_conf_tw
20150516 modern web_conf_tw
 
Ruby on bioinformatics
Ruby on bioinformaticsRuby on bioinformatics
Ruby on bioinformatics
 
Building Great Software Engineering Teams
Building Great Software Engineering TeamsBuilding Great Software Engineering Teams
Building Great Software Engineering Teams
 
Ponencia PROFECO CENHIES
Ponencia PROFECO CENHIESPonencia PROFECO CENHIES
Ponencia PROFECO CENHIES
 
Gótika 2012 19_berry
Gótika 2012 19_berryGótika 2012 19_berry
Gótika 2012 19_berry
 
Tema 2. Conocimiento
Tema 2. ConocimientoTema 2. Conocimiento
Tema 2. Conocimiento
 
Excursió a can joan
Excursió a can joanExcursió a can joan
Excursió a can joan
 
La prehistoria
La prehistoriaLa prehistoria
La prehistoria
 
Peras 091015103737-phpapp01
Peras 091015103737-phpapp01Peras 091015103737-phpapp01
Peras 091015103737-phpapp01
 
Preliminary Task Storyboard
Preliminary Task StoryboardPreliminary Task Storyboard
Preliminary Task Storyboard
 
Estadisticas de los pueblos
Estadisticas de los pueblosEstadisticas de los pueblos
Estadisticas de los pueblos
 

Similar a Rails-3-app-auto-generator-20100817

Integrating Browserify with Sprockets
Integrating Browserify with SprocketsIntegrating Browserify with Sprockets
Integrating Browserify with Sprockets
Spike Brehm
 
JRuby on Rails Deployment: What They Didn't Tell You
JRuby on Rails Deployment: What They Didn't Tell YouJRuby on Rails Deployment: What They Didn't Tell You
JRuby on Rails Deployment: What They Didn't Tell You
elliando dias
 
Migrating Legacy Rails Apps to Rails 3
Migrating Legacy Rails Apps to Rails 3Migrating Legacy Rails Apps to Rails 3
Migrating Legacy Rails Apps to Rails 3
Clinton Dreisbach
 
Flu3nt highlights
Flu3nt highlightsFlu3nt highlights
Flu3nt highlights
dswork
 

Similar a Rails-3-app-auto-generator-20100817 (20)

Integrating Browserify with Sprockets
Integrating Browserify with SprocketsIntegrating Browserify with Sprockets
Integrating Browserify with Sprockets
 
Consegi 2010 - Dicas de Desenvolvimento Web com Ruby
Consegi 2010 - Dicas de Desenvolvimento Web com RubyConsegi 2010 - Dicas de Desenvolvimento Web com Ruby
Consegi 2010 - Dicas de Desenvolvimento Web com Ruby
 
Monkeybars in the Manor
Monkeybars in the ManorMonkeybars in the Manor
Monkeybars in the Manor
 
Jaoo Michael Neale 09
Jaoo Michael Neale 09Jaoo Michael Neale 09
Jaoo Michael Neale 09
 
Rails Engine | Modular application
Rails Engine | Modular applicationRails Engine | Modular application
Rails Engine | Modular application
 
Fisl 11 - Dicas de Desenvolvimento Web com Ruby
Fisl 11 - Dicas de Desenvolvimento Web com RubyFisl 11 - Dicas de Desenvolvimento Web com Ruby
Fisl 11 - Dicas de Desenvolvimento Web com Ruby
 
Ruby on Rails survival guide of an aged Java developer
Ruby on Rails survival guide of an aged Java developerRuby on Rails survival guide of an aged Java developer
Ruby on Rails survival guide of an aged Java developer
 
Rapid Prototyping FTW!!!
Rapid Prototyping FTW!!!Rapid Prototyping FTW!!!
Rapid Prototyping FTW!!!
 
JRuby, Ruby, Rails and You on the Cloud
JRuby, Ruby, Rails and You on the CloudJRuby, Ruby, Rails and You on the Cloud
JRuby, Ruby, Rails and You on the Cloud
 
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-FrameworksZepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
 
Angular JS in 2017
Angular JS in 2017Angular JS in 2017
Angular JS in 2017
 
20141210 rakuten techtalk
20141210 rakuten techtalk20141210 rakuten techtalk
20141210 rakuten techtalk
 
JRuby on Rails Deployment: What They Didn't Tell You
JRuby on Rails Deployment: What They Didn't Tell YouJRuby on Rails Deployment: What They Didn't Tell You
JRuby on Rails Deployment: What They Didn't Tell You
 
Derailing rails
Derailing railsDerailing rails
Derailing rails
 
Migrating Legacy Rails Apps to Rails 3
Migrating Legacy Rails Apps to Rails 3Migrating Legacy Rails Apps to Rails 3
Migrating Legacy Rails Apps to Rails 3
 
Flu3nt highlights
Flu3nt highlightsFlu3nt highlights
Flu3nt highlights
 
introduction-infra-as-a-code using terraform
introduction-infra-as-a-code using terraformintroduction-infra-as-a-code using terraform
introduction-infra-as-a-code using terraform
 
ME vs WEB - AngularJS Fundamentals
ME vs WEB - AngularJS FundamentalsME vs WEB - AngularJS Fundamentals
ME vs WEB - AngularJS Fundamentals
 
Damage Control
Damage ControlDamage Control
Damage Control
 
Building web framework with Rack
Building web framework with RackBuilding web framework with Rack
Building web framework with Rack
 

Más de Tse-Ching Ho (6)

Webconf2013
Webconf2013Webconf2013
Webconf2013
 
Ajax nested form and ajax upload in rails
Ajax nested form and ajax upload in railsAjax nested form and ajax upload in rails
Ajax nested form and ajax upload in rails
 
mongodb-introduction
mongodb-introductionmongodb-introduction
mongodb-introduction
 
devise tutorial - 2011 rubyconf taiwan
devise tutorial - 2011 rubyconf taiwandevise tutorial - 2011 rubyconf taiwan
devise tutorial - 2011 rubyconf taiwan
 
model.search: customize your own search logic
model.search: customize your own search logicmodel.search: customize your own search logic
model.search: customize your own search logic
 
The Power of Rails 2.3 Engines & Templates
The Power of Rails 2.3 Engines & TemplatesThe Power of Rails 2.3 Engines & Templates
The Power of Rails 2.3 Engines & Templates
 

Último

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Último (20)

[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 

Rails-3-app-auto-generator-20100817

  • 1. Rails 3 app auto-generator Tse-Ching Ho 2010/08/17 Ruby-Tuesday | ruby.tw
  • 2. It’s bored to always write the same codes.
  • 3. Who needs auto generator?
  • 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 :(
  • 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. 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. 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
  • 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. 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. 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. 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. What else? File.exist? File path source_root destination_root Rails.root Customized configuration files (config/xxx.rb)
  • 16. Make every practices as generator!
  • 17. Q&A
  • 18. About me Tse-Ching Ho (http://github.com/tsechingho) Use Rails 2 since 2008 Senior R&D programmer in techbang.tw (T ) now