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

Como pegar um Trem em movimento

Como pegar um Trem em movimento

Palestra apresentada na RubyConfBrazil 2013. Aborda práticas, convenções e algumas ferramentas que facilitam a integração de novos membros à projetos Rails existentes.

Carlos Souza

August 29, 2013
Tweet

More Decks by Carlos Souza

Other Decks in Technology

Transcript

  1. $ git clone url $ cd project $ bundle $

    rake db:setup $ rails s $$$$
  2. $ git add app/models/user.rb $ git commit -m \ "Removed

    validation for cpf. Most users are not from Brazil and cpf is only required there." evite mensagens redundantes
  3. • Dependências do Sistema • Configurações • Banco de Dados

    • Como rodar testes • Outros serviços • Deployment
  4. $ bundle check source 'https://rubygems.org' gem 'rails', '4.0.0' gem 'pg'

    gem 'sass-rails', '~> 4.0.0' ... Your Ruby version is 1.8.7, but your Gemfile specified 1.9.3 Gemfile , :engine => 'jruby', :engine_version => '1.6.7' Your Ruby engine is ruby, but your Gemfile specified jruby ruby '1.9.3'
  5. Web app Scheduler Workers 1. bundle exec rails server 2.

    bundle exec rake worker 3. bundle exec rake urgent_worker 4. bundle exec rake scheduler 5. ... ?
  6. web: bundle exec rails s -p $PORT worker: bundle exec

    rake worker urgentworker: bundle exec rake urgent_worker scheduler: bundle exec rake scheduler Procfile
  7. Foreman 13:44:20 web.1 | started with pid 82891 13:44:20 worker.1

    | started with pid 82892 13:44:20 urgentworker.1 | started with pid 82893 13:44:20 scheduler.1 | started with pid 82894 13:44:25 web.1 | => Booting Puma 13:44:25 web.1 | => Rails 4.0.0 application starting in development on http://0.0.0.0:5000 13:44:25 web.1 | => Run `rails server -h` for more startup options 13:44:25 web.1 | => Ctrl-C to shutdown server 13:44:25 scheduler.1 | Scheduler.. 13:44:25 urgentworker.1 | Urgent worker.. 13:44:25 worker.1 | Worker.. 13:44:26 web.1 | Puma 2.0.1 starting... 13:44:26 web.1 | * Min threads: 0, max threads: 16 13:44:26 web.1 | * Environment: development 13:44:26 web.1 | * Listening on tcp://0.0.0.0:5000 $ foreman start $ gem install foreman
  8. class User < ActiveRecord::Base before_create :background_check attr_accessor :from_web protected def

    background_check if from_web && (!valid_ssn? && !valid_address?) # error message... return false end end end
  9. class User < ActiveRecord::Base # associations.. # validations.. def register

    # ... end def subscribe activate_recurring_payment subscribe_to_newsletter end def cancel_subscription deactivate_recuring_payment unsubscribe_from_newsletter end def suspend deactivate_recuring_payment end def unsuspend activate_recurring_payment end # etc... private def activate_recurring_payment; def subscribe_to_newsletter; def deactivate_recuring_payment; def unsubscribe_from_newsletter; end
  10. class UserRegistration attr_reader :user def initialize(params) @ssn = params.delete(:ssn) @address

    = params.delete(:address) @user = User.new(params) end def create user.save if valid_background_check? end private def valid_background_check? valid_ssn? && valid_address? end end
  11. class UserRegistration attr_reader :user def initialize(params) @ssn = params.delete(:ssn) @address

    = params.delete(:address) @user = User.new(params) end def create user.is_approved = valid_background_check? user.save end private def valid_background_check? valid_ssn? && valid_address? end end
  12. source 'https://rubygems.org' ruby '1.9.3' gem 'rails', '4.0.0' gem 'pg' gem

    'sass-rails', '~> 4.0.0' ... Gemfile gem 'thin' => Booting Thin => Rails 4.0.0 application starting in development on http://0.0.0.0:3000 => Run `rails server -h` for more startup options => Ctrl-C to shutdown server >> Thin web server (v1.5.0 codename Knife) >> Maximum connections set to 1024 $ rails s
  13. Cookies in Rails 3 def create ... session[:user_id] = user.id

    end cookie digitally signed visto no browser require 'rack' cookie = "BAh7CUkiD3Nlc3Npb25faWQGOgZFRkkiJ(...)" Rack::Session::Cookie::Base64::Marshal.new.decode(cookie) => {"session_id"=>"d3b170ba43a95f93e915388e62acdf6a", "user_id"=>"123", "_csrf_token"=>"rtkofTr8uQnCHc8CMxdpFHD(...)"} Lendo conteúdo de um cookie digitally signed
  14. require 'rack' cookie = "REE3QXILcHNsNHduU0t3eGMrcTA4dUhp(...)" Rack::Session::Cookie::Base64::Marshal.new.decode(cookie) => nil Lendo conteúdo

    de um cookie encrypted def create ... session[:user_id] = user.id end cookie encrypted visto no browser Cookies in Rails 4
  15. Foreman web: bundle exec rails s -p $PORT Procfile .env

    PORT=3001 SECRET_KEY_BASE=cd606ce82c02c70141d5...