SlideShare ist ein Scribd-Unternehmen logo
1 von 38
Rails Summer of Code
                                     Week 2




Richard Schneeman - @ThinkBohemian
Rails - Week 2
                 • Ruby
                  • Hashes
                 • Classes
                    • Macros
                    • Methods
                  • Instances
                    • Methods
Richard Schneeman - @ThinkBohemian
Ruby
            • Hashes - (Like a Struct)
             • Key - Value Pairs - Different DataTypes Ok
               hash = {:a => 100, :b => “hello”}
               >> hash[:a]
                => 100
               >> hash[:b]
                => hello
               >> hash.keys
                => [:b, :a]

Richard Schneeman - @ThinkBohemian
Ruby
            • Hashes in method parameters
             • options (a hash) is optional parameter
               def myMethod(variable, options = {})
                  puts variable
                  options.keys.each do |key|
                    puts options[key]
                  end
               end

               >> myMethod(“Hi”)
                 => Hi
               >> myMethod(“Hi”, :a => “hello” , “there”)
                 => Hi
                    Hello
                    There
Richard Schneeman - @ThinkBohemian
Hashes in Rails
          • Used heavily
           • options (a hash) is optional parameter




        Rails API:   (ActionView::Helpers::FormHelper) text_area
Richard Schneeman - @ThinkBohemian
Hashes in Rails
          • Used heavily
           • options (a hash) is optional parameter




        Rails API:   (ActionView::Helpers::FormHelper) text_area
Richard Schneeman - @ThinkBohemian
Ruby
            • Objects don’t have attributes
             • Need getter & setter methods
                class MyClass
                   def myAttribute=(value)
                      @myAttribute = value
                   end

                  def myAttribute
                     @myAttribute
                  end
                end
                >> object = MyClass.new
                >> object.myAttribute = “foo”
                >> object.myAttribute
                  => “foo”

Richard Schneeman - @ThinkBohemian
Ruby
            • Ruby gives us Class Macros !!
             • Example: attr_accessor
                class MyClass
                   attr_accessor :myAttribute
                end

                >> object = MyClass.new
                >> object.myAttribute = “foo”
                >> object.myAttribute
                  => “foo”




Richard Schneeman - @ThinkBohemian
Ruby
            • Class Methods
             • (methods that can be run on the class)
                class My_class
                  def myInstanceMethod(value)
              
      puts value
                  end
              
                  def self.myClassMethod(value)
              
     puts value
                  end
                end


                My_class.myClassMethod("foo")
                => "foo"

Richard Schneeman - @ThinkBohemian
Ruby
            • Class Methods
             • (methods that can be run on the class)
                class My_class
                  def myInstanceMethod(value)
              
      puts value
                  end
              
                  def self.myClassMethod(value)
              
     puts value
                  end
                end


                My_class.myClassMethod("foo")
                => "foo"

Richard Schneeman - @ThinkBohemian
Ruby
         • Instance Methods
                class My_class
                  def myInstanceMethod(value)
              
      puts value
                  end
              
                  def self.myClassMethod(value)
              
     puts value
                  end
                end

       >> myInstance = My_class.new
       >> myInstance.myInstanceMethod("foo")
         => “foo”
       >> myInstance.myClassMethod(“bar”)
         => undefined method ‘myClassMethod’ for #<My_class:0x100124d18>


Richard Schneeman - @ThinkBohemian
Ruby
         • Instance Methods
                class My_class
                  def myInstanceMethod(value)
              
      puts value
                  end
              
                  def self.myClassMethod(value)
              
     puts value
                  end
                end

       >> myInstance = My_class.new
       >> myInstance.myInstanceMethod("foo")
         => “foo”
       >> myInstance.myClassMethod(“bar”)
         => undefined method ‘myClassMethod’ for #<My_class:0x100124d18>


Richard Schneeman - @ThinkBohemian
Ruby
         • Instance Methods
                class My_class
                  def myInstanceMethod(value)
              
      puts value
                  end
              
                  def self.myClassMethod(value)
              
     puts value
                  end
                end

       >> myInstance = My_class.new
       >> myInstance.myInstanceMethod("foo")
         => “foo”
       >> myInstance.myClassMethod(“bar”)
         => undefined method ‘myClassMethod’ for #<My_class:0x100124d18>


Richard Schneeman - @ThinkBohemian
Ruby
                • Class Methods               class Dog

                 • Have self
                                                def self.find(name)
                                            
     ...
                                                end

                • Instance Methods          
                                               def wag_tail(frequency)
                                                  ...
                 • Don’t (Have self)        
                                               end

                                              end




Richard Schneeman - @ThinkBohemian
Ruby
                • Class Methods               class Dog

                 • Have self
                                                def self.find(name)
                                            
     ...
                                                end

                • Instance Methods          
                                               def wag_tail(frequency)
                                                  ...
                 • Don’t (Have self)        
                                               end

                                              end




Richard Schneeman - @ThinkBohemian
Ruby
            • attr_accessor is a Class method
                attr_accessor :myAttribute



                               Can be defined as:

                def self.attr_accessor(value)
                   ...
                end


                                           cool !

Richard Schneeman - @ThinkBohemian
Rails - Week 2
                 • Code Generation
                  • Migrations
                  • Scaffolding
                 • Validation
                 • Testing (Rspec AutoTest)

Richard Schneeman - @ThinkBohemian
Scaffolding
                 •   Generate Model, View, and Controller & Migration


>> rails generate scaffold Post name:string title:string content:text




                 •   Generates “One Size Fits All” code

                     •   app/models/post.rb

                     •   app/controllers/posts_controller.rb

                     •   app/views/posts/ {index, show, new, create, destroy}

                 •   Modify to suite needs (convention over configuration)
Richard Schneeman - @ThinkBohemian
Migrations
                  •   Create your data structure in your Database

                      •   Set a database’s schema
        migrate/create_post.rb

           class CreatePost < ActiveRecord::Migration
             def self.up
               create_table :post do |t|
                 t.string :name
                 t.string :title
                 t.text    :content
               end

               SystemSetting.create :name => "notice", :label => "Use notice?", :value => 1
             end

             def self.down
               drop_table :posts
             end
           end




Richard Schneeman - @ThinkBohemian
Migrations
                 •   Get your data structure into your database


                 >> rake db:migrate

                 •   Runs all “Up” migrations

           def self.up
            create_table   :post do |t|
               t.string    :name
               t.string    :title
               t.text      :content
             end
           end




Richard Schneeman - @ThinkBohemian
Migrations
        •   Creates a Table named post    def self.up
                                           create_table   :post do |t|
        •   Adds Columns                      t.string
                                              t.string
                                                          :name
                                                          :title

            •
                                              t.text      :content
                Name, Title, Content        end
                                          end

            •   created_at & updated_at
                added automatically




Richard Schneeman - @ThinkBohemian
Migrations
        •   Creates a Table named post    def self.up
                                           create_table   :post do |t|
        •   Adds Columns                      t.string
                                              t.string
                                                          :name
                                                          :title

            •
                                              t.text      :content
                Name, Title, Content        end
                                          end

            •   created_at & updated_at
                added automatically




Richard Schneeman - @ThinkBohemian
Migrations
                 •   Active Record maps ruby objects to database

                     •     Post.title

                                          Active Record is Rail’s ORM


           def self.up
            create_table   :post do |t|
               t.string    :name
               t.string    :title
               t.text      :content
             end
           end




Richard Schneeman - @ThinkBohemian
Migrations
                 •   Active Record maps ruby objects to database

                     •     Post.title

                                          Active Record is Rail’s ORM


           def self.up
            create_table   :post do |t|
               t.string    :name
               t.string    :title
               t.text      :content
             end
           end




Richard Schneeman - @ThinkBohemian
Migrations
                 •   Active Record maps ruby objects to database

                     •     Post.title

                                          Active Record is Rail’s ORM


           def self.up
            create_table   :post do |t|
               t.string    :name
               t.string    :title
               t.text      :content
             end
           end




Richard Schneeman - @ThinkBohemian
Migrations
                 •   Active Record maps ruby objects to database

                     •     Post.title

                                          Active Record is Rail’s ORM


           def self.up
            create_table   :post do |t|
               t.string    :name
               t.string    :title
               t.text      :content
             end
           end




Richard Schneeman - @ThinkBohemian
Migrations
                 •   Active Record maps ruby objects to database

                     •     Post.title

                                          Active Record is Rail’s ORM


           def self.up
            create_table   :post do |t|
               t.string    :name
               t.string    :title
               t.text      :content
             end
           end




Richard Schneeman - @ThinkBohemian
Migrations
                 •   Made a mistake? Issue a database Ctrl + Z !


                 >> rake db:rollback

                 •   Runs last “down” migration


                              def self.down
                                 drop_table :system_settings
                               end




Richard Schneeman - @ThinkBohemian
Scaffolding
                 •   Generate Model, View, and Controller


        >> scaffold Post name:string title:string content:text


                     •   models/post.rb

                     •   controllers/posts_controller.rb

                     •   views/post/{ index.html, show.html, new.html, create.html }




Richard Schneeman - @ThinkBohemian
Rails - Validations
                 •     Check your parameters before save

                      •   Provided by ActiveModel

                          •   Utilized by ActiveRecord

                     class Person < ActiveRecord::Base
                       validates :title, :presence => true, :title => true
                     end




                     bob = Person.create(:title => nil)
                     >> bob.valid?
                       => false
                     >> bob.save
                       => false




Richard Schneeman - @ThinkBohemian
Rails - Validations
               Can use ActiveModel without Rails

                   class Person
                     include ActiveModel::Validations
                     attr_accessor :title
                     validates :title, :presence => true, :title => true
                   end




                   bob = Person.create(:title => nil)
                   >> bob.valid?
                     => false
                   >> bob.save
                     => false




Richard Schneeman - @ThinkBohemian
Rails - Validations
                 •       Use Rail’s built in Validations
                     #   :acceptance => Boolean.
                     #   :confirmation => Boolean.
                     #   :exclusion => { :in => Ennumerable }.
                     #   :inclusion => { :in => Ennumerable }.
                     #   :format => { :with => Regexp, :on => :create }.
                     #   :length => { :maximum => Fixnum }.
                     #   :numericality => Boolean.
                     #   :presence => Boolean.
                     #   :uniqueness => Boolean.


                 •       Write your Own Validations
         class User < ActiveRecord::Base
           validate :my_custom_validation

           private
             def my_custom_validation
               self.errors.add(:password, "Custom Message") unless self.property.present?
             end
         end


Richard Schneeman - @ThinkBohemian
Testing
            • Test your code (or wish you did)
            • Makes upgrading and refactoring easier
            • Different Environments
             • production - live on the web
             • development - on your local computer
             • test - local clean environment
Richard Schneeman - @ThinkBohemian
Testing
         • Unit Tests
          • Test individual methods against known inputs
         • Integration Tests
          • Test Multiple Controllers
         • Functional Tests
          • Test full stack M- V-C
Richard Schneeman - @ThinkBohemian
Unit Testing
            • ActiveSupport::TestCase
            • Provides assert statements
             • assert true
             • assert (variable)
             • assert_same( obj1, obj2 )
             • many more
Richard Schneeman - @ThinkBohemian
Unit Testing
            • Run Unit Tests using:
                   >> rake test:units RAILS_ENV=test




Richard Schneeman - @ThinkBohemian
Unit Testing
            • Run Tests automatically the background
             • ZenTest
               • sudo gem install ZenTest
               • Run:
                              >> autotest




Richard Schneeman - @ThinkBohemian
Questions?




Richard Schneeman - @ThinkBohemian

Weitere ähnliche Inhalte

Was ist angesagt?

Ruby :: Training 1
Ruby :: Training 1Ruby :: Training 1
Ruby :: Training 1Pavel Tyk
 
Ruby Xml Mapping
Ruby Xml MappingRuby Xml Mapping
Ruby Xml MappingMarc Seeger
 
Php Crash Course - Macq Electronique 2010
Php Crash Course - Macq Electronique 2010Php Crash Course - Macq Electronique 2010
Php Crash Course - Macq Electronique 2010Michelangelo van Dam
 
Swift in SwiftUI
Swift in SwiftUISwift in SwiftUI
Swift in SwiftUIBongwon Lee
 
오브젝트C(pdf)
오브젝트C(pdf)오브젝트C(pdf)
오브젝트C(pdf)sunwooindia
 
Scala For Java Programmers
Scala For Java ProgrammersScala For Java Programmers
Scala For Java ProgrammersEnno Runne
 
From android/java to swift (3)
From android/java to swift (3)From android/java to swift (3)
From android/java to swift (3)allanh0526
 
What's a macro?: Learning by Examples / Scalaのマクロに実用例から触れてみよう!
What's a macro?: Learning by Examples / Scalaのマクロに実用例から触れてみよう!What's a macro?: Learning by Examples / Scalaのマクロに実用例から触れてみよう!
What's a macro?: Learning by Examples / Scalaのマクロに実用例から触れてみよう!scalaconfjp
 
Nikita Popov "What’s new in PHP 8.0?"
Nikita Popov "What’s new in PHP 8.0?"Nikita Popov "What’s new in PHP 8.0?"
Nikita Popov "What’s new in PHP 8.0?"Fwdays
 
From Ruby to Scala
From Ruby to ScalaFrom Ruby to Scala
From Ruby to Scalatod esking
 
Rails for PHP Developers
Rails for PHP DevelopersRails for PHP Developers
Rails for PHP DevelopersRobert Dempsey
 
Selfish presentation - ruby internals
Selfish presentation - ruby internalsSelfish presentation - ruby internals
Selfish presentation - ruby internalsWojciech Widenka
 

Was ist angesagt? (17)

Ruby :: Training 1
Ruby :: Training 1Ruby :: Training 1
Ruby :: Training 1
 
Ruby Xml Mapping
Ruby Xml MappingRuby Xml Mapping
Ruby Xml Mapping
 
Php Crash Course - Macq Electronique 2010
Php Crash Course - Macq Electronique 2010Php Crash Course - Macq Electronique 2010
Php Crash Course - Macq Electronique 2010
 
Swift Basics
Swift BasicsSwift Basics
Swift Basics
 
Swift in SwiftUI
Swift in SwiftUISwift in SwiftUI
Swift in SwiftUI
 
오브젝트C(pdf)
오브젝트C(pdf)오브젝트C(pdf)
오브젝트C(pdf)
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
 
Scala For Java Programmers
Scala For Java ProgrammersScala For Java Programmers
Scala For Java Programmers
 
From android/java to swift (3)
From android/java to swift (3)From android/java to swift (3)
From android/java to swift (3)
 
What's a macro?: Learning by Examples / Scalaのマクロに実用例から触れてみよう!
What's a macro?: Learning by Examples / Scalaのマクロに実用例から触れてみよう!What's a macro?: Learning by Examples / Scalaのマクロに実用例から触れてみよう!
What's a macro?: Learning by Examples / Scalaのマクロに実用例から触れてみよう!
 
Quick swift tour
Quick swift tourQuick swift tour
Quick swift tour
 
Scala. Inception.
Scala. Inception.Scala. Inception.
Scala. Inception.
 
Nikita Popov "What’s new in PHP 8.0?"
Nikita Popov "What’s new in PHP 8.0?"Nikita Popov "What’s new in PHP 8.0?"
Nikita Popov "What’s new in PHP 8.0?"
 
RoR_2_Ruby
RoR_2_RubyRoR_2_Ruby
RoR_2_Ruby
 
From Ruby to Scala
From Ruby to ScalaFrom Ruby to Scala
From Ruby to Scala
 
Rails for PHP Developers
Rails for PHP DevelopersRails for PHP Developers
Rails for PHP Developers
 
Selfish presentation - ruby internals
Selfish presentation - ruby internalsSelfish presentation - ruby internals
Selfish presentation - ruby internals
 

Mehr von Richard Schneeman

Scaling the Web: Databases & NoSQL
Scaling the Web: Databases & NoSQLScaling the Web: Databases & NoSQL
Scaling the Web: Databases & NoSQLRichard Schneeman
 
Rails 3 Beginner to Builder 2011 Week 8
Rails 3 Beginner to Builder 2011 Week 8Rails 3 Beginner to Builder 2011 Week 8
Rails 3 Beginner to Builder 2011 Week 8Richard Schneeman
 
Rails 3 Beginner to Builder 2011 Week 6
Rails 3 Beginner to Builder 2011 Week 6Rails 3 Beginner to Builder 2011 Week 6
Rails 3 Beginner to Builder 2011 Week 6Richard Schneeman
 
Rails 3 Beginner to Builder 2011 Week 5
Rails 3 Beginner to Builder 2011 Week 5Rails 3 Beginner to Builder 2011 Week 5
Rails 3 Beginner to Builder 2011 Week 5Richard Schneeman
 
Rails 3 Beginner to Builder 2011 Week 4
Rails 3 Beginner to Builder 2011 Week 4Rails 3 Beginner to Builder 2011 Week 4
Rails 3 Beginner to Builder 2011 Week 4Richard Schneeman
 
Rails 3 Beginner to Builder 2011 Week 1
Rails 3 Beginner to Builder 2011 Week 1Rails 3 Beginner to Builder 2011 Week 1
Rails 3 Beginner to Builder 2011 Week 1Richard Schneeman
 
Rails3 Summer of Code 2010 - Week 6
Rails3 Summer of Code 2010 - Week 6Rails3 Summer of Code 2010 - Week 6
Rails3 Summer of Code 2010 - Week 6Richard Schneeman
 
Rails3 Summer of Code 2010- Week 5
Rails3 Summer of Code 2010- Week 5Rails3 Summer of Code 2010- Week 5
Rails3 Summer of Code 2010- Week 5Richard Schneeman
 

Mehr von Richard Schneeman (11)

Scaling the Web: Databases & NoSQL
Scaling the Web: Databases & NoSQLScaling the Web: Databases & NoSQL
Scaling the Web: Databases & NoSQL
 
Rails 3 Beginner to Builder 2011 Week 8
Rails 3 Beginner to Builder 2011 Week 8Rails 3 Beginner to Builder 2011 Week 8
Rails 3 Beginner to Builder 2011 Week 8
 
Rails 3 Beginner to Builder 2011 Week 6
Rails 3 Beginner to Builder 2011 Week 6Rails 3 Beginner to Builder 2011 Week 6
Rails 3 Beginner to Builder 2011 Week 6
 
Rails 3 Beginner to Builder 2011 Week 5
Rails 3 Beginner to Builder 2011 Week 5Rails 3 Beginner to Builder 2011 Week 5
Rails 3 Beginner to Builder 2011 Week 5
 
Rails 3 Beginner to Builder 2011 Week 4
Rails 3 Beginner to Builder 2011 Week 4Rails 3 Beginner to Builder 2011 Week 4
Rails 3 Beginner to Builder 2011 Week 4
 
Rails 3 Beginner to Builder 2011 Week 1
Rails 3 Beginner to Builder 2011 Week 1Rails 3 Beginner to Builder 2011 Week 1
Rails 3 Beginner to Builder 2011 Week 1
 
Potential Friend Finder
Potential Friend FinderPotential Friend Finder
Potential Friend Finder
 
Rails3 Summer of Code 2010 - Week 6
Rails3 Summer of Code 2010 - Week 6Rails3 Summer of Code 2010 - Week 6
Rails3 Summer of Code 2010 - Week 6
 
Rails3 Summer of Code 2010- Week 5
Rails3 Summer of Code 2010- Week 5Rails3 Summer of Code 2010- Week 5
Rails3 Summer of Code 2010- Week 5
 
UT on Rails3 2010- Week 4
UT on Rails3 2010- Week 4 UT on Rails3 2010- Week 4
UT on Rails3 2010- Week 4
 
UT on Rails3 2010- Week 1
UT on Rails3 2010- Week 1UT on Rails3 2010- Week 1
UT on Rails3 2010- Week 1
 

Kürzlich hochgeladen

Top Rated Pune Call Girls Tingre Nagar ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Tingre Nagar ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Tingre Nagar ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Tingre Nagar ⟟ 6297143586 ⟟ Call Me For Genuine Se...Call Girls in Nagpur High Profile
 
2k Shots ≽ 9205541914 ≼ Call Girls In Dashrath Puri (Delhi)
2k Shots ≽ 9205541914 ≼ Call Girls In Dashrath Puri (Delhi)2k Shots ≽ 9205541914 ≼ Call Girls In Dashrath Puri (Delhi)
2k Shots ≽ 9205541914 ≼ Call Girls In Dashrath Puri (Delhi)Delhi Call girls
 
2k Shots ≽ 9205541914 ≼ Call Girls In Mukherjee Nagar (Delhi)
2k Shots ≽ 9205541914 ≼ Call Girls In Mukherjee Nagar (Delhi)2k Shots ≽ 9205541914 ≼ Call Girls In Mukherjee Nagar (Delhi)
2k Shots ≽ 9205541914 ≼ Call Girls In Mukherjee Nagar (Delhi)Delhi Call girls
 
call Now 9811711561 Cash Payment乂 Call Girls in Dwarka Mor
call Now 9811711561 Cash Payment乂 Call Girls in Dwarka Morcall Now 9811711561 Cash Payment乂 Call Girls in Dwarka Mor
call Now 9811711561 Cash Payment乂 Call Girls in Dwarka Morvikas rana
 
The Selfspace Journal Preview by Mindbrush
The Selfspace Journal Preview by MindbrushThe Selfspace Journal Preview by Mindbrush
The Selfspace Journal Preview by MindbrushShivain97
 
Pokemon Go... Unraveling the Conspiracy Theory
Pokemon Go... Unraveling the Conspiracy TheoryPokemon Go... Unraveling the Conspiracy Theory
Pokemon Go... Unraveling the Conspiracy Theorydrae5
 
9892124323, Call Girls in mumbai, Vashi Call Girls , Kurla Call girls
9892124323, Call Girls in mumbai, Vashi Call Girls , Kurla Call girls9892124323, Call Girls in mumbai, Vashi Call Girls , Kurla Call girls
9892124323, Call Girls in mumbai, Vashi Call Girls , Kurla Call girlsPooja Nehwal
 
$ Love Spells^ 💎 (310) 882-6330 in West Virginia, WV | Psychic Reading Best B...
$ Love Spells^ 💎 (310) 882-6330 in West Virginia, WV | Psychic Reading Best B...$ Love Spells^ 💎 (310) 882-6330 in West Virginia, WV | Psychic Reading Best B...
$ Love Spells^ 💎 (310) 882-6330 in West Virginia, WV | Psychic Reading Best B...PsychicRuben LoveSpells
 
2k Shots ≽ 9205541914 ≼ Call Girls In Palam (Delhi)
2k Shots ≽ 9205541914 ≼ Call Girls In Palam (Delhi)2k Shots ≽ 9205541914 ≼ Call Girls In Palam (Delhi)
2k Shots ≽ 9205541914 ≼ Call Girls In Palam (Delhi)Delhi Call girls
 
LC_YouSaidYes_NewBelieverBookletDone.pdf
LC_YouSaidYes_NewBelieverBookletDone.pdfLC_YouSaidYes_NewBelieverBookletDone.pdf
LC_YouSaidYes_NewBelieverBookletDone.pdfpastor83
 
2k Shots ≽ 9205541914 ≼ Call Girls In Jasola (Delhi)
2k Shots ≽ 9205541914 ≼ Call Girls In Jasola (Delhi)2k Shots ≽ 9205541914 ≼ Call Girls In Jasola (Delhi)
2k Shots ≽ 9205541914 ≼ Call Girls In Jasola (Delhi)Delhi Call girls
 
WOMEN EMPOWERMENT women empowerment.pptx
WOMEN EMPOWERMENT women empowerment.pptxWOMEN EMPOWERMENT women empowerment.pptx
WOMEN EMPOWERMENT women empowerment.pptxpadhand000
 
8377087607 Full Enjoy @24/7-CLEAN-Call Girls In Chhatarpur,
8377087607 Full Enjoy @24/7-CLEAN-Call Girls In Chhatarpur,8377087607 Full Enjoy @24/7-CLEAN-Call Girls In Chhatarpur,
8377087607 Full Enjoy @24/7-CLEAN-Call Girls In Chhatarpur,dollysharma2066
 

Kürzlich hochgeladen (15)

Top Rated Pune Call Girls Tingre Nagar ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Tingre Nagar ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Tingre Nagar ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Tingre Nagar ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
2k Shots ≽ 9205541914 ≼ Call Girls In Dashrath Puri (Delhi)
2k Shots ≽ 9205541914 ≼ Call Girls In Dashrath Puri (Delhi)2k Shots ≽ 9205541914 ≼ Call Girls In Dashrath Puri (Delhi)
2k Shots ≽ 9205541914 ≼ Call Girls In Dashrath Puri (Delhi)
 
2k Shots ≽ 9205541914 ≼ Call Girls In Mukherjee Nagar (Delhi)
2k Shots ≽ 9205541914 ≼ Call Girls In Mukherjee Nagar (Delhi)2k Shots ≽ 9205541914 ≼ Call Girls In Mukherjee Nagar (Delhi)
2k Shots ≽ 9205541914 ≼ Call Girls In Mukherjee Nagar (Delhi)
 
call Now 9811711561 Cash Payment乂 Call Girls in Dwarka Mor
call Now 9811711561 Cash Payment乂 Call Girls in Dwarka Morcall Now 9811711561 Cash Payment乂 Call Girls in Dwarka Mor
call Now 9811711561 Cash Payment乂 Call Girls in Dwarka Mor
 
The Selfspace Journal Preview by Mindbrush
The Selfspace Journal Preview by MindbrushThe Selfspace Journal Preview by Mindbrush
The Selfspace Journal Preview by Mindbrush
 
Pokemon Go... Unraveling the Conspiracy Theory
Pokemon Go... Unraveling the Conspiracy TheoryPokemon Go... Unraveling the Conspiracy Theory
Pokemon Go... Unraveling the Conspiracy Theory
 
9892124323, Call Girls in mumbai, Vashi Call Girls , Kurla Call girls
9892124323, Call Girls in mumbai, Vashi Call Girls , Kurla Call girls9892124323, Call Girls in mumbai, Vashi Call Girls , Kurla Call girls
9892124323, Call Girls in mumbai, Vashi Call Girls , Kurla Call girls
 
$ Love Spells^ 💎 (310) 882-6330 in West Virginia, WV | Psychic Reading Best B...
$ Love Spells^ 💎 (310) 882-6330 in West Virginia, WV | Psychic Reading Best B...$ Love Spells^ 💎 (310) 882-6330 in West Virginia, WV | Psychic Reading Best B...
$ Love Spells^ 💎 (310) 882-6330 in West Virginia, WV | Psychic Reading Best B...
 
2k Shots ≽ 9205541914 ≼ Call Girls In Palam (Delhi)
2k Shots ≽ 9205541914 ≼ Call Girls In Palam (Delhi)2k Shots ≽ 9205541914 ≼ Call Girls In Palam (Delhi)
2k Shots ≽ 9205541914 ≼ Call Girls In Palam (Delhi)
 
LC_YouSaidYes_NewBelieverBookletDone.pdf
LC_YouSaidYes_NewBelieverBookletDone.pdfLC_YouSaidYes_NewBelieverBookletDone.pdf
LC_YouSaidYes_NewBelieverBookletDone.pdf
 
2k Shots ≽ 9205541914 ≼ Call Girls In Jasola (Delhi)
2k Shots ≽ 9205541914 ≼ Call Girls In Jasola (Delhi)2k Shots ≽ 9205541914 ≼ Call Girls In Jasola (Delhi)
2k Shots ≽ 9205541914 ≼ Call Girls In Jasola (Delhi)
 
WOMEN EMPOWERMENT women empowerment.pptx
WOMEN EMPOWERMENT women empowerment.pptxWOMEN EMPOWERMENT women empowerment.pptx
WOMEN EMPOWERMENT women empowerment.pptx
 
8377087607 Full Enjoy @24/7-CLEAN-Call Girls In Chhatarpur,
8377087607 Full Enjoy @24/7-CLEAN-Call Girls In Chhatarpur,8377087607 Full Enjoy @24/7-CLEAN-Call Girls In Chhatarpur,
8377087607 Full Enjoy @24/7-CLEAN-Call Girls In Chhatarpur,
 
(Anamika) VIP Call Girls Navi Mumbai Call Now 8250077686 Navi Mumbai Escorts ...
(Anamika) VIP Call Girls Navi Mumbai Call Now 8250077686 Navi Mumbai Escorts ...(Anamika) VIP Call Girls Navi Mumbai Call Now 8250077686 Navi Mumbai Escorts ...
(Anamika) VIP Call Girls Navi Mumbai Call Now 8250077686 Navi Mumbai Escorts ...
 
(Aarini) Russian Call Girls Surat Call Now 8250077686 Surat Escorts 24x7
(Aarini) Russian Call Girls Surat Call Now 8250077686 Surat Escorts 24x7(Aarini) Russian Call Girls Surat Call Now 8250077686 Surat Escorts 24x7
(Aarini) Russian Call Girls Surat Call Now 8250077686 Surat Escorts 24x7
 

UT on Rails3 2010- Week 2

  • 1. Rails Summer of Code Week 2 Richard Schneeman - @ThinkBohemian
  • 2. Rails - Week 2 • Ruby • Hashes • Classes • Macros • Methods • Instances • Methods Richard Schneeman - @ThinkBohemian
  • 3. Ruby • Hashes - (Like a Struct) • Key - Value Pairs - Different DataTypes Ok hash = {:a => 100, :b => “hello”} >> hash[:a] => 100 >> hash[:b] => hello >> hash.keys => [:b, :a] Richard Schneeman - @ThinkBohemian
  • 4. Ruby • Hashes in method parameters • options (a hash) is optional parameter def myMethod(variable, options = {}) puts variable options.keys.each do |key| puts options[key] end end >> myMethod(“Hi”) => Hi >> myMethod(“Hi”, :a => “hello” , “there”) => Hi Hello There Richard Schneeman - @ThinkBohemian
  • 5. Hashes in Rails • Used heavily • options (a hash) is optional parameter Rails API: (ActionView::Helpers::FormHelper) text_area Richard Schneeman - @ThinkBohemian
  • 6. Hashes in Rails • Used heavily • options (a hash) is optional parameter Rails API: (ActionView::Helpers::FormHelper) text_area Richard Schneeman - @ThinkBohemian
  • 7. Ruby • Objects don’t have attributes • Need getter & setter methods class MyClass def myAttribute=(value) @myAttribute = value end def myAttribute @myAttribute end end >> object = MyClass.new >> object.myAttribute = “foo” >> object.myAttribute => “foo” Richard Schneeman - @ThinkBohemian
  • 8. Ruby • Ruby gives us Class Macros !! • Example: attr_accessor class MyClass attr_accessor :myAttribute end >> object = MyClass.new >> object.myAttribute = “foo” >> object.myAttribute => “foo” Richard Schneeman - @ThinkBohemian
  • 9. Ruby • Class Methods • (methods that can be run on the class) class My_class def myInstanceMethod(value) puts value end def self.myClassMethod(value) puts value end end My_class.myClassMethod("foo") => "foo" Richard Schneeman - @ThinkBohemian
  • 10. Ruby • Class Methods • (methods that can be run on the class) class My_class def myInstanceMethod(value) puts value end def self.myClassMethod(value) puts value end end My_class.myClassMethod("foo") => "foo" Richard Schneeman - @ThinkBohemian
  • 11. Ruby • Instance Methods class My_class def myInstanceMethod(value) puts value end def self.myClassMethod(value) puts value end end >> myInstance = My_class.new >> myInstance.myInstanceMethod("foo") => “foo” >> myInstance.myClassMethod(“bar”) => undefined method ‘myClassMethod’ for #<My_class:0x100124d18> Richard Schneeman - @ThinkBohemian
  • 12. Ruby • Instance Methods class My_class def myInstanceMethod(value) puts value end def self.myClassMethod(value) puts value end end >> myInstance = My_class.new >> myInstance.myInstanceMethod("foo") => “foo” >> myInstance.myClassMethod(“bar”) => undefined method ‘myClassMethod’ for #<My_class:0x100124d18> Richard Schneeman - @ThinkBohemian
  • 13. Ruby • Instance Methods class My_class def myInstanceMethod(value) puts value end def self.myClassMethod(value) puts value end end >> myInstance = My_class.new >> myInstance.myInstanceMethod("foo") => “foo” >> myInstance.myClassMethod(“bar”) => undefined method ‘myClassMethod’ for #<My_class:0x100124d18> Richard Schneeman - @ThinkBohemian
  • 14. Ruby • Class Methods class Dog • Have self def self.find(name) ... end • Instance Methods def wag_tail(frequency) ... • Don’t (Have self) end end Richard Schneeman - @ThinkBohemian
  • 15. Ruby • Class Methods class Dog • Have self def self.find(name) ... end • Instance Methods def wag_tail(frequency) ... • Don’t (Have self) end end Richard Schneeman - @ThinkBohemian
  • 16. Ruby • attr_accessor is a Class method attr_accessor :myAttribute Can be defined as: def self.attr_accessor(value) ... end cool ! Richard Schneeman - @ThinkBohemian
  • 17. Rails - Week 2 • Code Generation • Migrations • Scaffolding • Validation • Testing (Rspec AutoTest) Richard Schneeman - @ThinkBohemian
  • 18. Scaffolding • Generate Model, View, and Controller & Migration >> rails generate scaffold Post name:string title:string content:text • Generates “One Size Fits All” code • app/models/post.rb • app/controllers/posts_controller.rb • app/views/posts/ {index, show, new, create, destroy} • Modify to suite needs (convention over configuration) Richard Schneeman - @ThinkBohemian
  • 19. Migrations • Create your data structure in your Database • Set a database’s schema migrate/create_post.rb class CreatePost < ActiveRecord::Migration def self.up create_table :post do |t| t.string :name t.string :title t.text :content end SystemSetting.create :name => "notice", :label => "Use notice?", :value => 1 end def self.down drop_table :posts end end Richard Schneeman - @ThinkBohemian
  • 20. Migrations • Get your data structure into your database >> rake db:migrate • Runs all “Up” migrations def self.up create_table :post do |t| t.string :name t.string :title t.text :content end end Richard Schneeman - @ThinkBohemian
  • 21. Migrations • Creates a Table named post def self.up create_table :post do |t| • Adds Columns t.string t.string :name :title • t.text :content Name, Title, Content end end • created_at & updated_at added automatically Richard Schneeman - @ThinkBohemian
  • 22. Migrations • Creates a Table named post def self.up create_table :post do |t| • Adds Columns t.string t.string :name :title • t.text :content Name, Title, Content end end • created_at & updated_at added automatically Richard Schneeman - @ThinkBohemian
  • 23. Migrations • Active Record maps ruby objects to database • Post.title Active Record is Rail’s ORM def self.up create_table :post do |t| t.string :name t.string :title t.text :content end end Richard Schneeman - @ThinkBohemian
  • 24. Migrations • Active Record maps ruby objects to database • Post.title Active Record is Rail’s ORM def self.up create_table :post do |t| t.string :name t.string :title t.text :content end end Richard Schneeman - @ThinkBohemian
  • 25. Migrations • Active Record maps ruby objects to database • Post.title Active Record is Rail’s ORM def self.up create_table :post do |t| t.string :name t.string :title t.text :content end end Richard Schneeman - @ThinkBohemian
  • 26. Migrations • Active Record maps ruby objects to database • Post.title Active Record is Rail’s ORM def self.up create_table :post do |t| t.string :name t.string :title t.text :content end end Richard Schneeman - @ThinkBohemian
  • 27. Migrations • Active Record maps ruby objects to database • Post.title Active Record is Rail’s ORM def self.up create_table :post do |t| t.string :name t.string :title t.text :content end end Richard Schneeman - @ThinkBohemian
  • 28. Migrations • Made a mistake? Issue a database Ctrl + Z ! >> rake db:rollback • Runs last “down” migration def self.down drop_table :system_settings end Richard Schneeman - @ThinkBohemian
  • 29. Scaffolding • Generate Model, View, and Controller >> scaffold Post name:string title:string content:text • models/post.rb • controllers/posts_controller.rb • views/post/{ index.html, show.html, new.html, create.html } Richard Schneeman - @ThinkBohemian
  • 30. Rails - Validations • Check your parameters before save • Provided by ActiveModel • Utilized by ActiveRecord class Person < ActiveRecord::Base validates :title, :presence => true, :title => true end bob = Person.create(:title => nil) >> bob.valid? => false >> bob.save => false Richard Schneeman - @ThinkBohemian
  • 31. Rails - Validations Can use ActiveModel without Rails class Person include ActiveModel::Validations attr_accessor :title validates :title, :presence => true, :title => true end bob = Person.create(:title => nil) >> bob.valid? => false >> bob.save => false Richard Schneeman - @ThinkBohemian
  • 32. Rails - Validations • Use Rail’s built in Validations # :acceptance => Boolean. # :confirmation => Boolean. # :exclusion => { :in => Ennumerable }. # :inclusion => { :in => Ennumerable }. # :format => { :with => Regexp, :on => :create }. # :length => { :maximum => Fixnum }. # :numericality => Boolean. # :presence => Boolean. # :uniqueness => Boolean. • Write your Own Validations class User < ActiveRecord::Base validate :my_custom_validation private def my_custom_validation self.errors.add(:password, "Custom Message") unless self.property.present? end end Richard Schneeman - @ThinkBohemian
  • 33. Testing • Test your code (or wish you did) • Makes upgrading and refactoring easier • Different Environments • production - live on the web • development - on your local computer • test - local clean environment Richard Schneeman - @ThinkBohemian
  • 34. Testing • Unit Tests • Test individual methods against known inputs • Integration Tests • Test Multiple Controllers • Functional Tests • Test full stack M- V-C Richard Schneeman - @ThinkBohemian
  • 35. Unit Testing • ActiveSupport::TestCase • Provides assert statements • assert true • assert (variable) • assert_same( obj1, obj2 ) • many more Richard Schneeman - @ThinkBohemian
  • 36. Unit Testing • Run Unit Tests using: >> rake test:units RAILS_ENV=test Richard Schneeman - @ThinkBohemian
  • 37. Unit Testing • Run Tests automatically the background • ZenTest • sudo gem install ZenTest • Run: >> autotest Richard Schneeman - @ThinkBohemian

Hinweis der Redaktion