Permalink
Browse files

Add missing files

  • Loading branch information...
1 parent ad35d17 commit a8f3fb97777e3464d30f014b69c32efce07d3abd @sikachu committed Mar 26, 2009
@@ -0,0 +1,32 @@
+class SearchesController < ApplicationController
+ before_filter :process_search_vars, :only => :show
+
+ def index
+ @search = Search.new
+ end
+
+ def create
+ params[:search][:keyword].strip!
+ if params[:search][:keyword].empty? or params[:search][:keyword].size < 2
+ redirect_to searches_path
+ else
+ @search = Search.new(params[:search])
+ @search.process!
+ redirect_to search_path(@search.permalink)
+ end
+ end
+
+ def show
+ @search = Search.find_by_permalink(params[:id])
+ @search.load_objects!("sorted_by_#{params[:type]}", @offset)
+ end
+
+ protected
+
+ def process_search_vars
+ params[:type] = "relevance" unless %w(relevance added updated).include?(params[:type])
+ params[:page] ||= 1
+
+ @offset = (params[:page].to_i - 1) * 10 rescue 0
+ end
+end
View
@@ -0,0 +1,71 @@
+require 'digest'
+
+class Search < ActiveRecord::Base
+ serialize :sorted_by_relevance
+ serialize :sorted_by_added
+ serialize :sorted_by_updated
+
+ def process!
+ # Find each collection, then stores it to global array using merge_result_data
+ merge_result_data :location, Location.all(:conditions => ["title LIKE ? OR description LIKE ?", "%#{keyword}%", "%#{keyword}%"])
+ merge_result_data :event, Event.all(:conditions => ["name LIKE ? OR description LIKE ?", "%#{keyword}%", "%#{keyword}%"])
+
+ if tag = Tag.find_by_name(keyword, :include => [:locations, :events])
+ merge_result_data :location, tag.locations
+ merge_result_data :event, tag.events
+ end
+
+ # Sort the object ids based on criteria
+ self.sorted_by_relevance = result_object_ids.sort do |y,x| # Max -> Min
+ fetch_object(x).title.count(keyword) + fetch_object(x).description.count(keyword) <=> fetch_object(y).title.count(keyword) + fetch_object(y).description.count(keyword)
+ end
+ self.sorted_by_added = result_object_ids.sort do |y,x|
+ fetch_object(x).created_at <=> fetch_object(y).created_at
+ end
+ self.sorted_by_updated = result_object_ids.sort do |y,x|
+ fetch_object(x).updated_at <=> fetch_object(y).updated_at
+ end
+
+ # Done! Now save the data
+ self.permalink = Digest::SHA1.hexdigest("#{rand(2**100)}#{Time.now.to_s}") # Avoid collision
+ self.save!
+ end
+
+ def fetch_object(id)
+ result_objects.assoc(id)[1] rescue nil
+ end
+
+ def load_objects!(type, offset=0, limit=10)
+ # construct array for using with search
+ location_ids, event_ids = [], []
+ self.send(type).each do |object_type_and_id|
+ object_type, object_id = object_type_and_id.split(/-/)
+ if object_type == "location"
+ location_ids << object_id
+ else
+ event_ids << object_id
+ end
+ end
+
+ merge_result_data :location, Location.find_all_by_id(location_ids)
+ merge_result_data :event, Event.find_all_by_id(event_ids)
+ end
+
+ private
+
+ def merge_result_data(type, objects)
+ self.result_objects = result_objects | objects.collect{ |obj| ["#{type}-#{obj.id}", obj] }
+ end
+
+ def result_objects
+ @result_objects ||= []
+ end
+
+ def result_objects=(args)
+ @result_objects = args
+ end
+
+ def result_object_ids
+ result_objects.collect{ |r| r.first }
+ end
+end
@@ -0,0 +1,5 @@
+- content_for :headline do
+ - form_for @search do |f|
+ = f.label :keyword, "Search for"
+ = f.text_field :keyword
+ = f.submit "Search", :id => "search-button"
@@ -0,0 +1,4 @@
+= render "search_bar"
+
+#fullcontent
+ Enter your keyword about. The system will try to search from title, description, and tags.
@@ -0,0 +1,52 @@
+= render "search_bar"
+
+#fullcontent.gray
+ .floatleft
+ Search result for
+ %span.black= @search.keyword
+ showing
+ = @offset + 1
+ \-
+ = @offset + 10 > @search.sorted_by_relevance.size ? @search.sorted_by_relevance.size : @offset + 10
+ from
+ = @search.sorted_by_relevance.size
+ .floatright
+ Order by:
+ = link_to_unless params[:type] == "relevance", "Relevance", :type => "relevance", :page => params[:page]
+ |
+ = link_to_unless params[:type] == "added", "Date added", :type => "added", :page => params[:page]
+ |
+ = link_to_unless params[:type] == "updated", "Last updated", :type => "updated", :page => params[:page]
+ .clear
+
+- one_column do
+ = content_block do
+ - @search.send("sorted_by_#{params[:type]}")[@offset, 10].each do |id|
+ - object = @search.fetch_object(id)
+ .item[object]
+ .marker
+ - if object.class == Location
+ .location_badge
+ .top{:title => "Number of stars"}= object.stars / 10.0
+ .bottom{:title => "Number of feedbacks"}= object.feedbacks.count
+ - else
+ .event_badge
+ .top{:title => "Number of participants"}= object.participations.count
+ .bottom{:title => "Number of watchers"}= object.watchers.count
+ .info
+
+ %h3= link_to object.title, object_permalink(object)
+ %small
+ in
+ - if object.category.parent
+ = link_to object.category.parent.title, category_permalink(object.category.parent)
+ &gt;
+ = link_to object.category.title, category_permalink(object.category)
+ .spaceh10
+ .content.markdown
+ :markdown
+ #{object.description}
+ .photo
+ = image_tag object.class == Event ? object.location.thumbnail : object.thumbnail
+ .clear
+ .hr
@@ -0,0 +1,11 @@
+class RemoveScoreAndVoteCountFromLocationVersions < ActiveRecord::Migration
+ def self.up
+ remove_column :location_versions, :score
+ remove_column :location_versions, :vote_count
+ end
+
+ def self.down
+ add_column :location_versions, :vote_count, :integer
+ add_column :location_versions, :score, :integer
+ end
+end
@@ -0,0 +1,17 @@
+class CreateSearches < ActiveRecord::Migration
+ def self.up
+ create_table :searches do |t|
+ t.string :keyword
+ t.string :permalink
+ t.text :sorted_by_relevance
+ t.text :sorted_by_added
+ t.text :sorted_by_updated
+
+ t.timestamps
+ end
+ end
+
+ def self.down
+ drop_table :searches
+ end
+end
View
@@ -0,0 +1,13 @@
+# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
+
+one:
+ keyword: MyString
+ sorted_by_relevance: MyText
+ sorted_by_added: MyText
+ sorted_by_updated: MyText
+
+two:
+ keyword: MyString
+ sorted_by_relevance: MyText
+ sorted_by_added: MyText
+ sorted_by_updated: MyText
View
@@ -0,0 +1,8 @@
+require 'test_helper'
+
+class SearchTest < ActiveSupport::TestCase
+ # Replace this with your real tests.
+ test "the truth" do
+ assert true
+ end
+end

0 comments on commit a8f3fb9

Please sign in to comment.