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

E J Waggoner against Kellogg's Pantheism 8.pptx
E J Waggoner against Kellogg's Pantheism 8.pptxE J Waggoner against Kellogg's Pantheism 8.pptx
E J Waggoner against Kellogg's Pantheism 8.pptxJackieSparrow3
 
Call Girls In Karkardooma 83770 87607 Just-Dial Escorts Service 24X7 Avilable
Call Girls In Karkardooma 83770 87607 Just-Dial Escorts Service 24X7 AvilableCall Girls In Karkardooma 83770 87607 Just-Dial Escorts Service 24X7 Avilable
Call Girls In Karkardooma 83770 87607 Just-Dial Escorts Service 24X7 Avilabledollysharma2066
 
办理西悉尼大学毕业证成绩单、制作假文凭
办理西悉尼大学毕业证成绩单、制作假文凭办理西悉尼大学毕业证成绩单、制作假文凭
办理西悉尼大学毕业证成绩单、制作假文凭o8wvnojp
 
(南达科他州立大学毕业证学位证成绩单-永久存档)
(南达科他州立大学毕业证学位证成绩单-永久存档)(南达科他州立大学毕业证学位证成绩单-永久存档)
(南达科他州立大学毕业证学位证成绩单-永久存档)oannq
 
Call Girls in Govindpuri Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Govindpuri Delhi 💯Call Us 🔝8264348440🔝Call Girls in Govindpuri Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Govindpuri Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
Call Girls In Dwarka Sub City ☎️7838079806 ✅ 💯Call Girls In Delhi
Call Girls In Dwarka Sub City  ☎️7838079806 ✅ 💯Call Girls In DelhiCall Girls In Dwarka Sub City  ☎️7838079806 ✅ 💯Call Girls In Delhi
Call Girls In Dwarka Sub City ☎️7838079806 ✅ 💯Call Girls In DelhiSoniyaSingh
 
南新罕布什尔大学毕业证学位证成绩单-学历认证
南新罕布什尔大学毕业证学位证成绩单-学历认证南新罕布什尔大学毕业证学位证成绩单-学历认证
南新罕布什尔大学毕业证学位证成绩单-学历认证kbdhl05e
 
Authentic No 1 Amil Baba In Pakistan Amil Baba In Faisalabad Amil Baba In Kar...
Authentic No 1 Amil Baba In Pakistan Amil Baba In Faisalabad Amil Baba In Kar...Authentic No 1 Amil Baba In Pakistan Amil Baba In Faisalabad Amil Baba In Kar...
Authentic No 1 Amil Baba In Pakistan Amil Baba In Faisalabad Amil Baba In Kar...Authentic No 1 Amil Baba In Pakistan
 
Inspiring Through Words Power of Inspiration.pptx
Inspiring Through Words Power of Inspiration.pptxInspiring Through Words Power of Inspiration.pptx
Inspiring Through Words Power of Inspiration.pptxShubham Rawat
 
西伦敦大学毕业证学位证成绩单-怎么样做
西伦敦大学毕业证学位证成绩单-怎么样做西伦敦大学毕业证学位证成绩单-怎么样做
西伦敦大学毕业证学位证成绩单-怎么样做j5bzwet6
 
(No.1)↠Young Call Girls in Sikanderpur (Gurgaon) ꧁❤ 9711911712 ❤꧂ Escorts
(No.1)↠Young Call Girls in Sikanderpur (Gurgaon) ꧁❤ 9711911712 ❤꧂ Escorts(No.1)↠Young Call Girls in Sikanderpur (Gurgaon) ꧁❤ 9711911712 ❤꧂ Escorts
(No.1)↠Young Call Girls in Sikanderpur (Gurgaon) ꧁❤ 9711911712 ❤꧂ EscortsDelhi Escorts Service
 

Kürzlich hochgeladen (12)

E J Waggoner against Kellogg's Pantheism 8.pptx
E J Waggoner against Kellogg's Pantheism 8.pptxE J Waggoner against Kellogg's Pantheism 8.pptx
E J Waggoner against Kellogg's Pantheism 8.pptx
 
Call Girls In Karkardooma 83770 87607 Just-Dial Escorts Service 24X7 Avilable
Call Girls In Karkardooma 83770 87607 Just-Dial Escorts Service 24X7 AvilableCall Girls In Karkardooma 83770 87607 Just-Dial Escorts Service 24X7 Avilable
Call Girls In Karkardooma 83770 87607 Just-Dial Escorts Service 24X7 Avilable
 
办理西悉尼大学毕业证成绩单、制作假文凭
办理西悉尼大学毕业证成绩单、制作假文凭办理西悉尼大学毕业证成绩单、制作假文凭
办理西悉尼大学毕业证成绩单、制作假文凭
 
(南达科他州立大学毕业证学位证成绩单-永久存档)
(南达科他州立大学毕业证学位证成绩单-永久存档)(南达科他州立大学毕业证学位证成绩单-永久存档)
(南达科他州立大学毕业证学位证成绩单-永久存档)
 
Call Girls in Govindpuri Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Govindpuri Delhi 💯Call Us 🔝8264348440🔝Call Girls in Govindpuri Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Govindpuri Delhi 💯Call Us 🔝8264348440🔝
 
Call Girls In Dwarka Sub City ☎️7838079806 ✅ 💯Call Girls In Delhi
Call Girls In Dwarka Sub City  ☎️7838079806 ✅ 💯Call Girls In DelhiCall Girls In Dwarka Sub City  ☎️7838079806 ✅ 💯Call Girls In Delhi
Call Girls In Dwarka Sub City ☎️7838079806 ✅ 💯Call Girls In Delhi
 
Model Call Girl in Lado Sarai Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Lado Sarai Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Lado Sarai Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Lado Sarai Delhi reach out to us at 🔝9953056974🔝
 
南新罕布什尔大学毕业证学位证成绩单-学历认证
南新罕布什尔大学毕业证学位证成绩单-学历认证南新罕布什尔大学毕业证学位证成绩单-学历认证
南新罕布什尔大学毕业证学位证成绩单-学历认证
 
Authentic No 1 Amil Baba In Pakistan Amil Baba In Faisalabad Amil Baba In Kar...
Authentic No 1 Amil Baba In Pakistan Amil Baba In Faisalabad Amil Baba In Kar...Authentic No 1 Amil Baba In Pakistan Amil Baba In Faisalabad Amil Baba In Kar...
Authentic No 1 Amil Baba In Pakistan Amil Baba In Faisalabad Amil Baba In Kar...
 
Inspiring Through Words Power of Inspiration.pptx
Inspiring Through Words Power of Inspiration.pptxInspiring Through Words Power of Inspiration.pptx
Inspiring Through Words Power of Inspiration.pptx
 
西伦敦大学毕业证学位证成绩单-怎么样做
西伦敦大学毕业证学位证成绩单-怎么样做西伦敦大学毕业证学位证成绩单-怎么样做
西伦敦大学毕业证学位证成绩单-怎么样做
 
(No.1)↠Young Call Girls in Sikanderpur (Gurgaon) ꧁❤ 9711911712 ❤꧂ Escorts
(No.1)↠Young Call Girls in Sikanderpur (Gurgaon) ꧁❤ 9711911712 ❤꧂ Escorts(No.1)↠Young Call Girls in Sikanderpur (Gurgaon) ꧁❤ 9711911712 ❤꧂ Escorts
(No.1)↠Young Call Girls in Sikanderpur (Gurgaon) ꧁❤ 9711911712 ❤꧂ Escorts
 

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