Skip to content

Commit

Permalink
This is how the said problem got solved:
Browse files Browse the repository at this point in the history
* Created a before_filter which checks if there is filtering being done, which then sorts the hash and returns records.
* smart_filter helper is now a block helper, which means if the filtering is set up, it skips the content inside the block and shows the results.

Happy. :)
  • Loading branch information
rizwanreza committed Aug 27, 2010
1 parent a08f3e0 commit 73f2cd4
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 82 deletions.
12 changes: 12 additions & 0 deletions app/controllers/application_controller.rb
Expand Up @@ -3,8 +3,20 @@

class ApplicationController < ActionController::Base
helper :all # include all helpers, all the time
before_filter :sort_smart_filter
#protect_from_forgery # See ActionController::RequestForgeryProtection for details

# Scrub sensitive parameters from your log
# filter_parameter_logging :password
def sort_smart_filter
if params[:smart_filter]
search = params[:smart_filter]
hash = {}
search.delete_if {|column, value| value[:value] == "" }
search.each do |column, value|
hash.merge!({column.to_sym => {value[:criteria] => value[:value]}})
end
@filtered_results = AddressBook.smart_filter(hash)
end
end
end
15 changes: 0 additions & 15 deletions app/controllers/smart_filter_controller.rb

This file was deleted.

7 changes: 5 additions & 2 deletions app/helpers/application_helper.rb
Expand Up @@ -18,7 +18,8 @@ module ApplicationHelper
</div>
=end

def smart_filter(model, cols)
def smart_filter(model, cols, &block)
body = capture(&block)
html = ""
html << "<form action='/address_books' method='get'>"
columns(cols).each do |column|
Expand All @@ -31,7 +32,9 @@ def smart_filter(model, cols)
end
html << "<input type='submit'>"
html << "</form>"
html
html << render(:partial => 'shared/filtered_results') if @filtered_results
html << body unless @filtered_results
concat html
end

def columns(cols)
Expand Down
38 changes: 1 addition & 37 deletions app/views/address_books/index.html.erb
@@ -1,42 +1,6 @@
<h1>Listing address_books</h1>

<%= smart_filter(AddressBook, :all) %>
<% if params[:smart_filter] %>
<table>
<tr>
<th>Name</th>
<th>Address</th>
<th>Zip Code</th>
<th>Company</th>
<th>Email</th>
<th>Phone</th>
<th>Domain</th>
</tr>
<%
search = params[:smart_filter]
hash = {}
search.delete_if {|column, value| value[:value] == "" }
search.each do |column, value|
hash.merge!({column.to_sym => {value[:criteria] => value[:value]}})
end
%>
<% AddressBook.smart_filter(hash).each do |address_book| %>
<tr>
<td><%=h address_book.name %></td>
<td><%=h address_book.address %></td>
<td><%=h address_book.zipcode %></td>
<td><%=h address_book.company %></td>
<td><%=h address_book.email %></td>
<td><%=h address_book.phone %></td>
<td><%=h address_book.domain %></td>
<td><%= link_to 'Show', address_book %></td>
<td><%= link_to 'Edit', edit_address_book_path(address_book) %></td>
<td><%= link_to 'Destroy', address_book, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>
<% else %>
<% smart_filter(AddressBook, :all) do %>

<table>
<tr>
Expand Down
25 changes: 25 additions & 0 deletions app/views/shared/_filtered_results.html.erb
@@ -0,0 +1,25 @@
<table>
<tr>
<th>Name</th>
<th>Address</th>
<th>Zip Code</th>
<th>Company</th>
<th>Email</th>
<th>Phone</th>
<th>Domain</th>
</tr>
<% @filtered_results.each do |address_book| %>
<tr>
<td><%=h address_book.name %></td>
<td><%=h address_book.address %></td>
<td><%=h address_book.zipcode %></td>
<td><%=h address_book.company %></td>
<td><%=h address_book.email %></td>
<td><%=h address_book.phone %></td>
<td><%=h address_book.domain %></td>
<td><%= link_to 'Show', address_book %></td>
<td><%= link_to 'Edit', edit_address_book_path(address_book) %></td>
<td><%= link_to 'Destroy', address_book, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>
28 changes: 0 additions & 28 deletions app/views/smart_filter/show.html.erb

This file was deleted.

0 comments on commit 73f2cd4

Please sign in to comment.