-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3d3e414
commit f79c125
Showing
6 changed files
with
115 additions
and
52 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
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,33 +1,79 @@ | ||
# frozen_string_literal: true | ||
|
||
module SearchHelper | ||
extend ActiveSupport::Concern | ||
def self.matched_document(searchable) | ||
if searchable.instance_of?(Comment) | ||
searchable.commentable_type.constantize.find(searchable.commentable_id) | ||
elsif searchable.instance_of?(Answer) || searchable.instance_of?(CorrectAnswer) | ||
searchable.question | ||
else | ||
searchable | ||
end | ||
end | ||
|
||
def searchable_url(searchable) | ||
case searchable | ||
when SearchResult | ||
searchable.url | ||
when Comment | ||
"#{searchable.commentable.url}#comment_#{searchable.id}" | ||
when CorrectAnswer, Answer | ||
Rails.application.routes.url_helpers.question_path(searchable.question, anchor: "answer_#{searchable.id}") | ||
else | ||
helper_method = "#{searchable.class.name.underscore}_path" | ||
Rails.application.routes.url_helpers.send(helper_method, searchable) | ||
end | ||
rescue NoMethodError | ||
raise NoMethodError, "Route for #{searchable.class.name} is not defined. Please check your routes." | ||
end | ||
|
||
included do | ||
def url | ||
case self | ||
when Comment | ||
"#{commentable.url}#comment_#{id}" | ||
when CorrectAnswer | ||
Rails.application.routes.url_helpers.question_path(question, anchor: "answer_#{id}") | ||
when Answer | ||
Rails.application.routes.url_helpers.question_path(question, anchor: "answer_#{id}") | ||
def filtered_message(searchable) | ||
case searchable | ||
when SearchResult | ||
searchable.summary | ||
when Comment | ||
commentable = searchable.commentable_type.constantize.find(searchable.commentable_id) | ||
if policy(commentable).show? || (commentable.is_a?(Practice) && commentable.open_product?) | ||
searchable.body | ||
else | ||
helper_method = "#{self.class.name.underscore}_path" | ||
Rails.application.routes.url_helpers.send(helper_method, self) | ||
'該当プラクティスを修了するまで他の人の提出物へのコメントは見れません。' | ||
end | ||
rescue NoMethodError | ||
raise NoMethodError, "Route for #{self.class.name} is not defined. Please check your routes." | ||
when Product | ||
searchable.body.presence || '本文がありません。' | ||
else | ||
searchable.try(:description) || '本文がありません。' | ||
end | ||
end | ||
|
||
def formatted_summary(word) | ||
target_text = respond_to?(:body) ? body : description | ||
return target_text if word.blank? | ||
def comment_or_answer?(searchable) | ||
if searchable.is_a?(SearchResult) | ||
%w[comment answer correct_answer].include?(searchable.model_name) | ||
else | ||
searchable.is_a?(Comment) || searchable.is_a?(Answer) | ||
end | ||
end | ||
|
||
words = word.split(/[[:blank:]]+/) | ||
words.reduce(target_text) do |text, single_word| | ||
Searcher.highlight_word(text, single_word) | ||
end | ||
def talk?(searchable) | ||
if searchable.is_a?(SearchResult) | ||
searchable.model_name == 'user' && searchable.talk.present? | ||
else | ||
searchable.instance_of?(User) && searchable.talk.present? | ||
end | ||
end | ||
|
||
def user?(searchable) | ||
if searchable.is_a?(SearchResult) | ||
searchable.model_name == 'user' | ||
else | ||
searchable.instance_of?(User) | ||
end | ||
end | ||
|
||
def created_user(searchable) | ||
if searchable.is_a?(SearchResult) | ||
User.find_by(id: searchable.user_id) | ||
else | ||
searchable.respond_to?(:user) ? searchable.user : nil | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,25 @@ | ||
# frozen_string_literal: true | ||
|
||
class SearchResult | ||
include SearchHelper | ||
|
||
attr_accessor :url, :title, :summary, :formatted_summary, :user_id, | ||
:login_name, :formatted_updated_at, :model_name, :label, | ||
:wip, :commentable_user, :commentable_type, :primary_role | ||
|
||
def initialize(searchable, word) | ||
@url = searchable.try(:url) | ||
@url = searchable_url(searchable) | ||
@title = Searcher.fetch_title(searchable) | ||
@summary = searchable.try(:summary) | ||
@formatted_summary = searchable.formatted_summary(word) | ||
@summary = filtered_message(searchable) | ||
@formatted_summary = Searcher.highlight_word(@summary, word) | ||
@user_id = searchable.is_a?(User) ? searchable.id : searchable.try(:user_id) | ||
@login_name = Searcher.fetch_login_name(searchable) | ||
@formatted_updated_at = searchable.formatted_updated_at | ||
@formatted_updated_at = searchable.respond_to?(:formatted_updated_at) ? searchable.formatted_updated_at : searchable.updated_at.strftime('%Y年%m月%d日 %H:%M') | ||
@model_name = searchable.class.name.underscore | ||
@label = Searcher.fetch_label(searchable) | ||
@wip = searchable.try(:wip) | ||
@commentable_user = Searcher.fetch_commentable_user(searchable) | ||
@commentable_type = I18n.t("activerecord.models.#{searchable.try(:commentable)&.try(:model_name)&.name&.underscore}", default: '') | ||
@primary_role = searchable.primary_role | ||
@primary_role = created_user(searchable)&.primary_role | ||
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