Tips and Treats for New Developers

Tips and Treats for New Developers

0f9c9bbecc4067b9bce445cb11ed5d53?s=128

David Padilla

April 25, 2017
Tweet

Transcript

  1. 4.
  2. 5.
  3. 6.
  4. 9.
  5. 10.
  6. 11.
  7. 12.

    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.
  8. 13.

    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.
  9. 14.

    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.
  10. 15.
  11. 16.

    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
  12. 18.
  13. 19.
  14. 26.
  15. 37.
  16. 39.

    You should always be able to restore db state with:

    $ rails db:setup or $ rails db:reset
  17. 42.

    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
  18. 44.

    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
  19. 45.

    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
  20. 47.

    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
  21. 48.

    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
  22. 50.

    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
  23. 52.

    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
  24. 53.

    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
  25. 55.

    $ 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
  26. 62.
  27. 63.
  28. 67.

    class BigBadJob def perform(order_id, user_id) order = Order.find(order_id) user =

    User.find(user_id) order.assign_to(user) # ... end end
  29. 69.

    # # Rails 4 # class OrderMailer < ActionMailer::Base def

    confirmation(order, user) # ... end end
  30. 70.

    # # Rails 4 # class OrderMailer < ActionMailer::Base def

    confirmation(order_id, user_id) @order = Order.find(order_id) @user = User.find(user_id) # ... end end
  31. 74.
  32. 76.

    class ProductsController < ApplicationController def deactivate # ... end end

    # # config/routes.rb # Rails.application.routes.draw do resources :products do get :deactivate end end
  33. 77.

    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
  34. 78.

    class CartsController < ApplicationController def apply_coupon # ... end end

    # # config/routes.rb # Rails.application.routes.draw do resource :cart do get :apply_coupon end end
  35. 79.

    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
  36. 80.

    class ContactsController < ApplicationController def search # ... end end

    # # config/routes.rb # Rails.application.routes.draw do resources :contacts do get :search end end
  37. 81.

    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
  38. 84.
  39. 88.
  40. 90.
  41. 92.

    # # .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'
  42. 96.
  43. 97.
  44. 98.
  45. 99.

    # == 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
  46. 101.

    # == 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
  47. 102.
  48. 104.

    # == 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
  49. 105.

    # == 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
  50. 106.

    # == 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
  51. 107.
  52. 109.
  53. 110.

    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)
  54. 111.

    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)
  55. 112.

    gem 'bullet' config.after_initialize do Bullet.enable = true Bullet.console = true

    Bullet.rails_logger = true Bullet.add_footer = true end
  56. 113.
  57. 114.
  58. 116.

    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)
  59. 117.

    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)
  60. 118.
  61. 122.

    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
  62. 123.

    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