Skip to content

Commit

Permalink
Merge pull request #2 from david-days/search_resource
Browse files Browse the repository at this point in the history
Convert existed search function to resource-based search
  • Loading branch information
david-days committed Jun 7, 2016
2 parents 9d902e4 + 6ae5acc commit febd134
Show file tree
Hide file tree
Showing 64 changed files with 1,010 additions and 163 deletions.
8 changes: 6 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ ruby '2.1.2'
gem 'rails', '~> 3.2.20'
gem 'passenger'

gem 'rails-i18n', '~> 3.0.0'

# run with `bundle install --without production` or `bundle install --without mysql` to exclude this
group :mysql, :production do
gem 'mysql2', '~> 0.3.20'
Expand Down Expand Up @@ -39,7 +41,7 @@ group :test do
#gem 'rspec-rails'
gem 'factory_girl_rails'
gem 'test-unit'
gem 'rake'
gem 'rake', '~> 10.5.0'
end

# run with `bundle install --without production` to exclude these
Expand All @@ -62,4 +64,6 @@ gem "rack-openid"
gem "authlogic", "3.2.0"
gem "php-serialize", :require => "php_serialize"
gem 'less-rails', '~> 2.6'
gem 'jbuilder'
gem 'jbuilder'
gem 'mocha', '~> 1.1'

2 changes: 1 addition & 1 deletion app/assets/javascripts/like.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
function changelikecount(value,node_id) {
var count = $('#like-count-'+node_id).html();
// strip parens and convert to number
count = parseInt(count.substr(1, count.length-2));
count = parseInt(count);
count += value;
// push value back out
$('#like-count-'+node_id).html(count);
Expand Down
21 changes: 21 additions & 0 deletions app/assets/javascripts/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,25 @@ jQuery(document).ready(function() {
}
})

$('#questions_searchform').submit(function(e){
e.preventDefault()
window.location = '/search/questions/'+$('#questions_searchform_input').val()
})

$('#questions_searchform_input').typeahead({
items: 15,
minLength: 3,
source: function (query, process) {
return $.post('/search/questions_typeahead/'+query, {}, function (data) {
return process(data);
})
},
updater: function(item) {
var url;
if ($(item)[0] != undefined) url = $(item)[0].attributes['data-url'].value;
else url = '/search/questions/'+$('#questions_searchform_input').val();
window.location = url;
}
})

})
28 changes: 28 additions & 0 deletions app/assets/stylesheets/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,8 @@ body { padding-top: 70px; }

#tags .label {
line-height: 2em;
font-size: 13px;
margin-right: 4px;
}

/* Styles for specific areas of the site */
Expand Down Expand Up @@ -342,3 +344,29 @@ div.note.moderated h4 {
background: none;
font-size: smaller;
}

.question{
color: #aaa;
font-style: italic;
}
.question + a{
text-decoration: none;
}

.inline{
display: inline-block;
}

.inline img.img-circle{
vertical-align: baseline;
width: 35px;
margin-right: 6px;
}

.inline div.img-circle{
vertical-align: middle;
height: 35px;
width: 35px;
margin-right: 6px;
background: #ccc;
}
8 changes: 7 additions & 1 deletion app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ class ApplicationController < ActionController::Base
layout 'application'

helper_method :current_user_session, :current_user, :prompt_login, :sidebar

before_filter :set_locale

private

Expand Down Expand Up @@ -80,7 +82,7 @@ def require_user
unless current_user
store_location
flash[:notice] = "You must be logged in to access this page"
redirect_to login_url+'?return_to=' + URI.encode(request.env['PATH_INFO'])
redirect_to login_url
return false
end
end
Expand Down Expand Up @@ -111,5 +113,9 @@ def check_and_redirect_node(node)
end
false
end

def set_locale
I18n.locale = params[:locale] || I18n.default_locale
end

end
16 changes: 14 additions & 2 deletions app/controllers/notes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,13 @@ def create
else
flash[:notice] = "Research note published. Get the word out on <a href='/lists'>the discussion lists</a>!"
end
redirect_to @node.path
# Notice: Temporary redirect.Remove this condition after questions show page is complete.
# Just keep @node.path(:question)
if params[:redirect] && params[:redirect] == 'question'
redirect_to @node.path(:question)
else
redirect_to @node.path
end
else
render :template => "editor/post"
end
Expand Down Expand Up @@ -159,7 +165,13 @@ def update
end
@node.save!
flash[:notice] = "Edits saved."
redirect_to @node.path
# Notice: Temporary redirect.Remove this condition after questions show page is complete.
# Just keep @node.path(:question)
if params[:redirect] && params[:redirect] == 'question'
redirect_to @node.path(:question)
else
redirect_to @node.path
end
else
flash[:error] = "Your edit could not be saved."
render :action => :edit
Expand Down
82 changes: 82 additions & 0 deletions app/controllers/questions_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
class QuestionsController < ApplicationController

def index
@title = "Recent Questions"
@notes = DrupalNode.where(status: 1, type: 'note')
.joins(:drupal_tag)
.where('term_data.name LIKE ?', 'question:%')
.order('node.nid DESC')
.paginate(:page => params[:page], :per_page => 30)
@wikis = DrupalNode.limit(10)
.where(type: 'page', status: 1)
.order("nid DESC")
end

def show
if params[:author] && params[:date]
@node = DrupalNode.where(path: "/notes/#{params[:author]}/#{params[:date]}/#{params[:id]}").first
@node = @node || DrupalNode.where(path: "/report/#{params[:id]}").first
else
@node = DrupalNode.find params[:id]
end

unless @node.has_power_tag('question')
flash[:error] = "Not a question"
redirect_to "/"
end

if @node.author.status == 0 && !(current_user && (current_user.role == "admin" || current_user.role == "moderator"))
flash[:error] = "The author of that note has been banned."
redirect_to "/"
elsif @node.status == 4 && (current_user && (current_user.role == "admin" || current_user.role == "moderator"))
flash[:warning] = "First-time poster <a href='#{@node.author.name}'>#{@node.author.name}</a> submitted this #{time_ago_in_words(@node.created_at)} ago and it has not yet been approved by a moderator. <a class='btn btn-default btn-sm' href='/moderate/publish/#{@node.id}'>Approve</a> <a class='btn btn-default btn-sm' href='/moderate/spam/#{@node.id}'>Spam</a>"
elsif @node.status == 4 && (current_user && current_user.id == @node.author.id) && !flash[:first_time_post]
flash[:warning] = "Thank you for contributing open research, and thanks for your patience while your post is approved by <a href='/wiki/moderation'>community moderators</a> and we'll email you when it is published. In the meantime, if you have more to contribute, feel free to do so."
elsif @node.status != 1 && !(current_user && (current_user.role == "admin" || current_user.role == "moderator"))
# if it's spam or a draft
# no notification; don't let people easily fish for existing draft titles; we should try to 404 it
redirect_to "/"
end

@node.view
@title = @node.latest.title
@tags = @node.tags
@tagnames = @tags.collect(&:name)

set_sidebar :tags, @tagnames
end

def shortlink
@node = DrupalNode.find params[:id]
redirect_to @node.path(:question)
end

def popular
@title = "Popular Questions"
@notes = DrupalNode.where(status: 1, type: 'note')
.joins(:drupal_tag)
.where('term_data.name LIKE ?', 'question:%')
.order('node_counter.totalcount DESC')
.includes(:drupal_node_counter)
.limit(20)
@wikis = DrupalNode.limit(10)
.where(type: 'page', status: 1)
.order("nid DESC")
@unpaginated = true
render :template => 'questions/index'
end

def liked
@title = "Highly liked Questions"
@notes = DrupalNode.where(status: 1, type: 'note')
.joins(:drupal_tag)
.where('term_data.name LIKE ?', 'question:%')
.order("cached_likes DESC")
.limit(20)
@wikis = DrupalNode.limit(10)
.where(type: 'page', status: 1)
.order("nid DESC")
@unpaginated = true
render :template => 'questions/index'
end
end
48 changes: 0 additions & 48 deletions app/controllers/search_controller.rb

This file was deleted.

101 changes: 101 additions & 0 deletions app/controllers/searches_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
class SearchesController < ApplicationController

before_filter :set_search_service

def index
end

def new
# Rendering advanced search form
@title = 'Advanced search'
@search = Search.new
@nodes = []
end

def create
@title = 'Advanced search'
@search = Search.new(key_words: params[:id], title: @title)

if @search.save
@nodes = @search.advanced_search(params[:id], params)
render :show
else
puts 'search failed !'
end
end

def show
set_sidebar :tags, [params[:id]]
end

def normal_search
@title = "Search"
@tagnames = params[:id].split(',')
@users = @search_service.users(params[:id])
set_sidebar :tags, [params[:id]]

@notes = DrupalNode.paginate(page: params[:page])
.order("node.nid DESC")
.where('(type = "note" OR type = "page" OR type = "map") AND node.status = 1 AND (node.title LIKE ? OR node_revisions.title LIKE ? OR node_revisions.body LIKE ?)', "%"+params[:id]+"%","%"+params[:id]+"%","%"+params[:id]+"%")
.includes(:drupal_node_revision)
end

# def advanced
# @title = "Advanced search"
# all = !params[:notes] && !params[:wikis] && !params[:maps] && !params[:comments]
# @nodes = []
# end

# utility response to fill out search autocomplete
# needs *dramatic* optimization

def typeahead
@match = @search_service.type_ahead(params[:id])
render json: @match
end

def questions
@title = "Search questions"
@tagnames = params[:id].split(',')
@users = @search_service.users(params[:id])
set_sidebar :tags, [params[:id]]
@notes = DrupalNode.where('type = "note" AND node.status = 1 AND title LIKE ?', "%" + params[:id] + "%")
.joins(:drupal_tag)
.where('term_data.name LIKE ?', 'question:%')
.order('node.nid DESC')
.page(params[:page])
if @notes.empty?
session[:title] = params[:id]
redirect_to '/post?tags=question:question&template=question&title='+params[:id]+'&redirect=question'
else
render :template => 'search/index'
end
end

def questions_typeahead
matches = []
questions = DrupalNode.where('type = "note" AND node.status = 1 AND title LIKE ?', "%" + params[:id] + "%")
.joins(:drupal_tag)
.where('term_data.name LIKE ?', 'question:%')
.order('node.nid DESC')
.limit(25)
questions.each do |match|
matches << "<i data-url='"+match.path(:question)+"' class='fa fa-question-circle'></i> "+match.title
end
render :json => matches
end

def map
@users = DrupalUsers.where("lat != 0.0 AND lon != 0.0")
end

private
def set_search_service
@search_service = SearchService.new
end

def search_params
params.require(:search).permit(:comments, :maps, :wikis, :@notes)
end

end
Loading

0 comments on commit febd134

Please sign in to comment.