Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from nashikotokyo/feature/07_search
Feature/07 search
- Loading branch information
Showing
16 changed files
with
102 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,19 @@ | ||
class ApplicationController < ActionController::Base | ||
before_action :set_search_posts_form | ||
add_flash_types :success, :info, :warning, :danger | ||
|
||
private | ||
|
||
def not_authenticated | ||
redirect_to login_path, warning: 'ログインしてください' | ||
end | ||
|
||
# ヘッダー部分(=共通部分)に検索フォームを置くのでApplicationControllerに実装する | ||
def set_search_posts_form | ||
@search_form = SearchPostsForm.new(search_post_params) | ||
end | ||
|
||
def search_post_params | ||
params.fetch(:q, {}).permit(:body, :comment_body, :username) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
class SearchPostsForm | ||
include ActiveModel::Model | ||
include ActiveModel::Attributes | ||
|
||
attribute :body, :string | ||
attribute :comment_body, :string | ||
attribute :username, :string | ||
|
||
def search | ||
scope = Post.distinct | ||
scope = splited_bodies.map { |splited_body| scope.body_contain(splited_body) }.inject { |result, scp| result.or(scp) } if body.present? | ||
scope = scope.comment_body_contain(comment_body) if comment_body.present? | ||
scope = scope.username_contain(username) if username.present? | ||
scope | ||
end | ||
|
||
private | ||
|
||
def splited_bodies | ||
body.strip.split(/[[:blank:]]+/) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
= form_with model: search_form, url: search_posts_path, scope: :q, class: 'form-inline my-2 my-lg-0 mr-auto', method: :get, local: true do |f| | ||
= f.search_field :body, class: 'form-control mr-sm-2', placeholder: '本文' | ||
= f.search_field :comment_body, class: 'form-control mr-sm-2', placeholder: 'コメント' | ||
= f.search_field :username, class: 'form-control mr-sm-2', placeholder: 'ユーザー名' | ||
= f.submit 'Search', class: 'btn btn-outline-success my-2 my-sm-0' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
.container | ||
.row | ||
.col-md-8.col-12.offset-md-2 | ||
h2.text-center | ||
| 検索結果: #{@posts.total_count}件 | ||
= render @posts | ||
= paginate @posts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
db/migrate/20220326075536_change_followerid_and_followedid_of_relationships.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class ChangeFolloweridAndFollowedidOfRelationships < ActiveRecord::Migration[5.2] | ||
def up | ||
change_column :relationships, :follower_id, :bigint, null: false | ||
change_column :relationships, :followed_id, :bigint, null: false | ||
end | ||
|
||
def down | ||
change_column :relationships, :follower_id, :integer, null: false | ||
change_column :relationships, :followed_id, :integer, null: false | ||
end | ||
end |
6 changes: 6 additions & 0 deletions
6
db/migrate/20220326092430_add_foreign_key_to_follower_id_and_followed_id.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class AddForeignKeyToFollowerIdAndFollowedId < ActiveRecord::Migration[5.2] | ||
def change | ||
add_foreign_key :relationships, :users, column: :follower_id | ||
add_foreign_key :relationships, :users, column: :followed_id | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters