Add global search action to search posts and users

This commit is contained in:
2015-10-12 21:58:58 +08:00
parent 541ddd37a2
commit 30d7e935a1
7 changed files with 46 additions and 0 deletions
@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
@@ -0,0 +1,3 @@
// Place all the styles related to the api/v1/search controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
@@ -0,0 +1,16 @@
class Api::V1::SearchController < ApplicationController
skip_before_action :authenticate_user_from_token
def index
#.pluck(:id, :title, :url, :type, :good_format, :summary)
posts = Post.where('title LIKE ?',"%#{params[:keywords]}%").limit(5)
#.pluck(:id, :email, :name, :avatar)
users = User.where('name LIKE ?', "%#{params[:keywords]}%").limit(5)
render json: {
posts: posts,
users: users
}
end
end
+2
View File
@@ -0,0 +1,2 @@
module Api::V1::SearchHelper
end
+2
View File
@@ -107,6 +107,8 @@ Rails.application.routes.draw do
get '/timeline/latest', to: 'timeline#latest'
get '/timeline/hottest', to: 'timeline#hottest'
get '/search', to: 'search#index'
end
end
end
@@ -0,0 +1,5 @@
require 'rails_helper'
RSpec.describe Api::V1::SearchController, type: :controller do
end
+15
View File
@@ -0,0 +1,15 @@
require 'rails_helper'
# Specs in this file have access to a helper object that includes
# the Api::V1::SearchHelper. For example:
#
# describe Api::V1::SearchHelper do
# describe "string concat" do
# it "concats two strings with spaces" do
# expect(helper.concat_strings("this","that")).to eq("this that")
# end
# end
# end
RSpec.describe Api::V1::SearchHelper, type: :helper do
pending "add some examples to (or delete) #{__FILE__}"
end