Commit a9490a83 by tady

header search form

parent 0d77b39b
......@@ -50,3 +50,8 @@ gem 'devise'
gem 'omniauth-google-oauth2'
gem 'redcarpet'
group :development do
gem "better_errors"
gem "binding_of_caller"
end
......@@ -28,7 +28,13 @@ GEM
arel (4.0.1)
atomic (1.1.14)
bcrypt-ruby (3.1.2)
better_errors (1.0.1)
coderay (>= 1.0.0)
erubis (>= 2.6.6)
binding_of_caller (0.7.2)
debug_inspector (>= 0.0.1)
builder (3.1.4)
coderay (1.1.0)
coffee-rails (4.0.1)
coffee-script (>= 2.2.0)
railties (>= 4.0.0, < 5.0)
......@@ -36,6 +42,7 @@ GEM
coffee-script-source
execjs
coffee-script-source (1.6.3)
debug_inspector (0.0.2)
devise (3.2.2)
bcrypt-ruby (~> 3.0)
orm_adapter (~> 0.1)
......@@ -138,6 +145,8 @@ PLATFORMS
ruby
DEPENDENCIES
better_errors
binding_of_caller
coffee-rails (~> 4.0.0)
devise
jbuilder (~> 1.2)
......
......@@ -59,3 +59,13 @@ $(document).ready(function(){
})
})
});
// search form
$(document).ready(function(){
$('#app-search-form input').on('focus', function(e){
$(this).parents('#app-search-form').animate({width: '600px'});
}).on('blur', function(e){
$(this).parents('#app-search-form').animate({width: '200px'});
})
});
......@@ -42,6 +42,11 @@
border-top-right-radius: 0px;
}
#app-search-form {
width: 200px;
}
/* home#show
-------------------------------------------------- */
.text-box {
......
......@@ -2,11 +2,18 @@ class HomeController < ApplicationController
def show
if user_signed_in?
@latest_posts = Post.last(10)
if params[:q].present?
@posts = Post.build_query(params).limit(10)
else
@posts = Post.order(updated_at: :desc).limit(10)
end
render file: 'home/app'
else
render file: 'home/login'
end
end
end
......@@ -2,4 +2,37 @@ class Post < ActiveRecord::Base
has_many :post_tags
has_many :tags, through: :post_tags
belongs_to :author, class_name: 'User'
# Named scope
def self.build_query(params)
_where_list = self.includes(:author, :tags)
# 空白を一つに変換
query_string = params[:q].gsub(/[\s ]+/, ' ')
query_list = query_string.split(' ')
query_list.each do |_query|
case _query
when /^post:(.+)/
_where_list = _where_list.where('id = ?', $1)
when /^title:(.+)/
_where_list = _where_list.where('title LIKE ?', "%#{$1}%")
when /^body:(.+)/
_where_list = _where_list.where('body LIKE ?', "%#{$1}%")
when /^@(.+)/
_where_list = _where_list.where('users.name = ?', $1)
when /^#(.+)/
_where_list = _where_list.where('tags.name = ?', $1)
when /^date:(\d+)-(\d+)-(\d+)/
_date = Time.new($1, $2, $3)
_where_list = _where_list.where('updated_at > ? AND updated_at < ?', _date, _date + 1.day)
else
_where_list = _where_list.where('body LIKE ? OR body LIKE ?', "%#{$1}%", "%#{$1}%")
end
end
_where_list
end
end
......@@ -9,17 +9,8 @@
<div class="col-xs-6 col-md-4" id="sidebar" role="navigation">
<div class="input-group">
<input type="text" class="form-control">
<span class="input-group-btn">
<button class="btn btn-default" type="button">
<span class="glyphicon glyphicon-filter"></span>
</button>
</span>
</div><!-- /input-group -->
<div class="list-group">
<% @latest_posts.each do |post| %>
<% @posts.each do |post| %>
<a href="#" data-post-id="<%= post.id %>" class="list-group-item post-list"><%= post.title %></a>
<% end %>
</div>
......
......@@ -14,13 +14,15 @@
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<form class="navbar-form navbar-left" role="search">
<div class="form-group">
<input type="text" class="form-control" placeholder="Search">
<form id="app-search-form" class="navbar-form navbar-left" role="search">
<div class="input-group">
<input type="text" name="q" class="form-control" value="<%= params[:q] %>" placeholder="Search">
<span class="input-group-btn">
<button type="submit" class="btn btn-default">
<span class="glyphicon glyphicon-search"></span>
</button>
</span>
</div>
<button type="submit" class="btn btn-default">
<span class="glyphicon glyphicon-search"></span>
</button>
</form>
<div class="navbar-right">
<a href="<%= new_post_path %>" class="btn btn-primary navbar-btn">Post&nbsp;&nbsp;
......
......@@ -14,7 +14,9 @@ module Rendezvous
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
# config.time_zone = 'Central Time (US & Canada)'
config.time_zone = 'Tokyo'
config.active_record.default_timezone = :local
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
......
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