Created
September 22, 2010 17:50
-
-
Save guillermo/592147 to your computer and use it in GitHub Desktop.
Mini search logic
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def index | |
@search = Search.new(params[:search]) | |
@inmuebles = Inmueble.search(@search).paginate(:page => params[:page] || 1) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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