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
# Enumerable's Detect | |
# - returns first element for which block returns true and then stops. If none returns nil. | |
(1..100).detect { |i| i % 5 == 0 and i % 7 == 0 } #=> 35 | |
# Find All | |
(1..10).find_all { |i| i % 3 == 0 } #=> [3, 6, 9] | |
# Reject |
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
class AttributesApiExample < ApplicationRecord | |
attribute :start_date, :date, default: -> { 1.day.from_now } | |
attribute :end_date, :date, default: -> { 8.days.from_now } | |
attribute :my_attribute, :my_type, default: -> { 'My Default Value' } | |
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
class Current < ActiveSupport::CurrentAttributes | |
attribute :account, :user | |
attribute :request_id, :user_agent, :ip_address | |
resets { Time.zone = nil } | |
def user=(user) | |
super | |
self.account = user.account | |
Time.zone = user.time_zone |
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
# Sets up APPLICATION_NAME_HERE and deploys to Heroku. This should be run while on master | |
# NOTE: if you get a permission denied error, try running `chmod u+x bin/deploy` in the terminal | |
# Fails script if any commands fail | |
set -e | |
echo '== Installing dependencies ==' | |
bundle install |
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
def blocks_double_render_error | |
@books = Book.all | |
render 'index' and return | |
render 'show' | |
@books.last.update_column(:title, 'Or am I?') | |
end | |
#=> Rendering books/index.html.haml within layouts/application | |
#=> Book.last.title | |
#=> "Surprise Surprise" |
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
def double_render_error | |
@books = Book.all | |
render 'index' | |
render 'show' | |
end | |
#=> AbstractController::DoubleRenderError - Render and/or redirect were called multiple times in this action. Please note that you may only call render OR redirect, and at most once per action. |
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
def extra_curricular_activity | |
@book = Book.find(params[:id]) | |
render 'show' | |
@book.update_column(:title, 'Surprise Surprise') | |
end | |
#=> SQL (7.1ms) UPDATE "books" SET "title" = 'Surprise Surprise' WHERE "books"."id" = $1 [["id", 1]] |
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
def show | |
@book = Book.find(params[:id]) | |
render 'unexpected' | |
end | |
#=> Rendering books/unexpected.html.haml within layouts/application |
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
def index | |
@books = Book.all | |
end | |
#=> Rendering books/index.html.haml within layouts/application |
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
class String | |
def nostalgic_case | |
upcase.chars.join(" ") | |
end | |
end |