Skip to content

Instantly share code, notes, and snippets.

@guillermo
Created September 22, 2010 17:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guillermo/592147 to your computer and use it in GitHub Desktop.
Save guillermo/592147 to your computer and use it in GitHub Desktop.
Mini search logic
<div class="searchbox">
<%= form_for(@search, :url => inmuebles_path, :action =>"index",:method => "get") do %>
<%= select "by_type", options_for_select([['Piso', 'piso'], ['Local', 'local'],['Oficina', 'oficina']]) %>
<%= select "by_city","ciudad_id",Ciudad.all.map { |c| [c.localidad,c.id] }, {:prompt => 'Selecciona ciudad'} %>
<%= submit "Buscar", :class =>"btngo" %>
<% end %>
</div>
class Inmueble < AR::Base
# Definir los scopes necesarios by_type y by_city
# Implementar el método search como una concatenación de scopes. El scope se aplica solo si el value está presente, y el scope existe.
def self.search(search = {})
# Pasa los parámetros de busqueda a scopes si estos existen como métodos
search.to_hash.inject(self){ |scope, params|
option, value = *params
scope = scope.send(option.to_sym, value) if methods.include?(option.to_sym) && value.present?
scope
}
end
end
def index
@search = Search.new(params[:search])
@inmuebles = Inmueble.search(@search).paginate(:page => params[:page] || 1)
end
class Search < OpenStruct
def to_hash
@table
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment