Skip to content

Instantly share code, notes, and snippets.

@aalvesjr
aalvesjr / teste.php
Created October 24, 2012 14:14
True or False PHP
// Tudo FALSE
if(null)
echo 'NULL';
if(0)
echo '0';
if(false)
echo 'FALSE';
if("")
echo 'VAZIO';
if(array())
@aalvesjr
aalvesjr / gist:3939879
Created October 23, 2012 16:26
Rails: Otimizando STI com default_scope
fonte: http://fernandoluizao.wordpress.com/2009/08/12/rails-otimizando-sti-com-default_scope/
Não dá pra negar que STI (Single Table Inheritance) é um recurso bacana do Rails. Claro que não é bom abusar, mas em alguns casos, ela se encaixa muito bem. No rails, quando queremos usar STI, basta ter uma coluna type do tipo string na tabela “mãe”, e nessa coluna o Rails se encarrega de gravar o nome da classe do registro, e a partir daí tudo funciona normalmente (para mais detalhes sobre como funciona STI no Rails, veja a api). Porém, apesar da implementação do Rails ser muito elegante e funcionar bem, ela tem alguns problemas:
Comparações entre strings são lentas. Mesmo que haja um índice na coluna “type”, comparações entre strings ainda são bem mais lentas que comparações entre inteiros.
Caso o model seja renomeado, os dados da coluna “type” terão que ser atualizados, ou ficaremos com dados incorretos na tabela. Para corrigir podemos criar uma migration, mas é mais uma coisa para se preocupar
Vamos tentar mel
@aalvesjr
aalvesjr / p.html.erb
Created October 23, 2012 15:36
Pendencias GVAR
<div id="_guardaResultadoPesquisa">
<h2>Resultado da pesquisa</h2>
<table cellspadding="0" cellspacing="0" width="100%">
<tr border="0">
<th width="14%"><span>Código</span></th>
<th width="14%"><span>Nº</span></th>
<th width="14%"><span>Nome</span></th>
<th width="14%"><span>Data Vencimento</span></th>
<th width="14%"><span>Status</span></th>
@aalvesjr
aalvesjr / lnode.js
Created October 23, 2012 12:44
Brincando com o placar do LetsNode
/*
* Brincando com o placar do letsnode.com:8090
*
*/
function result(){
return eval(document.getElementById('operations').innerHTML);
}
function input_result() {
@aalvesjr
aalvesjr / cod.js
Created October 22, 2012 12:02
Alterar esse trecho. [GVAR]
<script type="text/javascript">
// Substituir esse script:
// https://github.com/kakobotasso/gvar/blob/master/app/views/financeiro/novo_pagamento.html.erb
//
// Depois colocar em um JS separado
function get_tecla(e){
var tecla = (window.event) ? event.keyCode : e.which;
return tecla;
}
@aalvesjr
aalvesjr / fhelper.html.erb
Created October 21, 2012 20:23
Testes com FormHelper
<!-- Antes -->
<span>
<% select_tag "release[instalments_attributes][#{count}][payment_id]", options_for_select(@payments,@release.instalments[count][:payment_id]), :class => 'select' %>
</span>
<!-- Depois -->
<span>
<%= i.select :payment_id, @payments, {}, :class => 'select' %>
</span>
@aalvesjr
aalvesjr / release.rb
Created October 14, 2012 19:17
Como tratar o /edit com accepts_nested_attributes_for ???
class Release < ActiveRecord::Base
attr_accessible :category_id, :code, :description, :first_paid, :fixed_payment, :name, :number_instalments, :total_amount, :instalments_attributes
has_many :instalments
accepts_nested_attributes_for :instalments
end
__END__
@aalvesjr
aalvesjr / joins.rb
Created October 5, 2012 04:17
Join para o modulo de financeiro
Instalment.joins(:release).where(:"releases.code" => "P1210040001")
# ActiveRecord::Relation < Object
@aalvesjr
aalvesjr / joins.rb
Created October 5, 2012 03:56
Joins with :rails
11.2.1 Joining a Single Association
Category.joins(:posts)
This produces:
SELECT categories.* FROM categories
INNER JOIN posts ON posts.category_id = categories.id
Or, in English: “return a Category object for all categories with posts”. Note that you will see duplicate categories if more than one post has the same category. If you want unique categories, you can use Category.joins(:post).select(“distinct(categories.id)”).
11.2.2 Joining Multiple Associations
@aalvesjr
aalvesjr / test.js
Created September 28, 2012 17:52
>>> em JS
var t = {}
,length = t.length
// undefined
length
// undefined
var t = {}
,length = t.length >>> 0
// undefined