Permalink
Browse files

641- Add Regional Filtering on Search results

  • Loading branch information...
1 parent 307981d commit 49822939dc3f22bd7736aa12df578a07a941ef1f @paramaw paramaw committed Aug 25, 2012
View
@@ -15,6 +15,7 @@
* [Regions Filter Shows for Cleveland, OH Region.](https://agilezen.com/project/18091/story/644)
* [Canceling Regions Filter, Will Remove Users Default Filter.](https://agilezen.com/project/18091/story/645)
* [Move Get Satisfaction to Bottom Right Corner.](https://agilezen.com/project/18091/story/647)
+ * [Add Regional Filtering on Search results](https://agilezen.com/project/18091/story/641)
### Chores
* [Upgrade to rails 3.1](https://agilezen.com/project/18091/story/526)
@@ -2,7 +2,8 @@ class SearchController < ApplicationController
def results
search_service = SearchService.new
-
+ @top_metro_regions = MetroRegion.top_metro_regions(5)
+
if params[:filter] and not determine_model_class(params[:filter]).nil?
@models_to_search = determine_model_class(params[:filter])
else
@@ -14,9 +15,9 @@ def results
@results = []
else
if(params.key?(:filter))
- @results = search_service.fetch_filtered_results(params[:q], params[:filter], @models_to_search).paginate(page: params[:page], per_page: 10)
+ @results = search_service.fetch_filtered_results(params[:q], params[:filter], :models => @models_to_search, :region_metrocodes => default_region).paginate(page: params[:page], per_page: 10)
else
- @results = search_service.fetch_results(params[:q], @models_to_search).paginate(page: params[:page], per_page: 10)
+ @results = search_service.fetch_results(params[:q], :models => @models_to_search, :region_metrocodes => default_region).paginate(page: params[:page], per_page: 10)
end
end
@@ -14,6 +14,7 @@ class Conversation < ActiveRecord::Base
text :summary, :stored => true, :boost => 2, :default_boost => 2 do
Sanitize.clean(summary, :remove_contents => ['style','script'])
end
+ integer :region_metrocodes, :multiple => true
end
has_many :contributions, :dependent => :destroy
has_many :confirmed_contributions, :class_name => 'Contribution',
@@ -151,6 +152,12 @@ def reflection_participants
def reflection_participants_count
reflection_participants.count
end
+
+ # region_metrocodes is plural because this method is available across several models, which is used to index the metrocodes in Solr.
+ def region_metrocodes
+ [metro_region.metrocode].compact if metro_region_id.present? && metro_region.present?
+ end
+
def self.available_filters
{
View
@@ -13,6 +13,7 @@ class Issue < ActiveRecord::Base
Sanitize.clean(summary, :remove_contents => ['style','script'])
end
string :type
+ integer :region_metrocodes, :multiple => true
end
ALL_TYPES = ['Issue', 'ManagedIssue']
@@ -221,6 +222,13 @@ def has_topic?(topic)
def require_topic
errors.add(:base, "Please select at least one Topic") if self.topic_ids.blank?
end
+
+ def region_metrocodes
+ conversations.collect do |conversation|
+ conversation.metro_region.metrocode if conversation.metro_region_id.present? && conversation.metro_region.present?
+ end.compact
+ end
+
private
@@ -243,5 +251,6 @@ def assign_position
define_method(:title) do
name
end
+
end
@@ -10,32 +10,53 @@ def initialize(search = nil)
end
end
- def fetch_results(query = nil, *models)
+ def fetch_results(query = nil, options = {})
+ models = [options.delete(:models)]
fields = accepted_fields(models)
fields[:fragment_size] = -1
+ region_metrocodes = options.delete(:region_metrocodes)
+
results = @search.search(models) do
keywords(query) do
highlight fields
end
+
+ # If region_metrocodes present, then filter conversation and issues, and ignore filter an other objects
+ if region_metrocodes.present?
+ classes_with_regional_metrocodes = [Conversation, Issue, ManagedIssue]
+ any_of do
+ all_of do
+ with(:class, classes_with_regional_metrocodes)
+ with(:region_metrocodes, region_metrocodes)
+ end
+ without(:class, classes_with_regional_metrocodes)
+ end
+
+ end
end
results.hits({ verify: true }).reject do |hit|
hit.result.is_a?(Contribution) and not hit.result.confirmed?
end
end
- def fetch_filtered_results(query = nil, filter = nil, *models)
+ def fetch_filtered_results(query = nil, filter = nil, options = {})
+ models = [options.delete(:models)]
fields = accepted_fields(models)
fields[:fragment_size] = -1
+ region_metrocodes = options.delete(:region_metrocodes)
results = @search.search(models) do
with(:content_type, 'BlogPost') if filter == 'blogs'
with(:content_type, 'RadioShow') if filter == 'radioshows'
with(:type, 'Issue') if filter == 'issues'
- if filter == 'projects'
+ case filter
+ when 'projects'
any_of do
with(:type, 'ManagedIssue')
with(:type, 'ManagedIssuePage')
end
+ when 'issues', 'conversations'
+ with(:region_metrocodes, region_metrocodes) if region_metrocodes.present?
end
keywords(query) do
highlight fields
@@ -115,7 +115,7 @@
.nav-search
= render :partial => 'search/search', locals: { filter_type: params[:filter] }
- if user_region
- - if current_page?(:controller => :conversations, :action => :index) || current_page?(:controller => :issues, :action => :index) || current_page?(:controller => :projects, :action => :index)
+ - if current_page?(:controller => :conversations, :action => :index) || current_page?(:controller => :issues, :action => :index) || current_page?(:controller => :projects, :action => :index) || current_page?(:controller => :search, :action => :results)
= render :partial => 'regions/notify'
= yield :main_body
#footer.footer
@@ -4,7 +4,7 @@
<ul class="simple-list block-link-list">
<%- @top_metro_regions.each do |metro_region| -%>
<li>
- <%= link_to raw("<span>#{metro_region.display_name}</span>"), metro_region_filter_url(metro_region.metrocode), :method => :post %>
+ <%= link_to raw("<span>#{metro_region.display_name}</span>" ), metro_region_filter_url(metro_region.metrocode), :method => :post, :class => (default_region.to_s == metro_region.metrocode ? 'active' : '') %>
</li>
<%- end -%>
</ul>
@@ -39,6 +39,7 @@
<% if(@results.length > 0 or params.key?(:filter)) %>
<div class="search-filter">
<div class="mod">
+ <%= render :partial => '/metro_regions/top_metro_side' %>
<h3>Filter Results</h3>
<ul>
<%
@@ -575,5 +575,14 @@ def paperclip_default_file_exists?(style)
conversation.reflection_participants_count.should == 3
end
end
-
+ describe "region_metrocodes" do
+ it "should return nil if there isn't an associated metro region" do
+ @conversation_no_metro = FactoryGirl.create(:conversation, metro_region: nil, metro_region_id:123456789)
+ @conversation_no_metro.region_metrocodes.should == nil
+ end
+ it "should return the metrocode value if an associated metro region is available" do
+ @conversation_with_metro = FactoryGirl.create(:conversation)
+ @conversation_with_metro.region_metrocodes.should == ['510']
+ end
+ end
end
View
@@ -489,4 +489,14 @@ def given_an_issue_with_topics
end
end
end
+ describe "region_metrocodes" do
+ it "should return nil if there isn't an associated metro region" do
+ @issue_no_metro = FactoryGirl.create(:issue)
+ @issue_no_metro.region_metrocodes.should == []
+ end
+ it "should return the metrocode value if an associated metro region is available" do
+ @issue_with_metro = FactoryGirl.create(:issue_with_conversation)
+ @issue_with_metro.region_metrocodes.should == ['510']
+ end
+ end
end
@@ -1,7 +1,13 @@
require 'spec_helper'
describe SearchService do
-
+
+ def create_hit(result)
+ hit = double("Hit")
+ hit.stub(:result) { result }
+ hit
+ end
+
context "Initialization" do
before(:each) do
@@ -15,7 +21,7 @@
@mock_search.should_receive(:search).with([Conversation]).and_return(@mock_search)
@mock_search.should_receive(:hits).and_return([])
- @search.fetch_results(params[:q], Conversation).should == []
+ @search.fetch_results(params[:q], :models => Conversation).should == []
end
it "should return an empty set of results" do
@@ -32,10 +38,5 @@
@search.fetch_results('contribution', Contribution).should == [confirmed_contribution_hit]
end
- def create_hit(result)
- hit = double("Hit")
- hit.stub(:result) { result }
- hit
- end
end
end
@@ -7,6 +7,7 @@
summary: 'original summary'
)
@top_metro_regions = [FactoryGirl.create(:metro_region)]
+ view.stub!(:user_region).and_return(510)
end
it "should show the ManagedIssues's index summaries" do

0 comments on commit 4982293

Please sign in to comment.