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

Tips and Treats for New Developers

Tips and Treats for New Developers

David Padilla

April 25, 2017
Tweet

More Decks by David Padilla

Other Decks in Technology

Transcript

  1. Solving the Riddle of Search: Using Sphinx with Rails The

    more complex your search queries becomes, the uglier your SQL statements get, even with ActiveRecord's helpful magic. Reclaim some clarity in your code by using the Sphinx search engine, a powerful tool that lets you search across your models in fast and complex ways.
  2. Webrat: Rails Acceptance Testing Evolved Webrat, a Ruby DSL for

    interacting with Web applications, helps you write expressive, maintainable acceptance tests while sidestepping the issues traditionally associated with in-browser approaches like Selenium and Watir. We'll look at how you can use Webrat to develop a robust acceptance test suite to ensure your app stays working as you refactor mercilessly.
  3. Smacking Git Around - Advanced Git Tricks Much of the

    Ruby and Rails community is now using Git, but there are a number of fun things that are a bit more difficult to get the hang of that are incredibly helpful to know when using Git. This session will go over some advanced Git usage for the casual or intermediate Git user.
  4. Git is an open source, distributed version control system implemented

    as a directed acyclic graph of commit objects pointing to snapshots of content, with all data saved in it's own custom content addressable filesystem by the SHA-1 checksum hash of each objects data. Branches are simply pointers into this directed graph of commits, identifying entry points that designate the latest work on that branch, allowing Git to traverse the pointers, determining a coherent history. This makes branching cheap and easy, and merging simple, encouraging non-linear development styles and frictionless context switching while facilitating distributed development, cryptographic integrity and late decision making. Nearly all commands run locally with no network latency overhead and it is implemented mainly in C, making it incredibly fast and efficient even for very large projects. There are several large open source projects using it, not to mention the 100,000 open source repositories maintained by the 80,000 developers on GitHub alone. https://gist.github.com/schacon/111475
  5. You should always be able to restore db state with:

    $ rails db:setup or $ rails db:reset
  6. def self.up add_column :companies, :zip_code, :integer add_column :companies, :name, :string

    Company.find(:all).each do |company| name, zip_code = company.code.split('-') company.update_attributes(name: name, zip_code: zip_code) end remove_column :companies, :code end
  7. def self.up add_column :companies, :zip_code, :integer add_column :companies, :name, :string

    Company.find(:all).each do |company| name, zip_code = company.code.split('-') company.update_attributes(name: name, zip_code: zip_code) end remove_column :companies, :code end
  8. def self.up add_column :companies, :zip_code, :integer add_column :companies, :name, :string

    Company.find(:all).each do |company| name, zip_code = company.code.split('-') company.update_attributes(name: name, zip_code: zip_code) end remove_column :companies, :code end
  9. def self.up add_column :companies, :zip_code, :integer add_column :companies, :name, :string

    Company.find(:all).each do |company| name, zip_code = company.code.split('-') company.update_attributes(name: name, zip_code: zip_code) end remove_column :companies, :code end
  10. def self.up add_column :companies, :zip_code, :integer add_column :companies, :name, :string

    Company.find(:all).each do |company| name, zip_code = company.code.split('-') company.update_attributes(name: name, zip_code: zip_code) end remove_column :companies, :code end
  11. def self.up add_column :companies, :zip_code, :integer add_column :companies, :name, :string

    Company.find(:all).each do |company| name, zip_code = company.code.split('-') company.update_attributes(name: name, zip_code: zip_code) end remove_column :companies, :code end
  12. def self.up add_column :companies, :zip_code, :integer add_column :companies, :name, :string

    execute <<EOS UPDATE companies SET name = SPLIT_PART(code, '-', 1), zip_code = SPLIT_PART(code, '-', 2) EOS remove_column :companies, :code end
  13. def self.up add_column :companies, :zip_code, :integer add_column :companies, :name, :string

    execute <<EOS UPDATE companies SET name = SPLIT_PART(code, '-', 1), zip_code = SPLIT_PART(code, '-', 2) EOS remove_column :companies, :code end
  14. $ rake -T data rake data:migrate # Apply pending data

    migrations rake data:migrate:down # Revert single data migration using VERSION rake data:migrate:pending # List pending migrations rake data:migrate:skip # Skip single data migration using VERSION rake data:migrate:up # Apply single data migration using VERSION
  15. class BigBadJob def perform(order_id, user_id) order = Order.find(order_id) user =

    User.find(user_id) order.assign_to(user) # ... end end
  16. # # Rails 4 # class OrderMailer < ActionMailer::Base def

    confirmation(order, user) # ... end end
  17. # # Rails 4 # class OrderMailer < ActionMailer::Base def

    confirmation(order_id, user_id) @order = Order.find(order_id) @user = User.find(user_id) # ... end end
  18. class ProductsController < ApplicationController def deactivate # ... end end

    # # config/routes.rb # Rails.application.routes.draw do resources :products do get :deactivate end end
  19. class ProductStatesController < ApplicationController def update # ... end end

    # # config/routes.rb # Rails.application.routes.draw do resources :products do resource :status, only: :update, controller: :product_statuses end end
  20. class CartsController < ApplicationController def apply_coupon # ... end end

    # # config/routes.rb # Rails.application.routes.draw do resource :cart do get :apply_coupon end end
  21. class DiscountsController < ApplicationController def create # ... end end

    # # config/routes.rb # Rails.application.routes.draw do resource :cart do resource :discount, only: :create, controller: :cart_discounts end end
  22. class ContactsController < ApplicationController def search # ... end end

    # # config/routes.rb # Rails.application.routes.draw do resources :contacts do get :search end end
  23. class Contacts::SearchesController < ApplicationsController def show # ... end end

    # # config/routes.rb # Rails.application.routes.draw do resources :contacts namespace :contacts do resource :search, only: :show end end
  24. # # .rubocop_todo.yml # Style/Documentation: Exclude: - 'spec/**/*' - 'test/**/*'

    - 'app/controllers/application_controller.rb' - 'app/helpers/application_helper.rb' - 'app/mailers/application_mailer.rb' - 'app/models/application_record.rb' - 'config/application.rb'
  25. # == Schema Information # # Table name: users #

    # id :integer not null, primary key # name :string # username :string # password :string # active :boolean # created_at :datetime not null # updated_at :datetime not null # class User < ApplicationRecord end
  26. # == Route Map # # Prefix Verb URI Pattern

    Controller#Action # contacts GET /contacts(.:format) contacts#index # POST /contacts(.:format) contacts#create # new_contact GET /contacts/new(.:format) contacts#new # edit_contact GET /contacts/:id/edit(.:format) contacts#edit # contact GET /contacts/:id(.:format) contacts#show # PATCH /contacts/:id(.:format) contacts#update # PUT /contacts/:id(.:format) contacts#update # DELETE /contacts/:id(.:format) contacts#destroy # contacts_search GET /contacts/search(.:format) contacts/searches#show # cart_discount POST /cart/discount(.:format) cart_discounts#create # new_cart GET /cart/new(.:format) carts#new # edit_cart GET /cart/edit(.:format) carts#edit # cart GET /cart(.:format) carts#show # PATCH /cart(.:format) carts#update # PUT /cart(.:format) carts#update # DELETE /cart(.:format) carts#destroy # POST /cart(.:format) carts#create # product_status PATCH /products/:product_id/status(.:format) product_statuses#update # PUT /products/:product_id/status(.:format) product_statuses#update # products GET /products(.:format) products#index # POST /products(.:format) products#create # new_product GET /products/new(.:format) products#new # edit_product GET /products/:id/edit(.:format) products#edit # product GET /products/:id(.:format) products#show # PATCH /products/:id(.:format) products#update # PUT /products/:id(.:format) products#update # DELETE /products/:id(.:format) products#destroy # Rails.application.routes.draw do resources :contacts namespace :contacts do resource :search, only: :show end resource :cart do resource :discount, only: :create, controller: :cart_discounts end resources :products do resource :status, only: :update, controller: :product_statuses end end
  27. # == Schema Information # # Table name: books #

    # id :integer not null, primary key # title :string # description :text # author_id :integer # created_at :datetime not null # updated_at :datetime not null # class Book < ApplicationRecord belongs_to :author has_many :comments end
  28. # == Schema Information # # Table name: authors #

    # id :integer not null, primary key # name :string # created_at :datetime not null # updated_at :datetime not null # class Author < ApplicationRecord end
  29. # == Schema Information # # Table name: comments #

    # id :integer not null, primary key # book_id :integer # user_id :integer # comment :text # created_at :datetime not null # updated_at :datetime not null # class Comment < ApplicationRecord belongs_to :user belongs_to :book end
  30. Started GET "/books" for 127.0.0.1 at 2017-04-18 13:33:52 -0700 Processing

    by BooksController#index as HTML Rendering books/index.html.erb within layouts/application Book Load (0.3ms) SELECT "books".* FROM "books" Author Load (0.1ms) SELECT "authors".* FROM "authors" WHERE "authors"."id" = ? L Comment Load (0.1ms) SELECT "comments".* FROM "comments" WHERE "comments"."book_i User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? Author Load (0.1ms) SELECT "authors".* FROM "authors" WHERE "authors"."id" = ? L Comment Load (0.1ms) SELECT "comments".* FROM "comments" WHERE "comments"."book_i CACHE User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIM CACHE Author Load (0.0ms) SELECT "authors".* FROM "authors" WHERE "authors"."id" Comment Load (0.1ms) SELECT "comments".* FROM "comments" WHERE "comments"."book_i CACHE User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIM CACHE Author Load (0.0ms) SELECT "authors".* FROM "authors" WHERE "authors"."id" Comment Load (0.1ms) SELECT "comments".* FROM "comments" WHERE "comments"."book_i CACHE User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIM Author Load (0.1ms) SELECT "authors".* FROM "authors" WHERE "authors"."id" = ? L Comment Load (0.1ms) SELECT "comments".* FROM "comments" WHERE "comments"."book_i CACHE User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIM Rendered books/index.html.erb within layouts/application (19.0ms) Completed 200 OK in 30ms (Views: 26.4ms | ActiveRecord: 1.5ms)
  31. Started GET "/books" for 127.0.0.1 at 2017-04-18 13:33:52 -0700 Processing

    by BooksController#index as HTML Rendering books/index.html.erb within layouts/application Book Load (0.3ms) SELECT "books".* FROM "books" Author Load (0.1ms) SELECT "authors".* FROM "authors" WHERE "authors"."id" = ? L Comment Load (0.1ms) SELECT "comments".* FROM "comments" WHERE "comments"."book_i User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? Author Load (0.1ms) SELECT "authors".* FROM "authors" WHERE "authors"."id" = ? L Comment Load (0.1ms) SELECT "comments".* FROM "comments" WHERE "comments"."book_i CACHE User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIM CACHE Author Load (0.0ms) SELECT "authors".* FROM "authors" WHERE "authors"."id" Comment Load (0.1ms) SELECT "comments".* FROM "comments" WHERE "comments"."book_i CACHE User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIM CACHE Author Load (0.0ms) SELECT "authors".* FROM "authors" WHERE "authors"."id" Comment Load (0.1ms) SELECT "comments".* FROM "comments" WHERE "comments"."book_i CACHE User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIM Author Load (0.1ms) SELECT "authors".* FROM "authors" WHERE "authors"."id" = ? L Comment Load (0.1ms) SELECT "comments".* FROM "comments" WHERE "comments"."book_i CACHE User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIM Rendered books/index.html.erb within layouts/application (19.0ms) Completed 200 OK in 30ms (Views: 26.4ms | ActiveRecord: 1.5ms)
  32. gem 'bullet' config.after_initialize do Bullet.enable = true Bullet.console = true

    Bullet.rails_logger = true Bullet.add_footer = true end
  33. Started GET "/books" for 127.0.0.1 at 2017-04-18 13:42:06 -0700 Processing

    by BooksController#index as HTML Rendering books/index.html.erb within layouts/application Book Load (0.1ms) SELECT "books".* FROM "books" Author Load (0.2ms) SELECT "authors".* FROM "authors" WHERE "authors"."id" IN (1, Comment Load (0.2ms) SELECT "comments".* FROM "comments" WHERE "comments"."book_i User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 Rendered books/index.html.erb within layouts/application (61.6ms) Completed 200 OK in 92ms (Views: 73.1ms | ActiveRecord: 3.5ms)
  34. Started GET "/books" for 127.0.0.1 at 2017-04-18 13:42:06 -0700 Processing

    by BooksController#index as HTML Rendering books/index.html.erb within layouts/application Book Load (0.1ms) SELECT "books".* FROM "books" Author Load (0.2ms) SELECT "authors".* FROM "authors" WHERE "authors"."id" IN (1, Comment Load (0.2ms) SELECT "comments".* FROM "comments" WHERE "comments"."book_i User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 Rendered books/index.html.erb within layouts/application (61.6ms) Completed 200 OK in 92ms (Views: 73.1ms | ActiveRecord: 3.5ms)
  35. Started GET "/books" for 127.0.0.1 at 2017-04-18 13:46:08 -0700 (0.1ms)

    SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY " Processing by BooksController#index as HTML Rendering books/index.html.erb within layouts/application Book Load (0.2ms) SELECT "books".* FROM "books" Author Load (0.1ms) SELECT "authors".* FROM "authors" WHERE "authors"."id" IN (1, Comment Load (0.1ms) SELECT "comments".* FROM "comments" WHERE "comments"."book_i User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 Rendered books/index.html.erb within layouts/application (50.1ms) Completed 200 OK in 116ms (Views: 94.4ms | ActiveRecord: 2.6ms) Oink Action: books#index Memory usage: 2596436 | PID: 31331 Instantiation Breakdown: Total: 14 | Book: 5 | Comment: 5 | Author: 3 | User: 1 Oink Log Entry Complete
  36. Started GET "/books" for 127.0.0.1 at 2017-04-18 13:46:08 -0700 (0.1ms)

    SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY " Processing by BooksController#index as HTML Rendering books/index.html.erb within layouts/application Book Load (0.2ms) SELECT "books".* FROM "books" Author Load (0.1ms) SELECT "authors".* FROM "authors" WHERE "authors"."id" IN (1, Comment Load (0.1ms) SELECT "comments".* FROM "comments" WHERE "comments"."book_i User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 Rendered books/index.html.erb within layouts/application (50.1ms) Completed 200 OK in 116ms (Views: 94.4ms | ActiveRecord: 2.6ms) Oink Action: books#index Memory usage: 2596436 | PID: 31331 Instantiation Breakdown: Total: 14 | Book: 5 | Comment: 5 | Author: 3 | User: 1 Oink Log Entry Complete