Fábio Akita é co-fundador da Codeminer 42, empresa de desenvolvimento de software principalmente para startups. Desde 2008 é o criador e organizador do Rubyconf Brasil, uma das maiores conferências de tecnologia da América Latina. Tem experiência de 20 anos no mercado de software, tendo passado pelo mercado de grandes agências, consultoria SAP corporativo e pelo primeiro boom de startups em 2000. Nesta edição do 7Masters, ele trouxe a talk "WTF Oriented Programming".
36. –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/
37. class
User
<
ActiveRecord::Base
validates
:email,
format:
{
with:
/A[^@]+@([^@.]+.)+[^@.]+z/
}
end
38. 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
39. 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
40. 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
41.
42. 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
43. 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
44. 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
45. 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
46. 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
47. 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