diff --git a/.gitignore b/.gitignore index 07edafd..78919eb 100644 --- a/.gitignore +++ b/.gitignore @@ -17,6 +17,6 @@ /bin/set-env.sh -/coverage/* +/coverage/.* *.bk diff --git a/Guardfile b/Guardfile index 4684d50..a503d81 100644 --- a/Guardfile +++ b/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' } @@ -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 diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index 00c5ce3..2e6ebf3 100644 --- a/app/controllers/posts_controller.rb +++ b/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] @@ -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 diff --git a/app/models/post.rb b/app/models/post.rb index 9d3bffe..a85c56f 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -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]}%") @@ -34,7 +31,7 @@ def self.build_query(params) end _where_list - end + end) # generate forked post (not saved) def generate_fork(user) diff --git a/app/views/partials/_app_header.html.erb b/app/views/partials/_app_header.html.erb index 47d51a5..4fa239d 100644 --- a/app/views/partials/_app_header.html.erb +++ b/app/views/partials/_app_header.html.erb @@ -14,7 +14,7 @@