Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
R
rendezvous
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
VeNtura
rendezvous
Commits
e9de798b
Commit
e9de798b
authored
Dec 29, 2013
by
tady
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
post#search rspec
parent
14c8b331
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
58 additions
and
19 deletions
+58
-19
.gitignore
+1
-1
Guardfile
+2
-2
app/controllers/posts_controller.rb
+1
-2
app/models/post.rb
+4
-7
app/views/partials/_app_header.html.erb
+1
-1
coverage/.last_run.json
+0
-5
spec/factories/posts.rb
+1
-1
spec/models/post_spec.rb
+48
-0
No files found.
.gitignore
View file @
e9de798b
...
@@ -17,6 +17,6 @@
...
@@ -17,6 +17,6 @@
/bin/set-env.sh
/bin/set-env.sh
/coverage/*
/coverage/
.
*
*.bk
*.bk
Guardfile
View file @
e9de798b
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
app/controllers/posts_controller.rb
View file @
e9de798b
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
...
...
app/models/post.rb
View file @
e9de798b
...
@@ -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
)
...
...
app/views/partials/_app_header.html.erb
View file @
e9de798b
...
@@ -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"
>
...
...
coverage/.last_run.json
deleted
100644 → 0
View file @
14c8b331
{
"result"
:
{
"covered_percent"
:
82.72
}
}
spec/factories/posts.rb
View file @
e9de798b
# 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
spec/models/post_spec.rb
View file @
e9de798b
...
@@ -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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment