From 73f2cd45ceacdb19f6c9ae14e522a51cafbe45fb Mon Sep 17 00:00:00 2001 From: Rizwan Reza Date: Fri, 27 Aug 2010 07:37:06 +0430 Subject: [PATCH] This is how the said problem got solved: * 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. :) --- app/controllers/application_controller.rb | 12 +++++++ app/controllers/smart_filter_controller.rb | 15 -------- app/helpers/application_helper.rb | 7 ++-- app/views/address_books/index.html.erb | 38 +-------------------- app/views/shared/_filtered_results.html.erb | 25 ++++++++++++++ app/views/smart_filter/show.html.erb | 28 --------------- 6 files changed, 43 insertions(+), 82 deletions(-) delete mode 100644 app/controllers/smart_filter_controller.rb create mode 100644 app/views/shared/_filtered_results.html.erb delete mode 100644 app/views/smart_filter/show.html.erb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index ed95b97..7fa1284 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -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 diff --git a/app/controllers/smart_filter_controller.rb b/app/controllers/smart_filter_controller.rb deleted file mode 100644 index bf2e8dc..0000000 --- a/app/controllers/smart_filter_controller.rb +++ /dev/null @@ -1,15 +0,0 @@ -class SmartFilterController < ApplicationController - def show - end - - def create - 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 - @address_books = AddressBook.smart_filter(hash) - redirect_to smart_filter_path - end -end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index bf866a3..ebb6eb0 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -18,7 +18,8 @@ module ApplicationHelper =end - def smart_filter(model, cols) + def smart_filter(model, cols, &block) + body = capture(&block) html = "" html << "
" columns(cols).each do |column| @@ -31,7 +32,9 @@ def smart_filter(model, cols) end html << "" html << "
" - html + html << render(:partial => 'shared/filtered_results') if @filtered_results + html << body unless @filtered_results + concat html end def columns(cols) diff --git a/app/views/address_books/index.html.erb b/app/views/address_books/index.html.erb index 0d78bfc..86a9b87 100644 --- a/app/views/address_books/index.html.erb +++ b/app/views/address_books/index.html.erb @@ -1,42 +1,6 @@

Listing address_books

-<%= smart_filter(AddressBook, :all) %> - -<% 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 - %> - <% AddressBook.smart_filter(hash).each do |address_book| %> - - - - - - - - - - - - - <% end %> -
NameAddressZip CodeCompanyEmailPhoneDomain
<%=h address_book.name %><%=h address_book.address %><%=h address_book.zipcode %><%=h address_book.company %><%=h address_book.email %><%=h address_book.phone %><%=h address_book.domain %><%= link_to 'Show', address_book %><%= link_to 'Edit', edit_address_book_path(address_book) %><%= link_to 'Destroy', address_book, :confirm => 'Are you sure?', :method => :delete %>
-<% else %> +<% smart_filter(AddressBook, :all) do %> diff --git a/app/views/shared/_filtered_results.html.erb b/app/views/shared/_filtered_results.html.erb new file mode 100644 index 0000000..25ae187 --- /dev/null +++ b/app/views/shared/_filtered_results.html.erb @@ -0,0 +1,25 @@ +
+ + + + + + + + + + <% @filtered_results.each do |address_book| %> + + + + + + + + + + + + + <% end %> +
NameAddressZip CodeCompanyEmailPhoneDomain
<%=h address_book.name %><%=h address_book.address %><%=h address_book.zipcode %><%=h address_book.company %><%=h address_book.email %><%=h address_book.phone %><%=h address_book.domain %><%= link_to 'Show', address_book %><%= link_to 'Edit', edit_address_book_path(address_book) %><%= link_to 'Destroy', address_book, :confirm => 'Are you sure?', :method => :delete %>
diff --git a/app/views/smart_filter/show.html.erb b/app/views/smart_filter/show.html.erb deleted file mode 100644 index 61eb5ae..0000000 --- a/app/views/smart_filter/show.html.erb +++ /dev/null @@ -1,28 +0,0 @@ -

Listing Address Books

- - - - - - - - - - - - -<% @@address_books.each do |address_book| %> - - - - - - - - - - - - -<% end %> -
NameAddressZip CodeCompanyEmailPhoneDomain
<%=h address_book.name %><%=h address_book.address %><%=h address_book.zipcode %><%=h address_book.company %><%=h address_book.email %><%=h address_book.phone %><%=h address_book.domain %><%= link_to 'Show', address_book %><%= link_to 'Edit', edit_address_book_path(address_book) %><%= link_to 'Destroy', address_book, :confirm => 'Are you sure?', :method => :delete %>