-
Static:
User.includes(:posts => :comments)
-
Dynamic:
:posts => :comments
is a product of user's input
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module SocialPresenter | |
# ... | |
class Twitter < Struct.new(:object) | |
def message | |
case object | |
when String |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
################# | |
# CLASSIC STYLE # | |
################# | |
class SessionsController < ApplicationController | |
# ... | |
def create | |
if user = AuthenticateUser.call(params[:username], params[:password]) | |
sign_in!(user) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 1. Models (Entities) | |
# | |
# * The nouns of your business logic | |
# * Usually persisted in the database | |
# * Usually contain validations | |
# * Usually expose associations with other models | |
# * Usually have scopes (to encapsulate the query logic) | |
################ | |
# ActiveRecord # |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gem "active_model_serializers", "0.10.0.rc1" | |
require "yaks" | |
require "active_record" | |
require "active_model_serializers" | |
require "benchmark/ips" | |
ActiveRecord::Base.establish_connection(adapter: "postgresql", database: "testing") | |
ActiveRecord::Schema.define do | |
create_table :users, force: true do |t| |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "benchmark/ips" | |
File.write "minitest_test.rb", <<-EOS | |
require "minitest/autorun" | |
require "minitest/pride" | |
class MintestTest < Minitest::Test | |
def test_foo | |
assert true | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Movie.select( | |
:year, | |
"ts_headline(title, to_tsquery('#{query}'), 'StartSel = <strong>, StopSel = </strong>, HighlightAll = true') AS title", | |
"ts_headline(plot, to_tsquery('#{query}'), 'StartSel = <strong>, StopSel = </strong>, HighlightAll = true') AS plot", | |
"ts_headline(episode, to_tsquery('#{query}'), 'StartSel = <strong>, StopSel = </strong>, HighlightAll = true') AS episode" | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#### | |
# 1. Simple assertion of required arguments | |
#### | |
# - You can forget #fetch | |
# - You get a better error, and always on the line where method is defined | |
def search(options = {}) | |
query = options.fetch(:query) | |
end | |
search() # => KeyError: key not found :query |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source "https://rubygems.org" | |
gem "rspec", "~> 3.1" | |
gem "rspec-rails", "~> 3.1" |