Skip to content

Commit

Permalink
post#search rspec
Browse files Browse the repository at this point in the history
  • Loading branch information
tadyjp committed Dec 29, 2013
1 parent 14c8b33 commit e9de798
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Expand Up @@ -17,6 +17,6 @@

/bin/set-env.sh

/coverage/*
/coverage/.*

*.bk
4 changes: 2 additions & 2 deletions Guardfile
@@ -1,4 +1,4 @@
guard :rspec, spring: true do
guard :rspec, all_after_pass: true, spring: true do
watch(%r{^spec/.+_spec\.rb$})
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
watch('spec/spec_helper.rb') { 'spec' }
Expand All @@ -11,7 +11,7 @@ guard :rspec, spring: true do
watch('app/controllers/application_controller.rb') { 'spec/controllers' }
end

guard :rubocop, cli: ['--rails', '--auto-correct'] do
guard :rubocop, all_after_pass: true, cli: ['--rails', '--auto-correct'] do
watch(%r{.+\.rb$})
watch(%r{(?:.+/)?\.rubocop\.yml$}) { |m| File.dirname(m[0]) }
end
3 changes: 1 addition & 2 deletions app/controllers/posts_controller.rb
@@ -1,5 +1,4 @@
require 'nkf'
require 'rv/mailer'

class PostsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update, :destroy]
Expand All @@ -12,7 +11,7 @@ class PostsController < ApplicationController
# GET /posts.json
def index
if params[:q].present?
@posts = Post.build_query(params).limit(10)
@posts = Post.search(params[:q]).limit(10)
else
@posts = Post.order(updated_at: :desc).limit(10)
end
Expand Down
11 changes: 4 additions & 7 deletions app/models/post.rb
Expand Up @@ -4,18 +4,15 @@ class Post < ActiveRecord::Base
belongs_to :author, class_name: 'User'

# Named scope

def self.build_query(params)
scope :search, (lambda do |query|
_where_list = includes(:author, :tags)

# Convert spaces to one space.
query_string = params[:q].gsub(/[\s ]+/, ' ')

query_list = query_string.split(' ')
query_list = query.gsub(/[\s ]+/, ' ').split(' ')

query_list.each do |_query|
case _query
when /^post:(.+)/
when /^id:(.+)/
_where_list = _where_list.where('id = ?', Regexp.last_match[1])
when /^title:(.+)/
_where_list = _where_list.where('title LIKE ?', "%#{Regexp.last_match[1]}%")
Expand All @@ -34,7 +31,7 @@ def self.build_query(params)
end

_where_list
end
end)

# generate forked post (not saved)
def generate_fork(user)
Expand Down
2 changes: 1 addition & 1 deletion app/views/partials/_app_header.html.erb
Expand Up @@ -14,7 +14,7 @@

<!-- Collect the nav links, forms, and other content for toggling -->
<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">
<input type="text" name="q" class="form-control" value="<%= params[:q] %>" placeholder="Search">
<span class="input-group-btn">
Expand Down
5 changes: 0 additions & 5 deletions coverage/.last_run.json

This file was deleted.

2 changes: 1 addition & 1 deletion spec/factories/posts.rb
@@ -1,6 +1,7 @@
# Read about factories at https://github.com/thoughtbot/factory_girl

FactoryGirl.define do

factory :post do
title 'sample title'
body 'sample body'
Expand All @@ -12,5 +13,4 @@

factory :post_tag do
end

end
48 changes: 48 additions & 0 deletions spec/models/post_spec.rb
Expand Up @@ -31,4 +31,52 @@
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

0 comments on commit e9de798

Please sign in to comment.