Commit e9de798b by tady

post#search rspec

parent 14c8b331
...@@ -17,6 +17,6 @@ ...@@ -17,6 +17,6 @@
/bin/set-env.sh /bin/set-env.sh
/coverage/* /coverage/.*
*.bk *.bk
guard :rspec, spring: true do guard :rspec, all_after_pass: true, spring: true do
watch(%r{^spec/.+_spec\.rb$}) watch(%r{^spec/.+_spec\.rb$})
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" } watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
watch('spec/spec_helper.rb') { 'spec' } watch('spec/spec_helper.rb') { 'spec' }
...@@ -11,7 +11,7 @@ guard :rspec, spring: true do ...@@ -11,7 +11,7 @@ guard :rspec, spring: true do
watch('app/controllers/application_controller.rb') { 'spec/controllers' } watch('app/controllers/application_controller.rb') { 'spec/controllers' }
end end
guard :rubocop, cli: ['--rails', '--auto-correct'] do guard :rubocop, all_after_pass: true, cli: ['--rails', '--auto-correct'] do
watch(%r{.+\.rb$}) watch(%r{.+\.rb$})
watch(%r{(?:.+/)?\.rubocop\.yml$}) { |m| File.dirname(m[0]) } watch(%r{(?:.+/)?\.rubocop\.yml$}) { |m| File.dirname(m[0]) }
end end
require 'nkf' require 'nkf'
require 'rv/mailer'
class PostsController < ApplicationController class PostsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update, :destroy] before_action :set_post, only: [:show, :edit, :update, :destroy]
...@@ -12,7 +11,7 @@ class PostsController < ApplicationController ...@@ -12,7 +11,7 @@ class PostsController < ApplicationController
# GET /posts.json # GET /posts.json
def index def index
if params[:q].present? if params[:q].present?
@posts = Post.build_query(params).limit(10) @posts = Post.search(params[:q]).limit(10)
else else
@posts = Post.order(updated_at: :desc).limit(10) @posts = Post.order(updated_at: :desc).limit(10)
end end
......
...@@ -4,18 +4,15 @@ class Post < ActiveRecord::Base ...@@ -4,18 +4,15 @@ class Post < ActiveRecord::Base
belongs_to :author, class_name: 'User' belongs_to :author, class_name: 'User'
# Named scope # Named scope
scope :search, (lambda do |query|
def self.build_query(params)
_where_list = includes(:author, :tags) _where_list = includes(:author, :tags)
# Convert spaces to one space. # Convert spaces to one space.
query_string = params[:q].gsub(/[\s ]+/, ' ') query_list = query.gsub(/[\s ]+/, ' ').split(' ')
query_list = query_string.split(' ')
query_list.each do |_query| query_list.each do |_query|
case _query case _query
when /^post:(.+)/ when /^id:(.+)/
_where_list = _where_list.where('id = ?', Regexp.last_match[1]) _where_list = _where_list.where('id = ?', Regexp.last_match[1])
when /^title:(.+)/ when /^title:(.+)/
_where_list = _where_list.where('title LIKE ?', "%#{Regexp.last_match[1]}%") _where_list = _where_list.where('title LIKE ?', "%#{Regexp.last_match[1]}%")
...@@ -34,7 +31,7 @@ class Post < ActiveRecord::Base ...@@ -34,7 +31,7 @@ class Post < ActiveRecord::Base
end end
_where_list _where_list
end end)
# generate forked post (not saved) # generate forked post (not saved)
def generate_fork(user) def generate_fork(user)
......
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
<!-- Collect the nav links, forms, and other content for toggling --> <!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1"> <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<form id="app-search-form" class="navbar-form navbar-left" role="search" action="<%= root_path %>"> <form id="app-search-form" class="navbar-form navbar-left" role="search" action="<%= posts_path %>">
<div class="input-group"> <div class="input-group">
<input type="text" name="q" class="form-control" value="<%= params[:q] %>" placeholder="Search"> <input type="text" name="q" class="form-control" value="<%= params[:q] %>" placeholder="Search">
<span class="input-group-btn"> <span class="input-group-btn">
......
{
"result": {
"covered_percent": 82.72
}
}
# Read about factories at https://github.com/thoughtbot/factory_girl # Read about factories at https://github.com/thoughtbot/factory_girl
FactoryGirl.define do FactoryGirl.define do
factory :post do factory :post do
title 'sample title' title 'sample title'
body 'sample body' body 'sample body'
...@@ -12,5 +13,4 @@ FactoryGirl.define do ...@@ -12,5 +13,4 @@ FactoryGirl.define do
factory :post_tag do factory :post_tag do
end end
end end
...@@ -31,4 +31,52 @@ describe Post do ...@@ -31,4 +31,52 @@ describe Post do
end end
end end
describe 'scope :search' do
before :each do
@alice = create(:alice)
@post1 = Post.create id: 1001, title: 'ruby rspec', body: 'This is first espec test: ruby'
@post2 = Post.create id: 1002, title: 'php test', body: 'PHP is very easy', author_id: @alice.id
@post3 = Post.create id: 1003, title: 'java java...', body: 'Java is not ruby...', updated_at: Time.new(1989, 2, 25, 5, 30, 0)
@tag_java = Tag.create(name: 'java')
@post3.tags << @tag_java
end
it 'by id' do
expect(Post.search('id:1001')).to have(1).items
expect(Post.search('id:1001')).to include(@post1)
end
it 'by title' do
expect(Post.search('title:ruby')).to have(1).items
expect(Post.search('title:ruby')).to include(@post1)
end
it 'by body' do
expect(Post.search('body:ruby')).to have(2).items
expect(Post.search('body:ruby')).to include(@post3)
end
it 'by @<author_name>' do
expect(Post.search('@Alice')).to have(1).items
expect(Post.search('@Alice')).to include(@post2)
end
it 'by #<tag_name>' do
expect(Post.search('#java')).to have(1).items
expect(Post.search('#java')).to include(@post3)
end
it 'by date' do
expect(Post.search('date:1989-2-25')).to have(1).items
expect(Post.search('date:1989-2-25')).to include(@post3)
end
it 'by else' do
expect(Post.search('ruby')).to have(2).items
expect(Post.search('ruby')).to include(@post1)
expect(Post.search('ruby')).to include(@post3)
end
end
end end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment