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 App extends React.Component { | |
render() { | |
return ( | |
<div> | |
<button onClick={this.props.async_update}>Click To Update State After Five Seconds</button> | |
</div> | |
); | |
} | |
} |
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
# config/environments/test.rb | |
config.after_initialize do | |
Bullet.enable = true | |
Bullet.bullet_logger = true | |
Bullet.raise = true # raise an error if n+1 query occurs | |
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
###### Upon successful authentication, generate auth_token and send back to user | |
jti = Digest::MD5.hexidigest(user.id, rand.to_s, "SUPER_SECRET_KEY") | |
payload = { | |
iat: current_time, | |
sub: current_user.id, | |
jti: jti, | |
} | |
auth_token = JWT.encoded(payload, "SUPER_SECRET_KEY", "HS256") | |
$redis.sadd("UserValidAuthTokenJTI:#{current_user.id}", jti) | |
render { auth_token: token } |
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 "pry" | |
class Ship | |
def self.find_by_id(id) | |
SHIPS.select {|ship| ship.id == id }.first | |
end | |
attr_reader :id, :length | |
def initialize(id:, length:) | |
@length = length |
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
NUMERAL_MAP = { | |
'M' => 1000, | |
'CM' => 900, | |
'D' => 500, | |
'CD' => 400, | |
'C' => 100, | |
'XC' => 90, | |
'L' => 50, | |
'XL' => 40, | |
'X' => 10, |
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 alternate(color) | |
if color == :red | |
return :blue | |
else | |
return :red | |
end | |
end | |
class SameNeighborColorError < StandardError; 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
# Suppose the AccountHolder parent class is used | |
# to simply have an association with an account so | |
# both corporations and people can have accounts. | |
class Account < ActiveRecord::Base | |
belongs_to :account_holder | |
end | |
class AccountHolder < ActiveRecord::Base | |
has_one :account |
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 Animal < ActiveRecord::Base | |
def eat | |
end | |
end | |
class Bird < Animal | |
set_table_name "birds" | |
def fly | |
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 Animal < ActiveRecord::Base | |
def eat | |
end | |
end | |
class Bird < Animal | |
def fly | |
end | |
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
# Under STI, find the top 10 accounts with the greatest balances | |
# with just one query. | |
top_accounts = Account.order(balance: :desc).limit(10) | |
# Under MTI, we need to query all accounts and sort them in memory: | |
top_corporate_accts = CorporateAccount.order(balance: :desc).limit(10) | |
top_sb_accts = SmallBusinessAccount.order(balance: :desc).limit(10) | |
top_personal_accts = PersonalAccount.order(balance: :desc).limit(10) |