The document discusses WTF oriented programming techniques including:
- Using try/catch blocks to safely access nested object attributes
- Applying the Law of Demeter to avoid violating encapsulation
- Mapping and selecting arrays instead of iterating with each
- Storing translations in locale files instead of hardcoding strings
- Conditional logic to dynamically set form field values
The examples provide concise code snippets that implement various WTF patterns for handling errors, following principles of object-oriented design, and internationalization.
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