SlideShare a Scribd company logo
WTF ORIENTED PROGRAMMING
by @AkitaOnRails
$('a').click(function(){	
  
	
  	
  	
  	
  window.location	
  =	
  $(a).attr('href');	
  
})
project_id	
  =	
  @user.project	
  ==	
  nil	
  ?	
  nil	
  :	
  @user.project.id
project_id	
  =	
  @user.project	
  ==	
  nil	
  ?	
  nil	
  :	
  @user.project.id
project_id	
  =	
  @user.try(:project).try(:id)
@politician	
  =	
  Politician.where(name:	
  "Terminator").first	
  
puts	
  @politician.try(:studio).try(:name)
@politician	
  =	
  Politician.where(name:	
  "Terminator").first	
  
puts	
  @politician.try(:studio).try(:name)
LAW OF DEMETER
@politician	
  =	
  Politician.where(name:	
  "Terminator").first	
  
puts	
  @politician.try(:studio).try(:name)
LAW OF DEMETER
class	
  Politician	
  
	
  	
  delegate	
  :name,	
  to:	
  :studio,	
  
	
  	
  	
  	
  prefix:	
  true,	
  allow_nil:	
  true	
  
	
  	
  #	
  ...	
  
end	
  
@politician.studio.name
class	
  Politician	
  
	
  	
  delegate	
  :name,	
  to:	
  :studio,	
  
	
  	
  	
  	
  prefix:	
  true,	
  allow_nil:	
  true	
  
	
  	
  #	
  ...	
  
end	
  
@politician.studio.name@politician.studio_name
class	
  Politician	
  
	
  	
  delegate	
  :name,	
  to:	
  :studio,	
  
	
  	
  	
  	
  prefix:	
  true,	
  allow_nil:	
  true	
  
	
  	
  #	
  ...	
  
end	
  
@politician.studio.name@politician.studio_name
array	
  =	
  []	
  
list.each	
  do	
  |state|	
  
	
  	
  array	
  <<	
  [state.name,	
  state.acronym]	
  
end	
  
array
array	
  =	
  []	
  
list.each	
  do	
  |state|	
  
	
  	
  array	
  <<	
  [state.name,	
  state.acronym]	
  
end	
  
array
for	
  state	
  in	
  list
array	
  =	
  []	
  
list.each	
  do	
  |state|	
  
	
  	
  array	
  <<	
  [state.name,	
  state.acronym]	
  
end	
  
array
list.map	
  {	
  |state|	
  [state.name,	
  state.acronym]	
  }
for	
  state	
  in	
  list
array	
  =	
  []	
  
list.each	
  do	
  |state|	
  
	
  	
  if	
  state.name	
  =~	
  /^S/	
  
	
  	
  	
  	
  array	
  <<	
  [state.name,	
  state.acronym]	
  
	
  	
  end	
  
end	
  
array
array	
  =	
  []	
  
list.each	
  do	
  |state|	
  
	
  	
  if	
  state.name	
  =~	
  /^S/	
  
	
  	
  	
  	
  array	
  <<	
  [state.name,	
  state.acronym]	
  
	
  	
  end	
  
end	
  
array
list.	
  
	
  	
  select	
  {	
  |state|	
  state.name	
  =~	
  /^S/	
  }.	
  
	
  	
  map	
  {	
  |state|	
  [state.name,	
  state.acronym]	
  }
def	
  formatdate(d)	
  
	
  	
  	
  	
  months	
  =	
  Hash.new	
  
	
  	
  	
  	
  months["Jan"]	
  =	
  "janeiro"	
  
	
  	
  	
  	
  months["Feb"]	
  =	
  "fevereiro"	
  
	
  	
  	
  	
  #	
  ...	
  
	
  	
  	
  	
  months["Dec"]	
  =	
  "dezembro"	
  
	
  	
  
	
  	
  	
  	
  weeks	
  =	
  Hash.new	
  
	
  	
  	
  	
  weeks["Sun"]	
  =	
  "Domingo"	
  
	
  	
  	
  	
  #	
  ...	
  
	
  	
  	
  	
  weeks["Fri"]	
  =	
  "Sexta"	
  
	
  	
  	
  	
  weeks["Sat"]	
  =	
  "Sábado"	
  
	
  	
  	
  	
  	
  
	
  	
  	
  	
  return	
  weeks[d.strftime("%a")]+",	
  "+d.strftime("%d")+"	
  de	
  "+months[d.strftime("%b")]	
  
end
def	
  formatdate(d)	
  
	
  	
  	
  	
  months	
  =	
  Hash.new	
  
	
  	
  	
  	
  months["Jan"]	
  =	
  "janeiro"	
  
	
  	
  	
  	
  months["Feb"]	
  =	
  "fevereiro"	
  
	
  	
  	
  	
  #	
  ...	
  
	
  	
  	
  	
  months["Dec"]	
  =	
  "dezembro"	
  
	
  	
  
	
  	
  	
  	
  weeks	
  =	
  Hash.new	
  
	
  	
  	
  	
  weeks["Sun"]	
  =	
  "Domingo"	
  
	
  	
  	
  	
  #	
  ...	
  
	
  	
  	
  	
  weeks["Fri"]	
  =	
  "Sexta"	
  
	
  	
  	
  	
  weeks["Sat"]	
  =	
  "Sábado"	
  
	
  	
  	
  	
  	
  
	
  	
  	
  	
  return	
  weeks[d.strftime("%a")]+",	
  "+d.strftime("%d")+"	
  de	
  "+months[d.strftime("%b")]	
  
end
#	
  config/locales/pt-­‐BR.yml	
  
pt-­‐BR:	
  
	
  	
  time:	
  
	
  	
  	
  	
  formats:	
  
	
  	
  	
  	
  	
  	
  short_wday:	
  "%a,	
  %d	
  de	
  %B”	
  
d.to_s(:short_wday)
.row	
  
	
  	
  .wrap-­‐select	
  
	
  	
  	
  	
  select.payment-­‐select	
  
	
  	
  	
  	
  	
  	
  -­‐	
  if	
  @payment_type	
  ==	
  'pagamento'	
  
	
  	
  	
  	
  	
  	
  	
  	
  option.icons-­‐select.card-­‐credit[value="pagamento"	
  data-­‐foo="card-­‐credit"	
  selected]	
  Pagamento	
  
	
  	
  	
  	
  	
  	
  -­‐	
  else	
  
	
  	
  	
  	
  	
  	
  	
  	
  option.icons-­‐select.card-­‐credit	
  value="pagamento"	
  data-­‐foo="card-­‐credit"	
  Pagamento	
  
	
  	
  	
  	
  	
  	
  -­‐	
  if	
  @payment_type.include?	
  'cartao'	
  
	
  	
  	
  	
  	
  	
  	
  	
  option.icons-­‐select.card-­‐credit[value="cartao"	
  data-­‐foo="card-­‐credit"	
  selected]	
  Cartão	
  
	
  	
  	
  	
  	
  	
  -­‐	
  else	
  
	
  	
  	
  	
  	
  	
  	
  	
  option.icons-­‐select.card-­‐credit	
  value="cartao"	
  data-­‐foo="card-­‐credit"	
  Cartão	
  
	
  	
  	
  	
  	
  	
  -­‐	
  if	
  @payment_type	
  ==	
  'dinheiro'	
  
	
  	
  	
  	
  	
  	
  	
  	
  option.icons-­‐select.cash[value="dinheiro"	
  data-­‐foo="cash"	
  selected]	
  Dinheiro	
  
	
  	
  	
  	
  	
  	
  -­‐	
  else	
  
	
  	
  	
  	
  	
  	
  	
  	
  option.icons-­‐select.cash	
  value="dinheiro"	
  data-­‐foo="cash"	
  Dinheiro	
  
	
  	
  	
  	
  	
  	
  -­‐	
  if	
  @payment_type	
  ==	
  'boleto'	
  
	
  	
  	
  	
  	
  	
  	
  	
  option.icons-­‐select.banking-­‐billet[value="boleto"	
  data-­‐foo="banking-­‐billet"	
  selected]	
  Boleto	
  
	
  	
  	
  	
  	
  	
  -­‐	
  else	
  
	
  	
  	
  	
  	
  	
  	
  	
  option.icons-­‐select.banking-­‐billet	
  value="boleto"	
  data-­‐foo="banking-­‐billet"	
  Boleto
.row	
  
	
  	
  .wrap-­‐select	
  
	
  	
  	
  	
  select.payment-­‐select	
  
	
  	
  	
  	
  	
  	
  -­‐	
  if	
  @payment_type	
  ==	
  'pagamento'	
  
	
  	
  	
  	
  	
  	
  	
  	
  option.icons-­‐select.card-­‐credit[value="pagamento"	
  data-­‐foo="card-­‐credit"	
  selected]	
  Pagamento	
  
	
  	
  	
  	
  	
  	
  -­‐	
  else	
  
	
  	
  	
  	
  	
  	
  	
  	
  option.icons-­‐select.card-­‐credit	
  value="pagamento"	
  data-­‐foo="card-­‐credit"	
  Pagamento	
  
	
  	
  	
  	
  	
  	
  -­‐	
  if	
  @payment_type.include?	
  'cartao'	
  
	
  	
  	
  	
  	
  	
  	
  	
  option.icons-­‐select.card-­‐credit[value="cartao"	
  data-­‐foo="card-­‐credit"	
  selected]	
  Cartão	
  
	
  	
  	
  	
  	
  	
  -­‐	
  else	
  
	
  	
  	
  	
  	
  	
  	
  	
  option.icons-­‐select.card-­‐credit	
  value="cartao"	
  data-­‐foo="card-­‐credit"	
  Cartão	
  
	
  	
  	
  	
  	
  	
  -­‐	
  if	
  @payment_type	
  ==	
  'dinheiro'	
  
	
  	
  	
  	
  	
  	
  	
  	
  option.icons-­‐select.cash[value="dinheiro"	
  data-­‐foo="cash"	
  selected]	
  Dinheiro	
  
	
  	
  	
  	
  	
  	
  -­‐	
  else	
  
	
  	
  	
  	
  	
  	
  	
  	
  option.icons-­‐select.cash	
  value="dinheiro"	
  data-­‐foo="cash"	
  Dinheiro	
  
	
  	
  	
  	
  	
  	
  -­‐	
  if	
  @payment_type	
  ==	
  'boleto'	
  
	
  	
  	
  	
  	
  	
  	
  	
  option.icons-­‐select.banking-­‐billet[value="boleto"	
  data-­‐foo="banking-­‐billet"	
  selected]	
  Boleto	
  
	
  	
  	
  	
  	
  	
  -­‐	
  else	
  
	
  	
  	
  	
  	
  	
  	
  	
  option.icons-­‐select.banking-­‐billet	
  value="boleto"	
  data-­‐foo="banking-­‐billet"	
  Boleto
.row	
  
	
  	
  .wrap-­‐select	
  
	
  	
  	
  	
  select.payment-­‐select	
  
	
  	
  	
  	
  	
  	
  -­‐	
  pc	
  =	
  %w(card-­‐credit	
  card-­‐credit	
  cash	
  banking-­‐billet)	
  
	
  	
  	
  	
  	
  	
  -­‐	
  %w(pagamento	
  cartao	
  dinheiro	
  boleto).each_with_index	
  do	
  |i,	
  ptype|	
  
	
  	
  	
  	
  	
  	
  	
  	
  option.icons-­‐select[class=pc[i]	
  value=ptype	
  data-­‐foo=pc[i]	
  selected=(@payment_type==ptype)]	
  I18n.t(ptype)
<?php	
  if	
  (LANGUAGE	
  ==	
  'en')	
  {	
  ?>	
  <body	
  class="en">	
  <?php	
  }	
  ?>	
  
<?php	
  if	
  (LANGUAGE	
  ==	
  'pt')	
  {	
  ?>	
  <body	
  class="pt">	
  <?php	
  }	
  ?>	
  
<?php	
  if	
  (LANGUAGE	
  ==	
  'mx')	
  {	
  ?>	
  <body	
  class="mx">	
  <?php	
  }	
  ?>	
  
<?php	
  if	
  (LANGUAGE	
  ==	
  'kp')	
  {	
  ?>	
  <body	
  class="kp">	
  <?php	
  }	
  ?>
<?php	
  if	
  (LANGUAGE	
  ==	
  'en')	
  {	
  ?>	
  <body	
  class="en">	
  <?php	
  }	
  ?>	
  
<?php	
  if	
  (LANGUAGE	
  ==	
  'pt')	
  {	
  ?>	
  <body	
  class="pt">	
  <?php	
  }	
  ?>	
  
<?php	
  if	
  (LANGUAGE	
  ==	
  'mx')	
  {	
  ?>	
  <body	
  class="mx">	
  <?php	
  }	
  ?>	
  
<?php	
  if	
  (LANGUAGE	
  ==	
  'kp')	
  {	
  ?>	
  <body	
  class="kp">	
  <?php	
  }	
  ?>
<body	
  class="<?php	
  LANGUAGE	
  ?>">
address	
  =	
  ((!client.street.to_s.nil?)?	
  client.street.to_s	
  :	
  "")	
  +	
  	
  
((!client.number.to_s.nil?)?	
  "	
  Nº	
  "	
  +	
  client.number.to_s	
  :	
  "")	
  +	
  "	
  "	
  +	
  	
  
((!client.city.to_s.nil?)?	
  client.city.to_s	
  :	
  "")	
  +	
  	
  
((!client.zip.to_s.nil?)?	
  "	
  -­‐	
  CEP:	
  "	
  +	
  client.zip.to_s	
  :	
  "")
address	
  =	
  ((!client.street.to_s.nil?)?	
  client.street.to_s	
  :	
  "")	
  +	
  	
  
((!client.number.to_s.nil?)?	
  "	
  Nº	
  "	
  +	
  client.number.to_s	
  :	
  "")	
  +	
  "	
  "	
  +	
  	
  
((!client.city.to_s.nil?)?	
  client.city.to_s	
  :	
  "")	
  +	
  	
  
((!client.zip.to_s.nil?)?	
  "	
  -­‐	
  CEP:	
  "	
  +	
  client.zip.to_s	
  :	
  "")
address	
  =	
  "#{client.street}	
  Nº	
  #{client.number}	
  #{client.city}	
  -­‐	
  CEP:	
  #{client.zip}"
address	
  =	
  ((!client.street.to_s.nil?)?	
  client.street.to_s	
  :	
  "")	
  +	
  	
  
((!client.number.to_s.nil?)?	
  "	
  Nº	
  "	
  +	
  client.number.to_s	
  :	
  "")	
  +	
  "	
  "	
  +	
  	
  
((!client.city.to_s.nil?)?	
  client.city.to_s	
  :	
  "")	
  +	
  	
  
((!client.zip.to_s.nil?)?	
  "	
  -­‐	
  CEP:	
  "	
  +	
  client.zip.to_s	
  :	
  "")
address	
  =	
  "#{client.street}	
  Nº	
  #{client.number}	
  #{client.city}	
  -­‐	
  CEP:	
  #{client.zip}"
address	
  =	
  "%s	
  Nº	
  %s	
  %s	
  -­‐	
  CEP:	
  %s"	
  %	
  [client.street,	
  client.number,	
  client.city,	
  client.zip]
address	
  =	
  ((!client.street.to_s.nil?)?	
  client.street.to_s	
  :	
  "")	
  +	
  	
  
((!client.number.to_s.nil?)?	
  "	
  Nº	
  "	
  +	
  client.number.to_s	
  :	
  "")	
  +	
  "	
  "	
  +	
  	
  
((!client.city.to_s.nil?)?	
  client.city.to_s	
  :	
  "")	
  +	
  	
  
((!client.zip.to_s.nil?)?	
  "	
  -­‐	
  CEP:	
  "	
  +	
  client.zip.to_s	
  :	
  "")
address	
  =	
  "#{client.street}	
  Nº	
  #{client.number}	
  #{client.city}	
  -­‐	
  CEP:	
  #{client.zip}"
address	
  =	
  "%s	
  Nº	
  %s	
  %s	
  -­‐	
  CEP:	
  %s"	
  %	
  [client.street,	
  client.number,	
  client.city,	
  client.zip]
class	
  ClientDecorator	
  
	
  	
  #	
  ...	
  
	
  	
  def	
  formatted_address	
  
	
  	
  	
  	
  "%s	
  Nº	
  %s	
  %s	
  -­‐	
  CEP:	
  %s"	
  %	
  [street,	
  number,	
  city,	
  zip]	
  
	
  	
  end	
  
end
–Jamie Zawinksi
“Some people, when confronted with a problem,
think, “I know, I’ll use regular expressions.” Now
they have two problems.”
http://davidcel.is/blog/2012/09/06/stop-validating-email-addresses-with-regex/
class	
  User	
  <	
  ActiveRecord::Base	
  
	
  	
  validates	
  :email,	
  format:	
  {	
  with:	
  /A[^@]+@([^@.]+.)+[^@.]+z/	
  }	
  
end
class	
  User	
  <	
  ActiveRecord::Base	
  
	
  	
  validates	
  :email,	
  format:	
  {	
  with:	
  /A[^@]+@([^@.]+.)+[^@.]+z/	
  }	
  
end
class	
  EmailValidator	
  <	
  ActiveModel::EachValidator	
  
	
  	
  def	
  validate_each(record,	
  attribute,	
  value)	
  
	
  	
  	
  	
  unless	
  email_valid?(value)	
  
	
  	
  	
  	
  	
  	
  record.errors[attribute]	
  <<	
  (options[:message]	
  ||	
  "must	
  be	
  a	
  valid	
  email")	
  
	
  	
  	
  	
  end	
  
	
  	
  end	
  
	
  def	
  email_valid?(email)	
  
	
  	
  	
  	
  Mail::Address.new(email)	
  
	
  	
  	
  	
  true	
  
	
  	
  rescue	
  Mail::Field::ParseError	
  =>	
  e	
  
	
  	
  	
  	
  false	
  
	
  	
  end	
  	
  
end
class	
  User	
  <	
  ActiveRecord::Base	
  
	
  	
  validates	
  :email,	
  format:	
  {	
  with:	
  /A[^@]+@([^@.]+.)+[^@.]+z/	
  }	
  
end
class	
  EmailValidator	
  <	
  ActiveModel::EachValidator	
  
	
  	
  def	
  validate_each(record,	
  attribute,	
  value)	
  
	
  	
  	
  	
  unless	
  email_valid?(value)	
  
	
  	
  	
  	
  	
  	
  record.errors[attribute]	
  <<	
  (options[:message]	
  ||	
  "must	
  be	
  a	
  valid	
  email")	
  
	
  	
  	
  	
  end	
  
	
  	
  end	
  
	
  def	
  email_valid?(email)	
  
	
  	
  	
  	
  Mail::Address.new(email)	
  
	
  	
  	
  	
  true	
  
	
  	
  rescue	
  Mail::Field::ParseError	
  =>	
  e	
  
	
  	
  	
  	
  false	
  
	
  	
  end	
  	
  
end
class	
  User	
  <	
  ActiveRecord::Base	
  
	
  	
  validates	
  :email,	
  format:	
  {	
  with:	
  /A[^@]+@([^@.]+.)+[^@.]+z/	
  }	
  
end
class	
  EmailValidator	
  <	
  ActiveModel::EachValidator	
  
	
  	
  def	
  validate_each(record,	
  attribute,	
  value)	
  
	
  	
  	
  	
  unless	
  email_valid?(value)	
  
	
  	
  	
  	
  	
  	
  record.errors[attribute]	
  <<	
  (options[:message]	
  ||	
  "must	
  be	
  a	
  valid	
  email")	
  
	
  	
  	
  	
  end	
  
	
  	
  end	
  
	
  def	
  email_valid?(email)	
  
	
  	
  	
  	
  Mail::Address.new(email)	
  
	
  	
  	
  	
  true	
  
	
  	
  rescue	
  Mail::Field::ParseError	
  =>	
  e	
  
	
  	
  	
  	
  false	
  
	
  	
  end	
  	
  
end
class	
  User	
  <	
  ActiveRecord::Base	
  
	
  	
  validates	
  :email,	
  email:	
  true	
  
end
class	
  User	
  <	
  ActiveRecord::Base	
  
	
  	
  validates	
  :link,	
  format:	
  {	
  with:	
  
	
  	
  	
  	
  /(^$)|(^(http|https)://[a-­‐z0-­‐9]+([-­‐.]{1}[a-­‐z0-­‐9]+)*.[a-­‐z]{2,5}(([0-­‐9]{1,5})?/.*)?$)/ix	
  
	
  	
  }	
  
end
https://coderwall.com/p/ztig5g/validate-urls-in-rails
class	
  User	
  <	
  ActiveRecord::Base	
  
	
  	
  validates	
  :link,	
  format:	
  {	
  with:	
  
	
  	
  	
  	
  /(^$)|(^(http|https)://[a-­‐z0-­‐9]+([-­‐.]{1}[a-­‐z0-­‐9]+)*.[a-­‐z]{2,5}(([0-­‐9]{1,5})?/.*)?$)/ix	
  
	
  	
  }	
  
end
https://coderwall.com/p/ztig5g/validate-urls-in-rails
class	
  UrlValidator	
  <	
  ActiveModel::EachValidator	
  
	
  	
  def	
  validate_each(record,	
  attribute,	
  value)	
  
	
  	
  	
  	
  unless	
  url_valid?(value)	
  
	
  	
  	
  	
  	
  	
  record.errors[attribute]	
  <<	
  (options[:message]	
  ||	
  "must	
  be	
  a	
  valid	
  URL")	
  
	
  	
  	
  	
  end	
  
	
  	
  end	
  
	
  	
  #	
  a	
  URL	
  may	
  be	
  technically	
  well-­‐formed	
  but	
  may	
  	
  
	
  	
  #	
  not	
  actually	
  be	
  valid,	
  so	
  this	
  checks	
  for	
  both.	
  
	
  	
  def	
  url_valid?(url)	
  
	
  	
  	
  	
  url	
  =	
  URI.parse(url)	
  rescue	
  false	
  
	
  	
  	
  	
  url.kind_of?(URI::HTTP)	
  ||	
  url.kind_of?(URI::HTTPS)	
  
	
  	
  end	
  	
  
end
class	
  User	
  <	
  ActiveRecord::Base	
  
	
  	
  validates	
  :link,	
  format:	
  {	
  with:	
  
	
  	
  	
  	
  /(^$)|(^(http|https)://[a-­‐z0-­‐9]+([-­‐.]{1}[a-­‐z0-­‐9]+)*.[a-­‐z]{2,5}(([0-­‐9]{1,5})?/.*)?$)/ix	
  
	
  	
  }	
  
end
https://coderwall.com/p/ztig5g/validate-urls-in-rails
class	
  UrlValidator	
  <	
  ActiveModel::EachValidator	
  
	
  	
  def	
  validate_each(record,	
  attribute,	
  value)	
  
	
  	
  	
  	
  unless	
  url_valid?(value)	
  
	
  	
  	
  	
  	
  	
  record.errors[attribute]	
  <<	
  (options[:message]	
  ||	
  "must	
  be	
  a	
  valid	
  URL")	
  
	
  	
  	
  	
  end	
  
	
  	
  end	
  
	
  	
  #	
  a	
  URL	
  may	
  be	
  technically	
  well-­‐formed	
  but	
  may	
  	
  
	
  	
  #	
  not	
  actually	
  be	
  valid,	
  so	
  this	
  checks	
  for	
  both.	
  
	
  	
  def	
  url_valid?(url)	
  
	
  	
  	
  	
  url	
  =	
  URI.parse(url)	
  rescue	
  false	
  
	
  	
  	
  	
  url.kind_of?(URI::HTTP)	
  ||	
  url.kind_of?(URI::HTTPS)	
  
	
  	
  end	
  	
  
end
class	
  User	
  <	
  ActiveRecord::Base	
  
	
  	
  validates	
  :link,	
  url:	
  true	
  
end
class	
  User	
  <	
  ActiveRecord::Base	
  
	
  	
  validates	
  :link,	
  format:	
  {	
  with:	
  
	
  	
  	
  	
  /(^$)|(^(http|https)://[a-­‐z0-­‐9]+([-­‐.]{1}[a-­‐z0-­‐9]+)*.[a-­‐z]{2,5}(([0-­‐9]{1,5})?/.*)?$)/ix	
  
	
  	
  }	
  
end
https://coderwall.com/p/ztig5g/validate-urls-in-rails
class	
  UrlValidator	
  <	
  ActiveModel::EachValidator	
  
	
  	
  def	
  validate_each(record,	
  attribute,	
  value)	
  
	
  	
  	
  	
  unless	
  url_valid?(value)	
  
	
  	
  	
  	
  	
  	
  record.errors[attribute]	
  <<	
  (options[:message]	
  ||	
  "must	
  be	
  a	
  valid	
  URL")	
  
	
  	
  	
  	
  end	
  
	
  	
  end	
  
	
  	
  #	
  a	
  URL	
  may	
  be	
  technically	
  well-­‐formed	
  but	
  may	
  	
  
	
  	
  #	
  not	
  actually	
  be	
  valid,	
  so	
  this	
  checks	
  for	
  both.	
  
	
  	
  def	
  url_valid?(url)	
  
	
  	
  	
  	
  url	
  =	
  URI.parse(url)	
  rescue	
  false	
  
	
  	
  	
  	
  url.kind_of?(URI::HTTP)	
  ||	
  url.kind_of?(URI::HTTPS)	
  
	
  	
  end	
  	
  
end
def	
  query	
  
	
  	
  Vote.connection.select_values	
  <<-­‐SQL	
  
	
  	
  	
  	
  SELECT	
  voteable_id	
  
	
  	
  	
  	
  FROM	
  votes	
  
	
  	
  	
  	
  LEFT	
  OUTER	
  JOIN	
  authorships	
  ON	
  authorships.bill_id	
  =	
  voteable_id	
  
	
  	
  	
  	
  LEFT	
  OUTER	
  JOIN	
  politicians	
  ON	
  politicians.id	
  =	
  politician_id	
  
	
  	
  	
  	
  WHERE	
  voteable_type	
  =	
  'Bill'	
  
	
  	
  	
  	
  	
  	
  AND	
  person_type	
  =	
  'User'	
  
	
  	
  	
  	
  	
  	
  AND	
  votes.created_at	
  >=	
  now()	
  -­‐	
  interval	
  '90	
  days'	
  
	
  	
  	
  	
  	
  	
  #{@condition.present??	
  "AND	
  #{@condition}"	
  :	
  ""}	
  
	
  	
  	
  	
  GROUP	
  BY	
  voteable_id	
  
	
  	
  	
  	
  ORDER	
  BY	
  count(*)	
  DESC	
  LIMIT	
  6;	
  
	
  	
  SQL	
  
end
http://guides.rubyonrails.org/active_record_querying.html
def	
  query	
  
	
  	
  Vote.connection.select_values	
  <<-­‐SQL	
  
	
  	
  	
  	
  SELECT	
  voteable_id	
  
	
  	
  	
  	
  FROM	
  votes	
  
	
  	
  	
  	
  LEFT	
  OUTER	
  JOIN	
  authorships	
  ON	
  authorships.bill_id	
  =	
  voteable_id	
  
	
  	
  	
  	
  LEFT	
  OUTER	
  JOIN	
  politicians	
  ON	
  politicians.id	
  =	
  politician_id	
  
	
  	
  	
  	
  WHERE	
  voteable_type	
  =	
  'Bill'	
  
	
  	
  	
  	
  	
  	
  AND	
  person_type	
  =	
  'User'	
  
	
  	
  	
  	
  	
  	
  AND	
  votes.created_at	
  >=	
  now()	
  -­‐	
  interval	
  '90	
  days'	
  
	
  	
  	
  	
  	
  	
  #{@condition.present??	
  "AND	
  #{@condition}"	
  :	
  ""}	
  
	
  	
  	
  	
  GROUP	
  BY	
  voteable_id	
  
	
  	
  	
  	
  ORDER	
  BY	
  count(*)	
  DESC	
  LIMIT	
  6;	
  
	
  	
  SQL	
  
end
def	
  query	
  
	
  	
  query	
  =	
  Vote.select('voteable_id')	
  
	
  	
  	
  	
  joins("LEFT	
  OUTER	
  JOIN	
  authorships	
  ON	
  authorships.bill_id	
  =	
  voteable_id").	
  
	
  	
  	
  	
  joins("LEFT	
  OUTER	
  JOIN	
  politicians	
  ON	
  politicians.id	
  =	
  politician_id").	
  
	
  	
  	
  	
  where(voteable_type:	
  'Bill',	
  person_type:	
  'User').	
  
	
  	
  	
  	
  where("votes.created_at	
  >=	
  now()	
  -­‐	
  interval	
  '90	
  days'").	
  
	
  	
  	
  	
  group('voteable_id').	
  
	
  	
  	
  	
  order('count(*)	
  desc').	
  
	
  	
  	
  	
  limit(6)	
  
	
  	
  query	
  =	
  query.where(@condition)	
  if	
  @condition.present?	
  
	
  	
  query	
  
end
http://guides.rubyonrails.org/active_record_querying.html
OBRIGADO!
@akitaonrails

More Related Content

What's hot

The Art of Transduction
The Art of TransductionThe Art of Transduction
The Art of Transduction
David Stockton
 
Everything you always wanted to know about forms* *but were afraid to ask
Everything you always wanted to know about forms* *but were afraid to askEverything you always wanted to know about forms* *but were afraid to ask
Everything you always wanted to know about forms* *but were afraid to ask
Andrea Giuliano
 
Taking Perl to Eleven with Higher-Order Functions
Taking Perl to Eleven with Higher-Order FunctionsTaking Perl to Eleven with Higher-Order Functions
Taking Perl to Eleven with Higher-Order Functions
David Golden
 
JQuery Basics
JQuery BasicsJQuery Basics
JQuery Basics
Alin Taranu
 
Jquery In Rails
Jquery In RailsJquery In Rails
Jquery In Rails
shen liu
 
The jQuery Divide
The jQuery DivideThe jQuery Divide
The jQuery Divide
Rebecca Murphey
 
Writing Sensible Code
Writing Sensible CodeWriting Sensible Code
Writing Sensible Code
Anis Ahmad
 
WordPress 3.1 at DC PHP
WordPress 3.1 at DC PHPWordPress 3.1 at DC PHP
WordPress 3.1 at DC PHP
andrewnacin
 
Solid in practice
Solid in practiceSolid in practice
Solid in practice
Jessica Mauerhan
 
앱스프레소를 이용한 모바일 앱 개발(2)
앱스프레소를 이용한 모바일 앱 개발(2)앱스프레소를 이용한 모바일 앱 개발(2)
앱스프레소를 이용한 모바일 앱 개발(2)
mosaicnet
 
好みの日本酒を呑みたい! 〜さけのわデータで探す自分好みの酒〜
好みの日本酒を呑みたい! 〜さけのわデータで探す自分好みの酒〜好みの日本酒を呑みたい! 〜さけのわデータで探す自分好みの酒〜
好みの日本酒を呑みたい! 〜さけのわデータで探す自分好みの酒〜
Takashi Kitano
 
{tidygraph}と{ggraph}によるモダンなネットワーク分析
{tidygraph}と{ggraph}によるモダンなネットワーク分析{tidygraph}と{ggraph}によるモダンなネットワーク分析
{tidygraph}と{ggraph}によるモダンなネットワーク分析
Takashi Kitano
 
PHP for Python Developers
PHP for Python DevelopersPHP for Python Developers
PHP for Python Developers
Carlos Vences
 
HTML5 and CSS3 Refresher
HTML5 and CSS3 RefresherHTML5 and CSS3 Refresher
HTML5 and CSS3 Refresher
Ivano Malavolta
 
Presentation1
Presentation1Presentation1
Presentation1
Rahadyan Gusti
 
RubyBarCamp “Полезные gems и plugins”
RubyBarCamp “Полезные gems и plugins”RubyBarCamp “Полезные gems и plugins”
RubyBarCamp “Полезные gems и plugins”
apostlion
 
5th Sem SS lab progs
5th Sem SS lab progs5th Sem SS lab progs
5th Sem SS lab progs
Nagarjun Pakka Kannadiga
 
WordCamp Denver 2012 - Custom Meta Boxes
WordCamp Denver 2012 - Custom Meta BoxesWordCamp Denver 2012 - Custom Meta Boxes
WordCamp Denver 2012 - Custom Meta Boxes
Jeremy Green
 

What's hot (20)

The Art of Transduction
The Art of TransductionThe Art of Transduction
The Art of Transduction
 
Everything you always wanted to know about forms* *but were afraid to ask
Everything you always wanted to know about forms* *but were afraid to askEverything you always wanted to know about forms* *but were afraid to ask
Everything you always wanted to know about forms* *but were afraid to ask
 
Taking Perl to Eleven with Higher-Order Functions
Taking Perl to Eleven with Higher-Order FunctionsTaking Perl to Eleven with Higher-Order Functions
Taking Perl to Eleven with Higher-Order Functions
 
JQuery Flot
JQuery FlotJQuery Flot
JQuery Flot
 
JQuery Basics
JQuery BasicsJQuery Basics
JQuery Basics
 
Jquery In Rails
Jquery In RailsJquery In Rails
Jquery In Rails
 
The jQuery Divide
The jQuery DivideThe jQuery Divide
The jQuery Divide
 
Writing Sensible Code
Writing Sensible CodeWriting Sensible Code
Writing Sensible Code
 
WordPress 3.1 at DC PHP
WordPress 3.1 at DC PHPWordPress 3.1 at DC PHP
WordPress 3.1 at DC PHP
 
[ HackFest.pl 2012] Testing - what for and how
[ HackFest.pl 2012] Testing - what for and how[ HackFest.pl 2012] Testing - what for and how
[ HackFest.pl 2012] Testing - what for and how
 
Solid in practice
Solid in practiceSolid in practice
Solid in practice
 
앱스프레소를 이용한 모바일 앱 개발(2)
앱스프레소를 이용한 모바일 앱 개발(2)앱스프레소를 이용한 모바일 앱 개발(2)
앱스프레소를 이용한 모바일 앱 개발(2)
 
好みの日本酒を呑みたい! 〜さけのわデータで探す自分好みの酒〜
好みの日本酒を呑みたい! 〜さけのわデータで探す自分好みの酒〜好みの日本酒を呑みたい! 〜さけのわデータで探す自分好みの酒〜
好みの日本酒を呑みたい! 〜さけのわデータで探す自分好みの酒〜
 
{tidygraph}と{ggraph}によるモダンなネットワーク分析
{tidygraph}と{ggraph}によるモダンなネットワーク分析{tidygraph}と{ggraph}によるモダンなネットワーク分析
{tidygraph}と{ggraph}によるモダンなネットワーク分析
 
PHP for Python Developers
PHP for Python DevelopersPHP for Python Developers
PHP for Python Developers
 
HTML5 and CSS3 Refresher
HTML5 and CSS3 RefresherHTML5 and CSS3 Refresher
HTML5 and CSS3 Refresher
 
Presentation1
Presentation1Presentation1
Presentation1
 
RubyBarCamp “Полезные gems и plugins”
RubyBarCamp “Полезные gems и plugins”RubyBarCamp “Полезные gems и plugins”
RubyBarCamp “Полезные gems и plugins”
 
5th Sem SS lab progs
5th Sem SS lab progs5th Sem SS lab progs
5th Sem SS lab progs
 
WordCamp Denver 2012 - Custom Meta Boxes
WordCamp Denver 2012 - Custom Meta BoxesWordCamp Denver 2012 - Custom Meta Boxes
WordCamp Denver 2012 - Custom Meta Boxes
 

Similar to Devs for Leokz e 7Masters - WTF Oriented Programming

Zero to SOLID
Zero to SOLIDZero to SOLID
Zero to SOLID
Vic Metcalfe
 
Introduction à CoffeeScript pour ParisRB
Introduction à CoffeeScript pour ParisRB Introduction à CoffeeScript pour ParisRB
Introduction à CoffeeScript pour ParisRB jhchabran
 
php Mailer
php Mailerphp Mailer
php Mailer
Randy Arios
 
Php functions
Php functionsPhp functions
Php functions
JIGAR MAKHIJA
 
An Elephant of a Different Colour: Hack
An Elephant of a Different Colour: HackAn Elephant of a Different Colour: Hack
An Elephant of a Different Colour: Hack
Vic Metcalfe
 
Coding Horrors
Coding HorrorsCoding Horrors
Coding Horrors
Mark Baker
 
Using R for Building a Simple and Effective Dashboard
Using R for Building a Simple and Effective DashboardUsing R for Building a Simple and Effective Dashboard
Using R for Building a Simple and Effective Dashboard
Andrea Gigli
 
次世代版 PowerCMS 開発プロジェクトのご紹介
次世代版 PowerCMS 開発プロジェクトのご紹介次世代版 PowerCMS 開発プロジェクトのご紹介
次世代版 PowerCMS 開発プロジェクトのご紹介
純生 野田
 
PowerCMS X
PowerCMS XPowerCMS X
PowerCMS X
純生 野田
 
Symfony CoP: Form component
Symfony CoP: Form componentSymfony CoP: Form component
Symfony CoP: Form component
Samuel ROZE
 
Bag Of Tricks From Iusethis
Bag Of Tricks From IusethisBag Of Tricks From Iusethis
Bag Of Tricks From Iusethis
Marcus Ramberg
 
(Ab)Using the MetaCPAN API for Fun and Profit
(Ab)Using the MetaCPAN API for Fun and Profit(Ab)Using the MetaCPAN API for Fun and Profit
(Ab)Using the MetaCPAN API for Fun and Profit
Olaf Alders
 
Rails-like JavaScript Using CoffeeScript, Backbone.js and Jasmine
Rails-like JavaScript Using CoffeeScript, Backbone.js and JasmineRails-like JavaScript Using CoffeeScript, Backbone.js and Jasmine
Rails-like JavaScript Using CoffeeScript, Backbone.js and Jasmine
Raimonds Simanovskis
 
Youth Tobacco Survey Analysis
Youth Tobacco Survey AnalysisYouth Tobacco Survey Analysis
Youth Tobacco Survey Analysis
Roshik Ganesan
 
Modern JavaScript Engine Performance
Modern JavaScript Engine PerformanceModern JavaScript Engine Performance
Modern JavaScript Engine PerformanceCatalin Dumitru
 
C questions
C questionsC questions
C questions
mohamed sikander
 
Historical Finance Data
Historical Finance DataHistorical Finance Data
Historical Finance Data
JEE HYUN PARK
 
Wx::Perl::Smart
Wx::Perl::SmartWx::Perl::Smart
Wx::Perl::Smart
lichtkind
 

Similar to Devs for Leokz e 7Masters - WTF Oriented Programming (20)

Zero to SOLID
Zero to SOLIDZero to SOLID
Zero to SOLID
 
Introduction à CoffeeScript pour ParisRB
Introduction à CoffeeScript pour ParisRB Introduction à CoffeeScript pour ParisRB
Introduction à CoffeeScript pour ParisRB
 
php Mailer
php Mailerphp Mailer
php Mailer
 
Php functions
Php functionsPhp functions
Php functions
 
An Elephant of a Different Colour: Hack
An Elephant of a Different Colour: HackAn Elephant of a Different Colour: Hack
An Elephant of a Different Colour: Hack
 
Php
PhpPhp
Php
 
Coding Horrors
Coding HorrorsCoding Horrors
Coding Horrors
 
Using R for Building a Simple and Effective Dashboard
Using R for Building a Simple and Effective DashboardUsing R for Building a Simple and Effective Dashboard
Using R for Building a Simple and Effective Dashboard
 
次世代版 PowerCMS 開発プロジェクトのご紹介
次世代版 PowerCMS 開発プロジェクトのご紹介次世代版 PowerCMS 開発プロジェクトのご紹介
次世代版 PowerCMS 開発プロジェクトのご紹介
 
PowerCMS X
PowerCMS XPowerCMS X
PowerCMS X
 
Symfony CoP: Form component
Symfony CoP: Form componentSymfony CoP: Form component
Symfony CoP: Form component
 
Bag Of Tricks From Iusethis
Bag Of Tricks From IusethisBag Of Tricks From Iusethis
Bag Of Tricks From Iusethis
 
(Ab)Using the MetaCPAN API for Fun and Profit
(Ab)Using the MetaCPAN API for Fun and Profit(Ab)Using the MetaCPAN API for Fun and Profit
(Ab)Using the MetaCPAN API for Fun and Profit
 
Rails-like JavaScript Using CoffeeScript, Backbone.js and Jasmine
Rails-like JavaScript Using CoffeeScript, Backbone.js and JasmineRails-like JavaScript Using CoffeeScript, Backbone.js and Jasmine
Rails-like JavaScript Using CoffeeScript, Backbone.js and Jasmine
 
Youth Tobacco Survey Analysis
Youth Tobacco Survey AnalysisYouth Tobacco Survey Analysis
Youth Tobacco Survey Analysis
 
Modern JavaScript Engine Performance
Modern JavaScript Engine PerformanceModern JavaScript Engine Performance
Modern JavaScript Engine Performance
 
Theme verdadeiro
Theme verdadeiroTheme verdadeiro
Theme verdadeiro
 
C questions
C questionsC questions
C questions
 
Historical Finance Data
Historical Finance DataHistorical Finance Data
Historical Finance Data
 
Wx::Perl::Smart
Wx::Perl::SmartWx::Perl::Smart
Wx::Perl::Smart
 

More from Fabio Akita

Devconf 2019 - São Carlos
Devconf 2019 - São CarlosDevconf 2019 - São Carlos
Devconf 2019 - São Carlos
Fabio Akita
 
Meetup Nerdzão - English Talk about Languages
Meetup Nerdzão  - English Talk about LanguagesMeetup Nerdzão  - English Talk about Languages
Meetup Nerdzão - English Talk about Languages
Fabio Akita
 
Desmistificando Blockchains p/ Developers - Criciuma Dev Conf 2018
Desmistificando Blockchains p/ Developers - Criciuma Dev Conf 2018Desmistificando Blockchains p/ Developers - Criciuma Dev Conf 2018
Desmistificando Blockchains p/ Developers - Criciuma Dev Conf 2018
Fabio Akita
 
Desmistificando Blockchains - 20o Encontro Locaweb SP
Desmistificando Blockchains - 20o Encontro Locaweb SPDesmistificando Blockchains - 20o Encontro Locaweb SP
Desmistificando Blockchains - 20o Encontro Locaweb SP
Fabio Akita
 
Desmistificando Blockchains - Insiter Goiania
Desmistificando Blockchains - Insiter GoianiaDesmistificando Blockchains - Insiter Goiania
Desmistificando Blockchains - Insiter Goiania
Fabio Akita
 
Blockchain em 7 minutos - 7Masters
Blockchain em 7 minutos - 7MastersBlockchain em 7 minutos - 7Masters
Blockchain em 7 minutos - 7Masters
Fabio Akita
 
Elixir -Tolerância a Falhas para Adultos - GDG Campinas
Elixir  -Tolerância a Falhas para Adultos - GDG CampinasElixir  -Tolerância a Falhas para Adultos - GDG Campinas
Elixir -Tolerância a Falhas para Adultos - GDG Campinas
Fabio Akita
 
Desmistificando Mitos de Tech Startups - Intercon 2017
Desmistificando Mitos de Tech Startups - Intercon 2017Desmistificando Mitos de Tech Startups - Intercon 2017
Desmistificando Mitos de Tech Startups - Intercon 2017
Fabio Akita
 
30 Days to Elixir and Crystal and Back to Ruby
30 Days to Elixir and Crystal and Back to Ruby30 Days to Elixir and Crystal and Back to Ruby
30 Days to Elixir and Crystal and Back to Ruby
Fabio Akita
 
Uma Discussão sobre a Carreira de TI
Uma Discussão sobre a Carreira de TIUma Discussão sobre a Carreira de TI
Uma Discussão sobre a Carreira de TI
Fabio Akita
 
THE CONF - Opening Keynote
THE CONF - Opening KeynoteTHE CONF - Opening Keynote
THE CONF - Opening Keynote
Fabio Akita
 
A Journey through New Languages - Rancho Dev 2017
A Journey through New Languages - Rancho Dev 2017A Journey through New Languages - Rancho Dev 2017
A Journey through New Languages - Rancho Dev 2017
Fabio Akita
 
Desmistificando Mitos de Startups - Sebrae - AP
Desmistificando Mitos de Startups - Sebrae - APDesmistificando Mitos de Startups - Sebrae - AP
Desmistificando Mitos de Startups - Sebrae - AP
Fabio Akita
 
A Journey through New Languages - Guru Sorocaba 2017
A Journey through New Languages - Guru Sorocaba 2017A Journey through New Languages - Guru Sorocaba 2017
A Journey through New Languages - Guru Sorocaba 2017
Fabio Akita
 
A Journey through New Languages - Insiter 2017
A Journey through New Languages - Insiter 2017A Journey through New Languages - Insiter 2017
A Journey through New Languages - Insiter 2017
Fabio Akita
 
A Journey through New Languages - Locaweb Tech Day
A Journey through New Languages - Locaweb Tech DayA Journey through New Languages - Locaweb Tech Day
A Journey through New Languages - Locaweb Tech Day
Fabio Akita
 
A Journey through new Languages - Intercon 2016
A Journey through new Languages - Intercon 2016A Journey through new Languages - Intercon 2016
A Journey through new Languages - Intercon 2016
Fabio Akita
 
Premature Optimization 2.0 - Intercon 2016
Premature Optimization 2.0 - Intercon 2016Premature Optimization 2.0 - Intercon 2016
Premature Optimization 2.0 - Intercon 2016
Fabio Akita
 
Conexão Kinghost - Otimização Prematura
Conexão Kinghost - Otimização PrematuraConexão Kinghost - Otimização Prematura
Conexão Kinghost - Otimização Prematura
Fabio Akita
 
The Open Commerce Conference - Premature Optimisation: The Root of All Evil
The Open Commerce Conference - Premature Optimisation: The Root of All EvilThe Open Commerce Conference - Premature Optimisation: The Root of All Evil
The Open Commerce Conference - Premature Optimisation: The Root of All Evil
Fabio Akita
 

More from Fabio Akita (20)

Devconf 2019 - São Carlos
Devconf 2019 - São CarlosDevconf 2019 - São Carlos
Devconf 2019 - São Carlos
 
Meetup Nerdzão - English Talk about Languages
Meetup Nerdzão  - English Talk about LanguagesMeetup Nerdzão  - English Talk about Languages
Meetup Nerdzão - English Talk about Languages
 
Desmistificando Blockchains p/ Developers - Criciuma Dev Conf 2018
Desmistificando Blockchains p/ Developers - Criciuma Dev Conf 2018Desmistificando Blockchains p/ Developers - Criciuma Dev Conf 2018
Desmistificando Blockchains p/ Developers - Criciuma Dev Conf 2018
 
Desmistificando Blockchains - 20o Encontro Locaweb SP
Desmistificando Blockchains - 20o Encontro Locaweb SPDesmistificando Blockchains - 20o Encontro Locaweb SP
Desmistificando Blockchains - 20o Encontro Locaweb SP
 
Desmistificando Blockchains - Insiter Goiania
Desmistificando Blockchains - Insiter GoianiaDesmistificando Blockchains - Insiter Goiania
Desmistificando Blockchains - Insiter Goiania
 
Blockchain em 7 minutos - 7Masters
Blockchain em 7 minutos - 7MastersBlockchain em 7 minutos - 7Masters
Blockchain em 7 minutos - 7Masters
 
Elixir -Tolerância a Falhas para Adultos - GDG Campinas
Elixir  -Tolerância a Falhas para Adultos - GDG CampinasElixir  -Tolerância a Falhas para Adultos - GDG Campinas
Elixir -Tolerância a Falhas para Adultos - GDG Campinas
 
Desmistificando Mitos de Tech Startups - Intercon 2017
Desmistificando Mitos de Tech Startups - Intercon 2017Desmistificando Mitos de Tech Startups - Intercon 2017
Desmistificando Mitos de Tech Startups - Intercon 2017
 
30 Days to Elixir and Crystal and Back to Ruby
30 Days to Elixir and Crystal and Back to Ruby30 Days to Elixir and Crystal and Back to Ruby
30 Days to Elixir and Crystal and Back to Ruby
 
Uma Discussão sobre a Carreira de TI
Uma Discussão sobre a Carreira de TIUma Discussão sobre a Carreira de TI
Uma Discussão sobre a Carreira de TI
 
THE CONF - Opening Keynote
THE CONF - Opening KeynoteTHE CONF - Opening Keynote
THE CONF - Opening Keynote
 
A Journey through New Languages - Rancho Dev 2017
A Journey through New Languages - Rancho Dev 2017A Journey through New Languages - Rancho Dev 2017
A Journey through New Languages - Rancho Dev 2017
 
Desmistificando Mitos de Startups - Sebrae - AP
Desmistificando Mitos de Startups - Sebrae - APDesmistificando Mitos de Startups - Sebrae - AP
Desmistificando Mitos de Startups - Sebrae - AP
 
A Journey through New Languages - Guru Sorocaba 2017
A Journey through New Languages - Guru Sorocaba 2017A Journey through New Languages - Guru Sorocaba 2017
A Journey through New Languages - Guru Sorocaba 2017
 
A Journey through New Languages - Insiter 2017
A Journey through New Languages - Insiter 2017A Journey through New Languages - Insiter 2017
A Journey through New Languages - Insiter 2017
 
A Journey through New Languages - Locaweb Tech Day
A Journey through New Languages - Locaweb Tech DayA Journey through New Languages - Locaweb Tech Day
A Journey through New Languages - Locaweb Tech Day
 
A Journey through new Languages - Intercon 2016
A Journey through new Languages - Intercon 2016A Journey through new Languages - Intercon 2016
A Journey through new Languages - Intercon 2016
 
Premature Optimization 2.0 - Intercon 2016
Premature Optimization 2.0 - Intercon 2016Premature Optimization 2.0 - Intercon 2016
Premature Optimization 2.0 - Intercon 2016
 
Conexão Kinghost - Otimização Prematura
Conexão Kinghost - Otimização PrematuraConexão Kinghost - Otimização Prematura
Conexão Kinghost - Otimização Prematura
 
The Open Commerce Conference - Premature Optimisation: The Root of All Evil
The Open Commerce Conference - Premature Optimisation: The Root of All EvilThe Open Commerce Conference - Premature Optimisation: The Root of All Evil
The Open Commerce Conference - Premature Optimisation: The Root of All Evil
 

Recently uploaded

Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
Alison B. Lowndes
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
Frank van Harmelen
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
Elena Simperl
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
DianaGray10
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
Product School
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
Safe Software
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Tobias Schneck
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
91mobiles
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
Guy Korland
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
KatiaHIMEUR1
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
Paul Groth
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
Product School
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
Product School
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
UiPathCommunity
 

Recently uploaded (20)

Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
 

Devs for Leokz e 7Masters - WTF Oriented Programming

  • 2.
  • 3.
  • 4.
  • 5.
  • 6. $('a').click(function(){          window.location  =  $(a).attr('href');   })
  • 7. project_id  =  @user.project  ==  nil  ?  nil  :  @user.project.id
  • 8. project_id  =  @user.project  ==  nil  ?  nil  :  @user.project.id project_id  =  @user.try(:project).try(:id)
  • 9. @politician  =  Politician.where(name:  "Terminator").first   puts  @politician.try(:studio).try(:name)
  • 10. @politician  =  Politician.where(name:  "Terminator").first   puts  @politician.try(:studio).try(:name) LAW OF DEMETER
  • 11. @politician  =  Politician.where(name:  "Terminator").first   puts  @politician.try(:studio).try(:name) LAW OF DEMETER
  • 12. class  Politician      delegate  :name,  to:  :studio,          prefix:  true,  allow_nil:  true      #  ...   end   @politician.studio.name
  • 13. class  Politician      delegate  :name,  to:  :studio,          prefix:  true,  allow_nil:  true      #  ...   end   @politician.studio.name@politician.studio_name
  • 14. class  Politician      delegate  :name,  to:  :studio,          prefix:  true,  allow_nil:  true      #  ...   end   @politician.studio.name@politician.studio_name
  • 15.
  • 16. array  =  []   list.each  do  |state|      array  <<  [state.name,  state.acronym]   end   array
  • 17. array  =  []   list.each  do  |state|      array  <<  [state.name,  state.acronym]   end   array for  state  in  list
  • 18. array  =  []   list.each  do  |state|      array  <<  [state.name,  state.acronym]   end   array list.map  {  |state|  [state.name,  state.acronym]  } for  state  in  list
  • 19. array  =  []   list.each  do  |state|      if  state.name  =~  /^S/          array  <<  [state.name,  state.acronym]      end   end   array
  • 20. array  =  []   list.each  do  |state|      if  state.name  =~  /^S/          array  <<  [state.name,  state.acronym]      end   end   array list.      select  {  |state|  state.name  =~  /^S/  }.      map  {  |state|  [state.name,  state.acronym]  }
  • 21. def  formatdate(d)          months  =  Hash.new          months["Jan"]  =  "janeiro"          months["Feb"]  =  "fevereiro"          #  ...          months["Dec"]  =  "dezembro"              weeks  =  Hash.new          weeks["Sun"]  =  "Domingo"          #  ...          weeks["Fri"]  =  "Sexta"          weeks["Sat"]  =  "Sábado"                    return  weeks[d.strftime("%a")]+",  "+d.strftime("%d")+"  de  "+months[d.strftime("%b")]   end
  • 22. def  formatdate(d)          months  =  Hash.new          months["Jan"]  =  "janeiro"          months["Feb"]  =  "fevereiro"          #  ...          months["Dec"]  =  "dezembro"              weeks  =  Hash.new          weeks["Sun"]  =  "Domingo"          #  ...          weeks["Fri"]  =  "Sexta"          weeks["Sat"]  =  "Sábado"                    return  weeks[d.strftime("%a")]+",  "+d.strftime("%d")+"  de  "+months[d.strftime("%b")]   end #  config/locales/pt-­‐BR.yml   pt-­‐BR:      time:          formats:              short_wday:  "%a,  %d  de  %B”   d.to_s(:short_wday)
  • 23. .row      .wrap-­‐select          select.payment-­‐select              -­‐  if  @payment_type  ==  'pagamento'                  option.icons-­‐select.card-­‐credit[value="pagamento"  data-­‐foo="card-­‐credit"  selected]  Pagamento              -­‐  else                  option.icons-­‐select.card-­‐credit  value="pagamento"  data-­‐foo="card-­‐credit"  Pagamento              -­‐  if  @payment_type.include?  'cartao'                  option.icons-­‐select.card-­‐credit[value="cartao"  data-­‐foo="card-­‐credit"  selected]  Cartão              -­‐  else                  option.icons-­‐select.card-­‐credit  value="cartao"  data-­‐foo="card-­‐credit"  Cartão              -­‐  if  @payment_type  ==  'dinheiro'                  option.icons-­‐select.cash[value="dinheiro"  data-­‐foo="cash"  selected]  Dinheiro              -­‐  else                  option.icons-­‐select.cash  value="dinheiro"  data-­‐foo="cash"  Dinheiro              -­‐  if  @payment_type  ==  'boleto'                  option.icons-­‐select.banking-­‐billet[value="boleto"  data-­‐foo="banking-­‐billet"  selected]  Boleto              -­‐  else                  option.icons-­‐select.banking-­‐billet  value="boleto"  data-­‐foo="banking-­‐billet"  Boleto
  • 24. .row      .wrap-­‐select          select.payment-­‐select              -­‐  if  @payment_type  ==  'pagamento'                  option.icons-­‐select.card-­‐credit[value="pagamento"  data-­‐foo="card-­‐credit"  selected]  Pagamento              -­‐  else                  option.icons-­‐select.card-­‐credit  value="pagamento"  data-­‐foo="card-­‐credit"  Pagamento              -­‐  if  @payment_type.include?  'cartao'                  option.icons-­‐select.card-­‐credit[value="cartao"  data-­‐foo="card-­‐credit"  selected]  Cartão              -­‐  else                  option.icons-­‐select.card-­‐credit  value="cartao"  data-­‐foo="card-­‐credit"  Cartão              -­‐  if  @payment_type  ==  'dinheiro'                  option.icons-­‐select.cash[value="dinheiro"  data-­‐foo="cash"  selected]  Dinheiro              -­‐  else                  option.icons-­‐select.cash  value="dinheiro"  data-­‐foo="cash"  Dinheiro              -­‐  if  @payment_type  ==  'boleto'                  option.icons-­‐select.banking-­‐billet[value="boleto"  data-­‐foo="banking-­‐billet"  selected]  Boleto              -­‐  else                  option.icons-­‐select.banking-­‐billet  value="boleto"  data-­‐foo="banking-­‐billet"  Boleto .row      .wrap-­‐select          select.payment-­‐select              -­‐  pc  =  %w(card-­‐credit  card-­‐credit  cash  banking-­‐billet)              -­‐  %w(pagamento  cartao  dinheiro  boleto).each_with_index  do  |i,  ptype|                  option.icons-­‐select[class=pc[i]  value=ptype  data-­‐foo=pc[i]  selected=(@payment_type==ptype)]  I18n.t(ptype)
  • 25. <?php  if  (LANGUAGE  ==  'en')  {  ?>  <body  class="en">  <?php  }  ?>   <?php  if  (LANGUAGE  ==  'pt')  {  ?>  <body  class="pt">  <?php  }  ?>   <?php  if  (LANGUAGE  ==  'mx')  {  ?>  <body  class="mx">  <?php  }  ?>   <?php  if  (LANGUAGE  ==  'kp')  {  ?>  <body  class="kp">  <?php  }  ?>
  • 26. <?php  if  (LANGUAGE  ==  'en')  {  ?>  <body  class="en">  <?php  }  ?>   <?php  if  (LANGUAGE  ==  'pt')  {  ?>  <body  class="pt">  <?php  }  ?>   <?php  if  (LANGUAGE  ==  'mx')  {  ?>  <body  class="mx">  <?php  }  ?>   <?php  if  (LANGUAGE  ==  'kp')  {  ?>  <body  class="kp">  <?php  }  ?> <body  class="<?php  LANGUAGE  ?>">
  • 27. address  =  ((!client.street.to_s.nil?)?  client.street.to_s  :  "")  +     ((!client.number.to_s.nil?)?  "  Nº  "  +  client.number.to_s  :  "")  +  "  "  +     ((!client.city.to_s.nil?)?  client.city.to_s  :  "")  +     ((!client.zip.to_s.nil?)?  "  -­‐  CEP:  "  +  client.zip.to_s  :  "")
  • 28. address  =  ((!client.street.to_s.nil?)?  client.street.to_s  :  "")  +     ((!client.number.to_s.nil?)?  "  Nº  "  +  client.number.to_s  :  "")  +  "  "  +     ((!client.city.to_s.nil?)?  client.city.to_s  :  "")  +     ((!client.zip.to_s.nil?)?  "  -­‐  CEP:  "  +  client.zip.to_s  :  "") address  =  "#{client.street}  Nº  #{client.number}  #{client.city}  -­‐  CEP:  #{client.zip}"
  • 29. address  =  ((!client.street.to_s.nil?)?  client.street.to_s  :  "")  +     ((!client.number.to_s.nil?)?  "  Nº  "  +  client.number.to_s  :  "")  +  "  "  +     ((!client.city.to_s.nil?)?  client.city.to_s  :  "")  +     ((!client.zip.to_s.nil?)?  "  -­‐  CEP:  "  +  client.zip.to_s  :  "") address  =  "#{client.street}  Nº  #{client.number}  #{client.city}  -­‐  CEP:  #{client.zip}" address  =  "%s  Nº  %s  %s  -­‐  CEP:  %s"  %  [client.street,  client.number,  client.city,  client.zip]
  • 30. address  =  ((!client.street.to_s.nil?)?  client.street.to_s  :  "")  +     ((!client.number.to_s.nil?)?  "  Nº  "  +  client.number.to_s  :  "")  +  "  "  +     ((!client.city.to_s.nil?)?  client.city.to_s  :  "")  +     ((!client.zip.to_s.nil?)?  "  -­‐  CEP:  "  +  client.zip.to_s  :  "") address  =  "#{client.street}  Nº  #{client.number}  #{client.city}  -­‐  CEP:  #{client.zip}" address  =  "%s  Nº  %s  %s  -­‐  CEP:  %s"  %  [client.street,  client.number,  client.city,  client.zip] class  ClientDecorator      #  ...      def  formatted_address          "%s  Nº  %s  %s  -­‐  CEP:  %s"  %  [street,  number,  city,  zip]      end   end
  • 31. –Jamie Zawinksi “Some people, when confronted with a problem, think, “I know, I’ll use regular expressions.” Now they have two problems.” http://davidcel.is/blog/2012/09/06/stop-validating-email-addresses-with-regex/
  • 32. class  User  <  ActiveRecord::Base      validates  :email,  format:  {  with:  /A[^@]+@([^@.]+.)+[^@.]+z/  }   end
  • 33. class  User  <  ActiveRecord::Base      validates  :email,  format:  {  with:  /A[^@]+@([^@.]+.)+[^@.]+z/  }   end class  EmailValidator  <  ActiveModel::EachValidator      def  validate_each(record,  attribute,  value)          unless  email_valid?(value)              record.errors[attribute]  <<  (options[:message]  ||  "must  be  a  valid  email")          end      end    def  email_valid?(email)          Mail::Address.new(email)          true      rescue  Mail::Field::ParseError  =>  e          false      end     end
  • 34. class  User  <  ActiveRecord::Base      validates  :email,  format:  {  with:  /A[^@]+@([^@.]+.)+[^@.]+z/  }   end class  EmailValidator  <  ActiveModel::EachValidator      def  validate_each(record,  attribute,  value)          unless  email_valid?(value)              record.errors[attribute]  <<  (options[:message]  ||  "must  be  a  valid  email")          end      end    def  email_valid?(email)          Mail::Address.new(email)          true      rescue  Mail::Field::ParseError  =>  e          false      end     end
  • 35. class  User  <  ActiveRecord::Base      validates  :email,  format:  {  with:  /A[^@]+@([^@.]+.)+[^@.]+z/  }   end class  EmailValidator  <  ActiveModel::EachValidator      def  validate_each(record,  attribute,  value)          unless  email_valid?(value)              record.errors[attribute]  <<  (options[:message]  ||  "must  be  a  valid  email")          end      end    def  email_valid?(email)          Mail::Address.new(email)          true      rescue  Mail::Field::ParseError  =>  e          false      end     end class  User  <  ActiveRecord::Base      validates  :email,  email:  true   end
  • 36.
  • 37. class  User  <  ActiveRecord::Base      validates  :link,  format:  {  with:          /(^$)|(^(http|https)://[a-­‐z0-­‐9]+([-­‐.]{1}[a-­‐z0-­‐9]+)*.[a-­‐z]{2,5}(([0-­‐9]{1,5})?/.*)?$)/ix      }   end https://coderwall.com/p/ztig5g/validate-urls-in-rails
  • 38. class  User  <  ActiveRecord::Base      validates  :link,  format:  {  with:          /(^$)|(^(http|https)://[a-­‐z0-­‐9]+([-­‐.]{1}[a-­‐z0-­‐9]+)*.[a-­‐z]{2,5}(([0-­‐9]{1,5})?/.*)?$)/ix      }   end https://coderwall.com/p/ztig5g/validate-urls-in-rails class  UrlValidator  <  ActiveModel::EachValidator      def  validate_each(record,  attribute,  value)          unless  url_valid?(value)              record.errors[attribute]  <<  (options[:message]  ||  "must  be  a  valid  URL")          end      end      #  a  URL  may  be  technically  well-­‐formed  but  may        #  not  actually  be  valid,  so  this  checks  for  both.      def  url_valid?(url)          url  =  URI.parse(url)  rescue  false          url.kind_of?(URI::HTTP)  ||  url.kind_of?(URI::HTTPS)      end     end
  • 39. class  User  <  ActiveRecord::Base      validates  :link,  format:  {  with:          /(^$)|(^(http|https)://[a-­‐z0-­‐9]+([-­‐.]{1}[a-­‐z0-­‐9]+)*.[a-­‐z]{2,5}(([0-­‐9]{1,5})?/.*)?$)/ix      }   end https://coderwall.com/p/ztig5g/validate-urls-in-rails class  UrlValidator  <  ActiveModel::EachValidator      def  validate_each(record,  attribute,  value)          unless  url_valid?(value)              record.errors[attribute]  <<  (options[:message]  ||  "must  be  a  valid  URL")          end      end      #  a  URL  may  be  technically  well-­‐formed  but  may        #  not  actually  be  valid,  so  this  checks  for  both.      def  url_valid?(url)          url  =  URI.parse(url)  rescue  false          url.kind_of?(URI::HTTP)  ||  url.kind_of?(URI::HTTPS)      end     end
  • 40. class  User  <  ActiveRecord::Base      validates  :link,  url:  true   end class  User  <  ActiveRecord::Base      validates  :link,  format:  {  with:          /(^$)|(^(http|https)://[a-­‐z0-­‐9]+([-­‐.]{1}[a-­‐z0-­‐9]+)*.[a-­‐z]{2,5}(([0-­‐9]{1,5})?/.*)?$)/ix      }   end https://coderwall.com/p/ztig5g/validate-urls-in-rails class  UrlValidator  <  ActiveModel::EachValidator      def  validate_each(record,  attribute,  value)          unless  url_valid?(value)              record.errors[attribute]  <<  (options[:message]  ||  "must  be  a  valid  URL")          end      end      #  a  URL  may  be  technically  well-­‐formed  but  may        #  not  actually  be  valid,  so  this  checks  for  both.      def  url_valid?(url)          url  =  URI.parse(url)  rescue  false          url.kind_of?(URI::HTTP)  ||  url.kind_of?(URI::HTTPS)      end     end
  • 41. def  query      Vote.connection.select_values  <<-­‐SQL          SELECT  voteable_id          FROM  votes          LEFT  OUTER  JOIN  authorships  ON  authorships.bill_id  =  voteable_id          LEFT  OUTER  JOIN  politicians  ON  politicians.id  =  politician_id          WHERE  voteable_type  =  'Bill'              AND  person_type  =  'User'              AND  votes.created_at  >=  now()  -­‐  interval  '90  days'              #{@condition.present??  "AND  #{@condition}"  :  ""}          GROUP  BY  voteable_id          ORDER  BY  count(*)  DESC  LIMIT  6;      SQL   end http://guides.rubyonrails.org/active_record_querying.html
  • 42. def  query      Vote.connection.select_values  <<-­‐SQL          SELECT  voteable_id          FROM  votes          LEFT  OUTER  JOIN  authorships  ON  authorships.bill_id  =  voteable_id          LEFT  OUTER  JOIN  politicians  ON  politicians.id  =  politician_id          WHERE  voteable_type  =  'Bill'              AND  person_type  =  'User'              AND  votes.created_at  >=  now()  -­‐  interval  '90  days'              #{@condition.present??  "AND  #{@condition}"  :  ""}          GROUP  BY  voteable_id          ORDER  BY  count(*)  DESC  LIMIT  6;      SQL   end def  query      query  =  Vote.select('voteable_id')          joins("LEFT  OUTER  JOIN  authorships  ON  authorships.bill_id  =  voteable_id").          joins("LEFT  OUTER  JOIN  politicians  ON  politicians.id  =  politician_id").          where(voteable_type:  'Bill',  person_type:  'User').          where("votes.created_at  >=  now()  -­‐  interval  '90  days'").          group('voteable_id').          order('count(*)  desc').          limit(6)      query  =  query.where(@condition)  if  @condition.present?      query   end http://guides.rubyonrails.org/active_record_querying.html
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.