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

Rails fora dos trilhos

Rails fora dos trilhos

Apresentação feita no Guru-Sorocaba

Rafael França

November 17, 2012
Tweet

More Decks by Rafael França

Other Decks in Programming

Transcript

  1. A dynamic, open source programming language with a focus on

    simplicity and productivity. It has an elegant syntax that is natural to read and easy to write.
  2. A dynamic, open source programming language with a focus on

    simplicity and productivity. It has an elegant syntax that is natural to read and easy to write.
  3. class Integer # Check whether the integer is evenly divisible

    by the argument. # # 0.multiple_of?(0) #=> true # 6.multiple_of?(5) #=> false # 10.multiple_of?(2) #=> true def multiple_of?(number) number != 0 ? self % number == 0 : zero? end end
  4. module I18NExtensions def t(options = {}) I18n.t self, options end

    end Symbol.send(:include, I18NExtensions)
  5. class Author < ActiveRecord::Base has_many :authorships has_many :books, :through =>

    :authorships end class Authorship < ActiveRecord::Base belongs_to :author belongs_to :book end @author = Author.first @author.authorships.collect { |a| a.book } @author.books
  6. class Author < ActiveRecord::Base has_many :authorships has_many :books, :through =>

    :authorships end class Authorship < ActiveRecord::Base belongs_to :author belongs_to :book end @author = Author.first @author.authorships.collect { |a| a.book } @author.books
  7. class Author < ActiveRecord::Base has_many :authorships has_many :books, :through =>

    :authorships end class Authorship < ActiveRecord::Base belongs_to :author belongs_to :book end @author = Author.first @author.authorships.collect { |a| a.book } @author.books
  8. class Author < ActiveRecord::Base has_many :authorships has_many :books, :through =>

    :authorships end class Authorship < ActiveRecord::Base belongs_to :author belongs_to :book end @author = Author.first @author.authorships.collect { |a| a.book } @author.books
  9. class Author < ActiveRecord::Base has_many :authorships has_many :books, :through =>

    :authorships end class Authorship < ActiveRecord::Base belongs_to :author belongs_to :book end @author = Author.first @author.authorships.collect { |a| a.book } @author.books
  10. class User < ActiveRecord::Base belongs_to :avatar end class Profile <

    ActiveRecord::Base belongs_to :user belongs_to :avatar, :through => :user end @user = User.first @user.profile.avatar @user.avatar
  11. module ActiveRecordExtensions def self.included(base) base.extend ClassMethods end module ClassMethods def

    self.extended(base) class << base alias_method_chain :belongs_to, :through_option end end def belongs_to_with_through_option(association, options = {}) if options[:through] if self.reflect_on_all_associations.find { |a| a.macro == :belongs_to && a.name == options[:through] } define_method association do |*args| through_assoc = self.send(options[:through], *args) through_assoc && through_assoc.send(options[:source] || association, *args) end else raise ':through option in :belongs_to macro must be another valid :belongs_to association' end else belongs_to_without_through_option association, options end end end end
  12. class User < ActiveRecord::Base belongs_to :avatar end class Profile <

    ActiveRecord::Base include ActiveRecordExtensions belongs_to :user belongs_to :avatar, :through => :user end @user = User.first @user.profile.avatar @user.avatar
  13. class User < ActiveRecord::Base belongs_to :avatar end class Profile <

    ActiveRecord::Base include ActiveRecordExtensions belongs_to :user belongs_to :avatar, :through => :user end @user = User.first @user.profile.avatar @user.avatar
  14. class User < ActiveRecord::Base belongs_to :avatar end class Profile <

    ActiveRecord::Base include ActiveRecordExtensions belongs_to :user belongs_to :avatar, :through => :user end @user = User.first @user.profile.avatar @user.avatar
  15. class User < ActiveRecord::Base belongs_to :avatar end class Profile <

    ActiveRecord::Base include ActiveRecordExtensions belongs_to :user belongs_to :avatar, :through => :user end @user = User.first @user.profile.avatar @user.avatar
  16. require 'will_paginate' module WillPaginate module ViewHelpers pagination_options[:previous_label] = :prev_label.t(:scope =>

    :mislav_will_paginate) pagination_options[:next_label] = :next_label.t(:scope => :mislav_will_paginate) def page_entries_info(collection, options = {}) entry_name = options[:entry_name] || :record.t(:scope => :mislav_will_paginate) entries_name = options[:entries_name] || :records.t(:scope => :mislav_will_paginate) if collection.total_pages < 2 case collection.size when 0 then :no_records.t(:entry_name => entry_name, :scope => [:mislav_will_paginate, :messages]) when 1 then :showing_1.t(:entry_name => entry_name, :scope => [:mislav_will_paginate, :messages]) else :showing_all.t(:size => collection.size, :entries_name => entries_name, :scope => [:mislav_will_paginate, :messages]) end else :showing.t(:entries_name => entries_name, :from => collection.offset + 1, :to => collection.offset + collection.length, :total => collection.total_entries, :scope => [:mislav_will_paginate, :messages]) end end end end
  17. class User < ActiveRecord::Base belongs_to :avatar end class Profile <

    ActiveRecord::Base belongs_to :user delegate :avatar, to: :user end @user = User.first @user.profile.avatar @user.avatar
  18. class User < ActiveRecord::Base belongs_to :avatar end class Profile <

    ActiveRecord::Base belongs_to :user delegate :avatar, to: :user end @user = User.first @user.profile.avatar @user.avatar
  19. class PeopleController < ActionController::Base def index @people = Person.find( :conditions

    => ["added_at > ? and deleted = ?", Time.now.utc, false], :order => "last_name, first_name") @people = @people.reject { |p| p.address.nil? } end end
  20. class Person < ActiveRecord::Base def self.find_recent people = find( :conditions

    => ["added_at > ? and deleted = ?", Time.now.utc, false], :order => "last_name, first_name") people.reject { |p| p.address.nil? } end # ... end class PeopleController < ActionController::Base def index @people = Person.find_recent end end
  21. class Person < ActiveRecord::Base def self.find_recent people = find( :conditions

    => ["added_at > ? and deleted = ?", Time.now.utc, false], :order => "last_name, first_name") people.reject { |p| p.address.nil? } end # ... end class PeopleController < ActionController::Base def index @people = Person.find_recent end end
  22. class Person < ActiveRecord::Base def self.find_recent people = find( :conditions

    => ["added_at > ? and deleted = ?", Time.now.utc, false], :order => "last_name, first_name") people.reject { |p| p.address.nil? } end # ... end class PeopleController < ActionController::Base def index @people = Person.find_recent end end
  23. class Account < ActiveRecord::Base validates_numericality_of :amount validates_inclusion_of :currency, :in =>

    %w(us_dollar real) def balance_in_us_dollar if currency == :us_dollar amount elsif currency == :real amount * conversion_rate(:us_dollar, :real) end end def balance_in_real if currency == :real amount elsif currency == :us_dollar amount * conversion_rate(:real, :us_dollar) end end def conversion_rate(from, to) SomeWebservice.get(from, to) end end
  24. class Account < ActiveRecord::Base validates_numericality_of :amount validates_inclusion_of :currency, :in =>

    %w(us_dollar real) def balance_in_us_dollar if currency == :us_dollar amount elsif currency == :real amount * conversion_rate(:us_dollar, :real) end end def balance_in_real if currency == :real amount elsif currency == :us_dollar amount * conversion_rate(:real, :us_dollar) end end def conversion_rate(from, to) SomeWebservice.get(from, to) end end
  25. class Account < ActiveRecord::Base validates_numericality_of :amount validates_inclusion_of :currency, :in =>

    %w(us_dollar real) def balance_in_us_dollar if currency == :us_dollar amount elsif currency == :real amount * conversion_rate(:us_dollar, :real) end end def balance_in_real if currency == :real amount elsif currency == :us_dollar amount * conversion_rate(:real, :us_dollar) end end def conversion_rate(from, to) SomeWebservice.get(from, to) end end
  26. class Account < ActiveRecord::Base validates_numericality_of :amount validates_inclusion_of :currency, :in =>

    %w(us_dollar real) def balance_in_us_dollar if currency == :us_dollar amount elsif currency == :real amount * conversion_rate(:us_dollar, :real) end end def balance_in_real if currency == :real amount elsif currency == :us_dollar amount * conversion_rate(:real, :us_dollar) end end def conversion_rate(from, to) SomeWebservice.get(from, to) end end
  27. class Money def initialize(amount, currency) @amount, @currency = amount, currency

    end def convert_to_us_dollar if currency == :us_dollar @amount elsif currency == :real @amount * ConversionRate.rate(:real, :us_dollar) end end def convert_to_real if currency == :real @amount elsif @currency == :dollar @amount * ConversionRate.rate(:us_dollar, :real) end end end
  28. class Money def initialize(amount, currency) @amount, @currency = amount, currency

    end def convert_to_us_dollar if currency == :us_dollar @amount elsif currency == :real @amount * ConversionRate.rate(:real, :us_dollar) end end def convert_to_real if currency == :real @amount elsif @currency == :dollar @amount * ConversionRate.rate(:us_dollar, :real) end end end
  29. class Account < ActiveRecord::Base validates_numericality_of :amount validates_inclusion_of :currency, :in =>

    %w(us_dollar real) composed_of :balance, :class_name => Money, :mapping => %w(amount currency) end
  30. class Account < ActiveRecord::Base validates_numericality_of :amount validates_inclusion_of :currency, :in =>

    %w(us_dollar real) def balance @balance ||= Money.new(amount, currency) end def balance=(balance) self.amount = balance.amount self.currency = balance.currency end end http://blog.plataformatec.com.br/2012/06/about-the- composed_of-removal/
  31. $ find app/models -name '*.rb' | xargs wc -l |

    sort -r | head -4 7567 total 608 app/models/channel.rb 563 app/models/article.rb 477 app/models/video.rb
  32. <% people = Person.find( :conditions => ["added_at > ? and

    deleted = ?", Time.now.utc, false], :order => "last_name, first_name") %> <% people.reject { |p| p.address.nil? }.each do |person| %> <div id="person-<%= person.new_record? ? "new" : person.id %>"> <span class="name"> <%= person.last_name %>, <%= person.first_name %> </span> <span class="age"> <%= (Date.today - person.birthdate) / 365 %> </span> </div> <% end %>
  33. class Person < ActiveRecord::Base # ... def name "#{last_name}, #{first_name}"

    end def age (Date.today - birthdate) / 365 end def pseudo_id new_record? ? "new" : id end end
  34. <% @people.each do |person| %> <div id="person-<%= person.pseudo_id %>"> <span

    class="name"><%= person.name %></span> <span class="age"><%= person.age %></span> </div> <% end %>
  35. class PersonPresenter def initialize(person) @person end def name "#{@person.last_name}, #{@person.first_name}"

    end def age (Date.today - @person.birthdate) / 365 end def pseudo_id @person.new_record? ? "new" : person.id end end class PeopleController < ApplicationController def show @person = PersonPresenter.new(Person.find(:id)) end end
  36. render :partial => template_name, :locals => { :my_object => my_object,

    :options => options[:options].join(" "), :use_jquery => options[:use_jquery], :use_css => options[:use_css], :thumb => options[:thumb], :full => options[:full], :width => options[:width], :height => options[:height], :thumb_width => options[:thumb_width], :thumb_height => options[:thumb_height] }
  37. def comment_form(comment, options = {}) # ... render :partial =>

    '_comment_form', :locals => a_huge_amount_of_locals end