SlideShare a Scribd company logo
Rails 3 generators
By: Josh Moore
About Me

✤   Josh Moore (

    ✤   www.codingforrent.com
    ✤   twitter.com/codingforrent
    ✤   http://github.com/joshsmoore
✤   Ruby
    ✤   Watir at work
    ✤   Rails on the Google App Engine for hobby
        ✤   maintain rails_dm_datastore gem
Does anybody mind if I use
English?
Contents



✤   Why you need Rails 3 generators
✤   What is new with Rails 3 generators /thor
✤   Creating your own generator
✤   Demo
Why should you care?
The Problem
Date
The Problem


✤   You cannot deviate from the rails convection without losing
    productivity.

✤   Who uses something besides the normal rails libraries?

    ✤   ERB, ActiveRecord, Test::Unit, fixtures

    ✤   The generators are not nearly as nice as they used to be.
Are you sure this is a big deal?


✤   I use DataMapper, Haml

    ✤   Generator for views does not work

    ✤   Generator for models does not work

    ✤   Scaffold does not work
Fixing it



✤   Pretty much impossible in Rails 2

✤   You could make your own generator, but not integrated different
    names or syntax
Rails 3 goal


✤   Modularity - Easy to switch components

✤   But, the generators still produce the same objects by default

    ✤   ActiveRecord

    ✤   TestUnit

    ✤   ERB
How does Rails 3 help?


✤   Meta Generators

✤   Scaffold is now a meta generator

✤   Hooks

✤   Internals are complete new - rebuilt with Thor
How does that apply to me?




✤   Demo
Scaffold Meta Generator


✤   Actually does not generator anything, just hooks into other generators
✤   Scaffold - orm, scaffold-controller
✤   ActiveRecord - test-framework
✤   TestUnit - fixture-replacement
✤   ScaffoldController - template-engine
List of hooks

✤   test_framework        ✤   orm

✤   webrat                ✤   performance_tool

✤   resource_controller   ✤   generator

✤   template_engine       ✤   scaffold_controller

✤   integration_tool      ✤   helper
Hooks


✤   Allow one generator to invoke another generator

✤   Can control what generator is invoked

✤   Allow you to override the defaults

✤   Create custom hooks if you want to
Override the defaults

✤   For each command

    ✤   --template-engine=haml

    ✤   --orm=datamapper

✤   Change the default in config/application.rb
config.generators do |g|
  g.orm             :datamapper
  g.template_engine :haml
  g.test_framework :test_unit, :fixture_replacement
=> :factory_girl
end
Thor


✤   “Map options to a class. Simply create a class with the appropriate
    annotations and have options automatically map to functions and
    parameters.”

    ✤




✤   Plan English: Replacement for Rake and sake with a sane way to pass
    parameters, which means it makes it easy to map command line
    arguments to parameters and methods in a Ruby class
> thor app:install myname --force
> thor app -L
      class App < Thor
        map "-L" => :list


        desc "install APP_NAME", "install one of the available apps"
        method_options :force => :boolean, :alias => :string
        def install(name)
          user_alias = options[:alias]
          if options.force?
            puts "forcing deletion"
          end
            puts 'installing'
        end


        desc "list [SEARCH]", "list all of the available apps, limited by SEARCH"
        def list(search="")
          # list everything
          if search == ""
            puts "searching everything"
          else
            puts "searching for #{search}"
          end
        end
      end
Things that are hard in Rake



✤   --force - boolean parameters are harder

✤   mapping -L to the app:list action

✤   parameters mapped to Ruby types (boolean, string, number, array,
    and hash)
What does this mean for Rails
generators?

✤   Thor::Actions

    ✤   General purpose actions

✤   Rails::Generators::Actions

    ✤   actions specific to rails

✤   Application templates and Rails generators now share same API
    (Thor::Actions)
Building your own generator
There’s a generator for that

✤   rails g generator NAME

✤   creates these files

✤   haml/

    ✤   haml_generator.rb - Gets called when you execute the generator

    ✤   USAGE - Describes the generator

    ✤   templates/ - templates go here
haml_generator.rb Explained
Code

✤   Place your code in instance methods

    ✤   It has access to all arguments and options

✤   All public instance methods will be executed (probably in the order
    that the methods are defined in)

✤   Inherits from Thor::Group
haml_generator.rb Explained
Parameters


✤   Arguments and options

✤   Arguments are non-named parameters that come first

✤   Options are named parameters that come last

✤   Straight from Thor
Arguments


✤   Described at the top of your class

✤   The order they are declared is the order they will be accepted on the
    command line
Arguments Example



class HamlGenerator < Rails::Generators::NamedBase

 argument :test_arg, :type => :numeric, :banner => '<number of units>'

 argument :test_arg3, :type => :string, :required => false, :default => 'default'
Declaring Arguments

✤   :desc - Description for the argument.

✤   :required, :optional - If the argument is required or not.

✤   :type - The type of the argument, can be

    ✤   :string, :hash, :array, :numeric.

✤   :default - Default value for this argument. It cannot be required and
    have default values.

✤   :banner - String that shows the input format
Argument Rules

✤   Once an argument is declared optional all remaining arguments must
    be optional

✤   you cannot “miss” an optional argument

✤   Once an array argument is declared it will parse everything option
    after it into the array

✤   Access the value of the argument by calling its name (attr_accessor)

✤   Required by default
Options


✤   Also described at the top of your class

✤   But, the order does not matter
Option Example


class HamlGenerator < Rails::Generators::NamedBase

 class_option :test_arg, :type => :numeric, :banner => '<number of units>'

 class_option :test_arg3, :type => :string, :required => false, :default => 'default'
Declaring a class_option

✤   :desc - Description for the argument.

✤   :required, :optional - If the argument is required or not.

✤   :type - The type of the argument, can be

    ✤   :boolean, :numeric, :hash, :array, :string

✤   :default - Default value for this argument. It cannot be required and
    have default values.

✤   :banner - String that shows the input format
class_option rules



✤   use the --name-of-class-option to set the class option

✤   optional by default

✤   underscores (_) in option name become dashes (-)
Implementing Hooks


✤   simply call hook_for :<NAME>

✤   switches provided --[no|skip]-test-framework

✤   under the hood

    ✤   creates a class_option

    ✤   invokes “invoke_from_option”
Example
Questions?

More Related Content

What's hot

Puppet Camp Amsterdam 2015: How To Leverage The Power of the Puppet Forge
Puppet Camp Amsterdam 2015: How To Leverage The Power of the Puppet ForgePuppet Camp Amsterdam 2015: How To Leverage The Power of the Puppet Forge
Puppet Camp Amsterdam 2015: How To Leverage The Power of the Puppet Forge
Puppet
 
What You Need to Know About Lambdas - Jamie Allen (Typesafe)
What You Need to Know About Lambdas - Jamie Allen (Typesafe)What You Need to Know About Lambdas - Jamie Allen (Typesafe)
What You Need to Know About Lambdas - Jamie Allen (Typesafe)
jaxLondonConference
 
Mutation testing with the mutant gem
Mutation testing with the mutant gemMutation testing with the mutant gem
Mutation testing with the mutant gem
DonSchado
 
Power of Puppet 4
Power of Puppet 4Power of Puppet 4
Power of Puppet 4
Martin Alfke
 
Apache Camel
Apache CamelApache Camel
Apache Camel
helggeist
 
Introduction to Elm
Introduction to ElmIntroduction to Elm
Introduction to Elm
Rogerio Chaves
 
Unit testing JavaScript using Mocha and Node
Unit testing JavaScript using Mocha and NodeUnit testing JavaScript using Mocha and Node
Unit testing JavaScript using Mocha and Node
Josh Mock
 
Design Summit - Rails 4 Migration - Aaron Patterson
Design Summit - Rails 4 Migration - Aaron PattersonDesign Summit - Rails 4 Migration - Aaron Patterson
Design Summit - Rails 4 Migration - Aaron Patterson
ManageIQ
 
Modern Perl
Modern PerlModern Perl
Modern Perl
Marcos Rebelo
 
Um2010
Um2010Um2010
Um2010
Jun Furuse
 

What's hot (10)

Puppet Camp Amsterdam 2015: How To Leverage The Power of the Puppet Forge
Puppet Camp Amsterdam 2015: How To Leverage The Power of the Puppet ForgePuppet Camp Amsterdam 2015: How To Leverage The Power of the Puppet Forge
Puppet Camp Amsterdam 2015: How To Leverage The Power of the Puppet Forge
 
What You Need to Know About Lambdas - Jamie Allen (Typesafe)
What You Need to Know About Lambdas - Jamie Allen (Typesafe)What You Need to Know About Lambdas - Jamie Allen (Typesafe)
What You Need to Know About Lambdas - Jamie Allen (Typesafe)
 
Mutation testing with the mutant gem
Mutation testing with the mutant gemMutation testing with the mutant gem
Mutation testing with the mutant gem
 
Power of Puppet 4
Power of Puppet 4Power of Puppet 4
Power of Puppet 4
 
Apache Camel
Apache CamelApache Camel
Apache Camel
 
Introduction to Elm
Introduction to ElmIntroduction to Elm
Introduction to Elm
 
Unit testing JavaScript using Mocha and Node
Unit testing JavaScript using Mocha and NodeUnit testing JavaScript using Mocha and Node
Unit testing JavaScript using Mocha and Node
 
Design Summit - Rails 4 Migration - Aaron Patterson
Design Summit - Rails 4 Migration - Aaron PattersonDesign Summit - Rails 4 Migration - Aaron Patterson
Design Summit - Rails 4 Migration - Aaron Patterson
 
Modern Perl
Modern PerlModern Perl
Modern Perl
 
Um2010
Um2010Um2010
Um2010
 

Viewers also liked

Rails01
Rails01Rails01
Ruby on Rails Training - Module 1
Ruby on Rails Training - Module 1Ruby on Rails Training - Module 1
Ruby on Rails Training - Module 1
Mark Menard
 
Rails Text Mate Cheats
Rails Text Mate CheatsRails Text Mate Cheats
Rails Text Mate Cheats
dezarrolla
 
Ruby on Rails 101
Ruby on Rails 101Ruby on Rails 101
Ruby on Rails 101
Clayton Lengel-Zigich
 
Ruby on Rails Kickstart 103 & 104
Ruby on Rails Kickstart 103 & 104Ruby on Rails Kickstart 103 & 104
Ruby on Rails Kickstart 103 & 104
Heng-Yi Wu
 
Rest and Rails
Rest and RailsRest and Rails
Rest and Rails
Chamnap Chhorn
 
Railsguide
RailsguideRailsguide
Railsguide
lanlau
 
Rails 3 Beginner to Builder 2011 Week 3
Rails 3 Beginner to Builder 2011 Week 3Rails 3 Beginner to Builder 2011 Week 3
Rails 3 Beginner to Builder 2011 Week 3
Richard Schneeman
 
Introducing Command Line Applications with Ruby
Introducing Command Line Applications with RubyIntroducing Command Line Applications with Ruby
Introducing Command Line Applications with Ruby
Nikhil Mungel
 
Ruby on Rails 101 - Presentation Slides for a Five Day Introductory Course
Ruby on Rails 101 - Presentation Slides for a Five Day Introductory CourseRuby on Rails 101 - Presentation Slides for a Five Day Introductory Course
Ruby on Rails 101 - Presentation Slides for a Five Day Introductory Course
peter_marklund
 
Ruby on Rails for beginners
Ruby on Rails for beginnersRuby on Rails for beginners
Ruby on Rails for beginners
Vysakh Sreenivasan
 

Viewers also liked (11)

Rails01
Rails01Rails01
Rails01
 
Ruby on Rails Training - Module 1
Ruby on Rails Training - Module 1Ruby on Rails Training - Module 1
Ruby on Rails Training - Module 1
 
Rails Text Mate Cheats
Rails Text Mate CheatsRails Text Mate Cheats
Rails Text Mate Cheats
 
Ruby on Rails 101
Ruby on Rails 101Ruby on Rails 101
Ruby on Rails 101
 
Ruby on Rails Kickstart 103 & 104
Ruby on Rails Kickstart 103 & 104Ruby on Rails Kickstart 103 & 104
Ruby on Rails Kickstart 103 & 104
 
Rest and Rails
Rest and RailsRest and Rails
Rest and Rails
 
Railsguide
RailsguideRailsguide
Railsguide
 
Rails 3 Beginner to Builder 2011 Week 3
Rails 3 Beginner to Builder 2011 Week 3Rails 3 Beginner to Builder 2011 Week 3
Rails 3 Beginner to Builder 2011 Week 3
 
Introducing Command Line Applications with Ruby
Introducing Command Line Applications with RubyIntroducing Command Line Applications with Ruby
Introducing Command Line Applications with Ruby
 
Ruby on Rails 101 - Presentation Slides for a Five Day Introductory Course
Ruby on Rails 101 - Presentation Slides for a Five Day Introductory CourseRuby on Rails 101 - Presentation Slides for a Five Day Introductory Course
Ruby on Rails 101 - Presentation Slides for a Five Day Introductory Course
 
Ruby on Rails for beginners
Ruby on Rails for beginnersRuby on Rails for beginners
Ruby on Rails for beginners
 

Similar to Rails 3 generators

Angular - Chapter 2 - TypeScript Programming
Angular - Chapter 2 - TypeScript Programming  Angular - Chapter 2 - TypeScript Programming
Angular - Chapter 2 - TypeScript Programming
WebStackAcademy
 
Practical catalyst
Practical catalystPractical catalyst
Practical catalyst
dwm042
 
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
Tse-Ching Ho
 
JavaScript Robotics
JavaScript RoboticsJavaScript Robotics
JavaScript Robotics
Anna Gerber
 
2 BytesC++ course_2014_c7_ operator overloading, friends and references
2 BytesC++ course_2014_c7_ operator overloading, friends and references 2 BytesC++ course_2014_c7_ operator overloading, friends and references
2 BytesC++ course_2014_c7_ operator overloading, friends and references
kinan keshkeh
 
Coding standard
Coding standardCoding standard
Coding standard
FAROOK Samath
 
Developing web apps using Erlang-Web
Developing web apps using Erlang-WebDeveloping web apps using Erlang-Web
Developing web apps using Erlang-Web
fanqstefan
 
Speedy TDD with Rails
Speedy TDD with RailsSpeedy TDD with Rails
Speedy TDD with Rails
PatchSpace Ltd
 
Java8
Java8Java8
2011-02-03 LA RubyConf Rails3 TDD Workshop
2011-02-03 LA RubyConf Rails3 TDD Workshop2011-02-03 LA RubyConf Rails3 TDD Workshop
2011-02-03 LA RubyConf Rails3 TDD Workshop
Wolfram Arnold
 
Lambdas in Java 8
Lambdas in Java 8Lambdas in Java 8
Lambdas in Java 8
Tobias Coetzee
 
Ember-CLI Blueprints for fun and profit
Ember-CLI Blueprints for fun and profitEmber-CLI Blueprints for fun and profit
Ember-CLI Blueprints for fun and profit
Salesforce Engineering
 
Ruby on Rails: Coding Guideline
Ruby on Rails: Coding GuidelineRuby on Rails: Coding Guideline
Ruby on Rails: Coding Guideline
Nascenia IT
 
High Performance Django 1
High Performance Django 1High Performance Django 1
High Performance Django 1
DjangoCon2008
 
High Performance Django
High Performance DjangoHigh Performance Django
High Performance Django
DjangoCon2008
 
PyGrunn 2017 - Django Performance Unchained - slides
PyGrunn 2017 - Django Performance Unchained - slidesPyGrunn 2017 - Django Performance Unchained - slides
PyGrunn 2017 - Django Performance Unchained - slides
Artur Barseghyan
 
Angular Weekend
Angular WeekendAngular Weekend
Angular Weekend
Troy Miles
 
Generic Programming
Generic ProgrammingGeneric Programming
Generic Programming
Muhammad Alhalaby
 
Java script final presentation
Java script final presentationJava script final presentation
Java script final presentation
Adhoura Academy
 
Meta Object Protocols
Meta Object ProtocolsMeta Object Protocols
Meta Object Protocols
Pierre de Lacaze
 

Similar to Rails 3 generators (20)

Angular - Chapter 2 - TypeScript Programming
Angular - Chapter 2 - TypeScript Programming  Angular - Chapter 2 - TypeScript Programming
Angular - Chapter 2 - TypeScript Programming
 
Practical catalyst
Practical catalystPractical catalyst
Practical catalyst
 
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
 
JavaScript Robotics
JavaScript RoboticsJavaScript Robotics
JavaScript Robotics
 
2 BytesC++ course_2014_c7_ operator overloading, friends and references
2 BytesC++ course_2014_c7_ operator overloading, friends and references 2 BytesC++ course_2014_c7_ operator overloading, friends and references
2 BytesC++ course_2014_c7_ operator overloading, friends and references
 
Coding standard
Coding standardCoding standard
Coding standard
 
Developing web apps using Erlang-Web
Developing web apps using Erlang-WebDeveloping web apps using Erlang-Web
Developing web apps using Erlang-Web
 
Speedy TDD with Rails
Speedy TDD with RailsSpeedy TDD with Rails
Speedy TDD with Rails
 
Java8
Java8Java8
Java8
 
2011-02-03 LA RubyConf Rails3 TDD Workshop
2011-02-03 LA RubyConf Rails3 TDD Workshop2011-02-03 LA RubyConf Rails3 TDD Workshop
2011-02-03 LA RubyConf Rails3 TDD Workshop
 
Lambdas in Java 8
Lambdas in Java 8Lambdas in Java 8
Lambdas in Java 8
 
Ember-CLI Blueprints for fun and profit
Ember-CLI Blueprints for fun and profitEmber-CLI Blueprints for fun and profit
Ember-CLI Blueprints for fun and profit
 
Ruby on Rails: Coding Guideline
Ruby on Rails: Coding GuidelineRuby on Rails: Coding Guideline
Ruby on Rails: Coding Guideline
 
High Performance Django 1
High Performance Django 1High Performance Django 1
High Performance Django 1
 
High Performance Django
High Performance DjangoHigh Performance Django
High Performance Django
 
PyGrunn 2017 - Django Performance Unchained - slides
PyGrunn 2017 - Django Performance Unchained - slidesPyGrunn 2017 - Django Performance Unchained - slides
PyGrunn 2017 - Django Performance Unchained - slides
 
Angular Weekend
Angular WeekendAngular Weekend
Angular Weekend
 
Generic Programming
Generic ProgrammingGeneric Programming
Generic Programming
 
Java script final presentation
Java script final presentationJava script final presentation
Java script final presentation
 
Meta Object Protocols
Meta Object ProtocolsMeta Object Protocols
Meta Object Protocols
 

Recently uploaded

Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
Zilliz
 
Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024
Jason Packer
 
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
saastr
 
Skybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoptionSkybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoption
Tatiana Kojar
 
FREE A4 Cyber Security Awareness Posters-Social Engineering part 3
FREE A4 Cyber Security Awareness  Posters-Social Engineering part 3FREE A4 Cyber Security Awareness  Posters-Social Engineering part 3
FREE A4 Cyber Security Awareness Posters-Social Engineering part 3
Data Hops
 
Choosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptxChoosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptx
Brandon Minnick, MBA
 
Taking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdfTaking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdf
ssuserfac0301
 
Serial Arm Control in Real Time Presentation
Serial Arm Control in Real Time PresentationSerial Arm Control in Real Time Presentation
Serial Arm Control in Real Time Presentation
tolgahangng
 
Monitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdfMonitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdf
Tosin Akinosho
 
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development ProvidersYour One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
akankshawande
 
zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...
zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...
zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...
Alex Pruden
 
GraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracyGraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracy
Tomaz Bratanic
 
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
alexjohnson7307
 
Freshworks Rethinks NoSQL for Rapid Scaling & Cost-Efficiency
Freshworks Rethinks NoSQL for Rapid Scaling & Cost-EfficiencyFreshworks Rethinks NoSQL for Rapid Scaling & Cost-Efficiency
Freshworks Rethinks NoSQL for Rapid Scaling & Cost-Efficiency
ScyllaDB
 
Skybuffer AI: Advanced Conversational and Generative AI Solution on SAP Busin...
Skybuffer AI: Advanced Conversational and Generative AI Solution on SAP Busin...Skybuffer AI: Advanced Conversational and Generative AI Solution on SAP Busin...
Skybuffer AI: Advanced Conversational and Generative AI Solution on SAP Busin...
Tatiana Kojar
 
Introduction of Cybersecurity with OSS at Code Europe 2024
Introduction of Cybersecurity with OSS  at Code Europe 2024Introduction of Cybersecurity with OSS  at Code Europe 2024
Introduction of Cybersecurity with OSS at Code Europe 2024
Hiroshi SHIBATA
 
GNSS spoofing via SDR (Criptored Talks 2024)
GNSS spoofing via SDR (Criptored Talks 2024)GNSS spoofing via SDR (Criptored Talks 2024)
GNSS spoofing via SDR (Criptored Talks 2024)
Javier Junquera
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc
 
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Jeffrey Haguewood
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
panagenda
 

Recently uploaded (20)

Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
 
Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024
 
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
 
Skybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoptionSkybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoption
 
FREE A4 Cyber Security Awareness Posters-Social Engineering part 3
FREE A4 Cyber Security Awareness  Posters-Social Engineering part 3FREE A4 Cyber Security Awareness  Posters-Social Engineering part 3
FREE A4 Cyber Security Awareness Posters-Social Engineering part 3
 
Choosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptxChoosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptx
 
Taking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdfTaking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdf
 
Serial Arm Control in Real Time Presentation
Serial Arm Control in Real Time PresentationSerial Arm Control in Real Time Presentation
Serial Arm Control in Real Time Presentation
 
Monitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdfMonitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdf
 
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development ProvidersYour One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
 
zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...
zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...
zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...
 
GraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracyGraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracy
 
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
 
Freshworks Rethinks NoSQL for Rapid Scaling & Cost-Efficiency
Freshworks Rethinks NoSQL for Rapid Scaling & Cost-EfficiencyFreshworks Rethinks NoSQL for Rapid Scaling & Cost-Efficiency
Freshworks Rethinks NoSQL for Rapid Scaling & Cost-Efficiency
 
Skybuffer AI: Advanced Conversational and Generative AI Solution on SAP Busin...
Skybuffer AI: Advanced Conversational and Generative AI Solution on SAP Busin...Skybuffer AI: Advanced Conversational and Generative AI Solution on SAP Busin...
Skybuffer AI: Advanced Conversational and Generative AI Solution on SAP Busin...
 
Introduction of Cybersecurity with OSS at Code Europe 2024
Introduction of Cybersecurity with OSS  at Code Europe 2024Introduction of Cybersecurity with OSS  at Code Europe 2024
Introduction of Cybersecurity with OSS at Code Europe 2024
 
GNSS spoofing via SDR (Criptored Talks 2024)
GNSS spoofing via SDR (Criptored Talks 2024)GNSS spoofing via SDR (Criptored Talks 2024)
GNSS spoofing via SDR (Criptored Talks 2024)
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
 
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
 

Rails 3 generators

  • 2. About Me ✤ Josh Moore ( ✤ www.codingforrent.com ✤ twitter.com/codingforrent ✤ http://github.com/joshsmoore ✤ Ruby ✤ Watir at work ✤ Rails on the Google App Engine for hobby ✤ maintain rails_dm_datastore gem
  • 3. Does anybody mind if I use English?
  • 4. Contents ✤ Why you need Rails 3 generators ✤ What is new with Rails 3 generators /thor ✤ Creating your own generator ✤ Demo
  • 7. The Problem ✤ You cannot deviate from the rails convection without losing productivity. ✤ Who uses something besides the normal rails libraries? ✤ ERB, ActiveRecord, Test::Unit, fixtures ✤ The generators are not nearly as nice as they used to be.
  • 8. Are you sure this is a big deal? ✤ I use DataMapper, Haml ✤ Generator for views does not work ✤ Generator for models does not work ✤ Scaffold does not work
  • 9. Fixing it ✤ Pretty much impossible in Rails 2 ✤ You could make your own generator, but not integrated different names or syntax
  • 10. Rails 3 goal ✤ Modularity - Easy to switch components ✤ But, the generators still produce the same objects by default ✤ ActiveRecord ✤ TestUnit ✤ ERB
  • 11. How does Rails 3 help? ✤ Meta Generators ✤ Scaffold is now a meta generator ✤ Hooks ✤ Internals are complete new - rebuilt with Thor
  • 12. How does that apply to me? ✤ Demo
  • 13. Scaffold Meta Generator ✤ Actually does not generator anything, just hooks into other generators ✤ Scaffold - orm, scaffold-controller ✤ ActiveRecord - test-framework ✤ TestUnit - fixture-replacement ✤ ScaffoldController - template-engine
  • 14. List of hooks ✤ test_framework ✤ orm ✤ webrat ✤ performance_tool ✤ resource_controller ✤ generator ✤ template_engine ✤ scaffold_controller ✤ integration_tool ✤ helper
  • 15. Hooks ✤ Allow one generator to invoke another generator ✤ Can control what generator is invoked ✤ Allow you to override the defaults ✤ Create custom hooks if you want to
  • 16. Override the defaults ✤ For each command ✤ --template-engine=haml ✤ --orm=datamapper ✤ Change the default in config/application.rb config.generators do |g| g.orm :datamapper g.template_engine :haml g.test_framework :test_unit, :fixture_replacement => :factory_girl end
  • 17. Thor ✤ “Map options to a class. Simply create a class with the appropriate annotations and have options automatically map to functions and parameters.” ✤ ✤ Plan English: Replacement for Rake and sake with a sane way to pass parameters, which means it makes it easy to map command line arguments to parameters and methods in a Ruby class
  • 18. > thor app:install myname --force > thor app -L class App < Thor map "-L" => :list desc "install APP_NAME", "install one of the available apps" method_options :force => :boolean, :alias => :string def install(name) user_alias = options[:alias] if options.force? puts "forcing deletion" end puts 'installing' end desc "list [SEARCH]", "list all of the available apps, limited by SEARCH" def list(search="") # list everything if search == "" puts "searching everything" else puts "searching for #{search}" end end end
  • 19. Things that are hard in Rake ✤ --force - boolean parameters are harder ✤ mapping -L to the app:list action ✤ parameters mapped to Ruby types (boolean, string, number, array, and hash)
  • 20. What does this mean for Rails generators? ✤ Thor::Actions ✤ General purpose actions ✤ Rails::Generators::Actions ✤ actions specific to rails ✤ Application templates and Rails generators now share same API (Thor::Actions)
  • 21. Building your own generator
  • 22. There’s a generator for that ✤ rails g generator NAME ✤ creates these files ✤ haml/ ✤ haml_generator.rb - Gets called when you execute the generator ✤ USAGE - Describes the generator ✤ templates/ - templates go here
  • 23. haml_generator.rb Explained Code ✤ Place your code in instance methods ✤ It has access to all arguments and options ✤ All public instance methods will be executed (probably in the order that the methods are defined in) ✤ Inherits from Thor::Group
  • 24. haml_generator.rb Explained Parameters ✤ Arguments and options ✤ Arguments are non-named parameters that come first ✤ Options are named parameters that come last ✤ Straight from Thor
  • 25. Arguments ✤ Described at the top of your class ✤ The order they are declared is the order they will be accepted on the command line
  • 26. Arguments Example class HamlGenerator < Rails::Generators::NamedBase argument :test_arg, :type => :numeric, :banner => '<number of units>' argument :test_arg3, :type => :string, :required => false, :default => 'default'
  • 27. Declaring Arguments ✤ :desc - Description for the argument. ✤ :required, :optional - If the argument is required or not. ✤ :type - The type of the argument, can be ✤ :string, :hash, :array, :numeric. ✤ :default - Default value for this argument. It cannot be required and have default values. ✤ :banner - String that shows the input format
  • 28. Argument Rules ✤ Once an argument is declared optional all remaining arguments must be optional ✤ you cannot “miss” an optional argument ✤ Once an array argument is declared it will parse everything option after it into the array ✤ Access the value of the argument by calling its name (attr_accessor) ✤ Required by default
  • 29. Options ✤ Also described at the top of your class ✤ But, the order does not matter
  • 30. Option Example class HamlGenerator < Rails::Generators::NamedBase class_option :test_arg, :type => :numeric, :banner => '<number of units>' class_option :test_arg3, :type => :string, :required => false, :default => 'default'
  • 31. Declaring a class_option ✤ :desc - Description for the argument. ✤ :required, :optional - If the argument is required or not. ✤ :type - The type of the argument, can be ✤ :boolean, :numeric, :hash, :array, :string ✤ :default - Default value for this argument. It cannot be required and have default values. ✤ :banner - String that shows the input format
  • 32. class_option rules ✤ use the --name-of-class-option to set the class option ✤ optional by default ✤ underscores (_) in option name become dashes (-)
  • 33. Implementing Hooks ✤ simply call hook_for :<NAME> ✤ switches provided --[no|skip]-test-framework ✤ under the hood ✤ creates a class_option ✤ invokes “invoke_from_option”

Editor's Notes