Advertisement
mehigh

rails2_filters

Jun 24th, 2011
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 3.93 KB | None | 0 0
  1. Model
  2.  
  3. def self.filter_products(filter_options)
  4.     scope = System.scoped({})
  5.     scope = scope.scoped :conditions => ["systems.blogging >= 2"]           if filter_options.include?("blogging")
  6.     scope = scope.scoped :conditions => ["systems.chat >= 2"]               if filter_options.include?("chat")
  7.     scope = scope.scoped :conditions => ["systems.classifieds >= 2"]        if filter_options.include?("classifieds")
  8.     scope = scope.scoped :conditions => ["systems.forum >= 2"]              if filter_options.include?("forum")
  9.     scope = scope.scoped :conditions => ["systems.events_calendar >= 2"]    if filter_options.include?("calendar")
  10.     scope = scope.scoped :conditions => ["systems.link_management >= 2"]    if filter_options.include?("links")
  11.     scope = scope.scoped :conditions => ["systems.photo_gallery >= 2"]      if filter_options.include?("gallery")
  12.     scope = scope.scoped :conditions => ["systems.search_engine >= 2"]      if filter_options.include?("search")
  13.     scope = scope.scoped :conditions => ["systems.syndicated_content >= 2"] if filter_options.include?("rss")
  14.     scope = scope.scoped :conditions => ["systems. >= 2"]                   if filter_options.include?("")
  15.     scope
  16. end
  17.  
  18. Controller
  19.  
  20.   def filter
  21.     @filter_url = params[:id]    
  22.     @filter_options = @filter_url.split('-') rescue nil    
  23.     unless @filter_options.blank?
  24.       @systems = System.filter_products(@filter_options)      
  25.     else      
  26.       @filter_options = []
  27.       @systems = System.all      
  28.     end
  29.   end
  30.  
  31.   def filter_search
  32.     @options = params[:options]
  33.     @filter = []
  34.     @options.each { |key,value|
  35.       if value == "1"
  36.         @filter << key
  37.       end
  38.     }
  39.     @filters = @filter.sort.join('-')
  40.    
  41.     unless @filters.empty?
  42.       redirect_to filter_path(@filters)
  43.     else
  44.       flash[:notice] = 'Select a filter condition!'
  45.       redirect_to filter_path
  46.     end
  47.   end
  48.  
  49. View
  50.   <% form_for :options, :url => filter_search_path do |f| %>
  51.     <fieldset>
  52.       <ul>
  53.         <li><h4>Functionality</h4>
  54.           <ul>
  55.             <li>
  56.               <%= f.check_box :blogging, filter_status("blogging") %>
  57.               <label for="options_blogging">Blog</label>
  58.             </li>
  59.             <li>
  60.               <%= f.check_box :chat, filter_status("chat") %>
  61.               <label for="options_chat">Chat</label>
  62.             </li>
  63.             <li>
  64.               <%= f.check_box :classifieds, filter_status("classifieds") %>
  65.               <label for="options_classifieds">Classifieds</label>
  66.             </li>
  67.             <li>
  68.               <%= f.check_box :forum, filter_status("forum") %>
  69.               <label for="options_forum">Forum</label>
  70.             </li>
  71.             <li>
  72.               <%= f.check_box :calendar, filter_status("calendar") %>
  73.               <label for="options_calendar">Events calendar</label>
  74.             </li>
  75.             <li>
  76.               <%= f.check_box :links, filter_status("links") %>
  77.               <label for="options_links">Link management</label>
  78.             </li>
  79.             <li>
  80.               <%= f.check_box :gallery, filter_status("gallery") %>
  81.               <label for="options_gallery">Photo gallery</label>
  82.             </li>
  83.             <li>
  84.               <%= f.check_box :search, filter_status("search") %>
  85.               <label for="options_search">Search Engine</label>
  86.             </li>
  87.             <li>
  88.               <%= f.check_box :rss, filter_status("rss") %>
  89.               <label for="options_rss">RSS</label>
  90.             </li>
  91.           </ul>
  92.         </li>
  93.       </ul>      
  94.     </fieldset>
  95.     <fieldset class="submit">
  96.       <input type="submit" class="button" id="filter" value="Filter" name="" />
  97.     </fieldset>
  98.   <% end %>
  99.  
  100. Config/routes.rb
  101.  
  102. map.filter_search '/systems/filter_search', :controller => 'systems', :action => 'filter_search'
  103.     map.filter '/systems/filter/:id', :controller => 'systems', :action => 'filter', :id => nil
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement