WTF ORIENTED PROGRAMMINGby @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
@[email protected]_name
class Politician
delegate :name, to: :studio,
prefix: true, allow_nil: true
# ...
end
@[email protected]_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