Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Learning Ruby's Dynamism with Rails

wtnabe
November 23, 2013

Learning Ruby's Dynamism with Rails

* Class-exp with AR
* include and extend with AS::Concern
* Standard Callbacks

wtnabe

November 23, 2013
Tweet

More Decks by wtnabe

Other Decks in Programming

Transcript

  1. ActiveRecord example class Book < ActiveRecord::Base belongs_to :bookstand validates :name,

    :presence => true scope :done, -> {where(:done => true)} def recent? published_at >= 1.year.ago end end
  2. Just another OOPL class Book extends ActiveRecord\Model { static $belongs_to

    = array( array('bookstand') ); static $validates_presence_of = array( array('name') ); public static function done() {} public function is_recent() {} }
  3. class Book < ActiveRecord::Base belongs_to :bookstand validates :name, :presence =>

    true scope :done, -> {where(:done => true)} def recent? published_at >= 1.year.ago end end
  4. class Book < ActiveRecord::Base # class method call belongs_to :bookstand

    # class method call validates :name, :presence => true # class method call scope :done, -> {where(:done => true)} # instance method definition def recent? published_at >= 1.year.ago end end
  5. Ruby's Class Class is expression class keyword changes context It

    runs all class methods callable in class context
  6. new Rails 4 app. app ├── assets │ ├── images

    │ ├── javascripts │ └── stylesheets ├── controllers │ └── concerns <-- what ? ├── helpers ├── mailers ├── models │ └── concerns <-- what ? └── views └── layouts
  7. include module ModA def methodA; p 'methodA'; end end class

    KlassA include ModA end KlassA.new.methodA # => "methodA"
  8. extend module ModB def methodB; p 'methodB'; end end class

    KlassB extend ModB end KlassB.methodB # => "methodB"
  9. Concern module M extend ActiveSupport::Concern included do scope :foo, :conditions

    => {:created_at => nil} end module ClassMethods def cm; puts 'I am a class method'; end end module InstanceMethods def im; puts 'I am an instance method'; end end end
  10. When to use class ModelA < ActiveRecord::Base include M end

    class ModelB < ActiveRecord::Base include M end treat common attributes with multi-models
  11. class Book < ActiveRecord::Base # call class methods belongs_to :bookstand

    validates :name, :presence => true scope :done, -> {where(:done => true)} # define class methods class << self def recent self.where(:published_ad >= 1.year.ago) end end # define instance methods def recent? published_at >= 1.year.ago end end
  12. ActiveSupport::Concern module M included do # call class methods end

    module ClassMethods ... end module InstanceMethods ... end end
  13. How to use class Controller < ApplicationController before_action :auth, :unless

    => :logged_in? end class Model < ActiveRecord::Base before_validation do normalize_attributes end end ※ :unless keyword is allowed from ActiveSupport::Callbacks 4.0
  14. Similar to Final keyword class Subklass < Klass; end #

    => ...:3:in `inherited': # Subklass cannot inherit Klass (StandardE
  15. Ruby is Dynamic ! Class is expression Class can be

    modified easily with AS::Concern Useful Standard Callbacks