Como usar DRY para melhorar o código

0 views
Skip to first unread message

Vinicíus Luiz

unread,
Nov 12, 2009, 1:30:05 PM11/12/09
to rails-br
Fala pessoal!
como posso melhorar esse código, ou melhor, remover a redundância que
tem lá na consulta ??????

def find_to_jqgrid(type, options = {})
return if options[:q].blank?

current_page = options[:page].to_i
limit_by_page = options[:rows].to_i || 10
sort_name = options[:sidx] || "id"
sord_order = options[:sord] || "asc"
conditions = options[:conditions] || "1=1"

records = count
total = records/limit_by_page + 1
current_page = total if current_page > total

start = (limit_by_page*current_page) - limit_by_page
@pages = unless conditions.blank?
all(:conditions=>conditions, :order=>"#{sort_name} #
{sord_order}", :limit=>limit_by_page, :offset=>start)
else
all(:order=>"#{sort_name} #
{sord_order}", :limit=>limit_by_page, :offset=>start)
end
end

valeu...

Felipe Mathies

unread,
Nov 12, 2009, 1:32:09 PM11/12/09
to rail...@googlegroups.com
http://agilewebdevelopment.com/plugins/will_paginate

2009/11/12 Vinicíus Luiz <mcp.vi...@gmail.com>



--
Felipe Mathies
msn: felipe...@hotmail.com
skype: voorhees1986
blog : http://www.felipemathies.com

Vinicius Luiz

unread,
Nov 12, 2009, 1:33:47 PM11/12/09
to rails-br
poh filipe, paginação já vem no componente js que to usando por isso o will nao rola... bom, uma ligeira alteração

    unless conditions.blank?
       find(type,:conditions=>conditions, :order=>"#{sort_name} #{sord_order}", :limit=>limit_by_page, :offset=>start)
    else
       find(type,:order=>"#{sort_name} #{sord_order}", :limit=>limit_by_page, :offset=>start)
    end

só quero melhorar essa duplicação ae... alguma ideia ??????

att,

Vinicius Luiz
Skype/Yahoo: vinicius.luiz
MSN: vinic...@hotmail.com
Gtalk: mcp.vi...@gmail.com
Twitter: http://twitter.com/vinicius_luiz
Blog: http://viniciusluizz.wordpress.com

----------------------------------------------

           Analista Desenvolvedor - TI                
CETAM Ltda. - Centro de Tecnologia da Amazônia

when there is no place in hell, the dead walked the earth!


2009/11/12 Vinicíus Luiz <mcp.vi...@gmail.com>

Catón

unread,
Nov 12, 2009, 1:35:38 PM11/12/09
to rail...@googlegroups.com
Paginação via JS?

Mas daí vc tem que carregar tudo de uma só vez :s



Lucas Catón
Software Developer
http://blog.lucascaton.com.br/


2009/11/12 Vinicius Luiz <mcp.vi...@gmail.com>

Vinicius Luiz

unread,
Nov 12, 2009, 1:41:04 PM11/12/09
to rail...@googlegroups.com
é mais ai vou ter um js.rjs por controller que for usar esse componente ai não da rock... isso é um pluginzinho que to fazendo por isso não é interessante por muitas dependências nele, ja tem o jrails é o js do componente e isso ja basta pra funcionar...

bom, de toda forma então valeu...


att,

Vinicius Luiz
Skype/Yahoo: vinicius.luiz
MSN: vinic...@hotmail.com
Gtalk: mcp.vi...@gmail.com
Twitter: http://twitter.com/vinicius_luiz
Blog: http://viniciusluizz.wordpress.com

----------------------------------------------

           Analista Desenvolvedor - TI                
CETAM Ltda. - Centro de Tecnologia da Amazônia

when there is no place in hell, the dead walked the earth!


2009/11/12 Catón <lucas...@gmail.com>

Catón

unread,
Nov 12, 2009, 1:43:39 PM11/12/09
to rail...@googlegroups.com
Cria named_scopes pra limpar o código :)

Vinicius Luiz

unread,
Nov 12, 2009, 2:21:25 PM11/12/09
to rail...@googlegroups.com
é mas ai perco a portabilidade do plugin ja que sempre vou precisar configurar o model, a ideia seria justamente configurar se precisar mesmo...

caike

unread,
Nov 13, 2009, 1:43:04 AM11/13/09
to rail...@googlegroups.com
order_limit_offset = { :order=>"#{sort_name} #{sord_order}", :limit=>limit_by_page, :offset=>start }

@pages = if conditions
  all(order_limit_offset.merge(:conditions=>conditions))
else
  all(order_limit_offset )
end
     
--
Att,
- Caike
http://caikesouza.com

Vinicius Luiz

unread,
Nov 13, 2009, 6:59:36 AM11/13/09
to rail...@googlegroups.com
caike muito boa essa solução, valeu...


att,

Vinicius Luiz
Skype/Yahoo: vinicius.luiz
MSN: vinic...@hotmail.com
Gtalk: mcp.vi...@gmail.com
Twitter: http://twitter.com/vinicius_luiz
Blog: http://viniciusluizz.wordpress.com

----------------------------------------------

           Analista Desenvolvedor - TI                
CETAM Ltda. - Centro de Tecnologia da Amazônia

when there is no place in hell, the dead walked the earth!


2009/11/13 caike <carlos...@gmail.com>
Reply all
Reply to author
Forward
0 new messages