Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1510 でのレビュー指摘内容の修正 #1541

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,9 @@ Style/ColonMethodCall:
Style/TrivialAccessors:
Enabled: true

Style/AccessModifierDeclarations:
Enabled: true

Performance/FlatMap:
Enabled: true

Expand Down
16 changes: 1 addition & 15 deletions app/models/announcement.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,5 @@ class Announcement < ApplicationRecord
validates :description, presence: true
validates :target, presence: true

concerning :KeywordSearch do
class_methods do
private

def params_for_keyword_search(searched_values = {})
{ groupings: groupings(split_keyword_by_blank(searched_values[:word])) }
end

def groupings(words)
words.map do |word|
{ title_or_description_cont_all: word }
end
end
end
end
columns_for_keyword_search :title, :description
end
16 changes: 1 addition & 15 deletions app/models/answer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,7 @@ class Answer < ActiveRecord::Base
validates :description, presence: true
validates :user, presence: true

concerning :KeywordSearch do
class_methods do
private

def params_for_keyword_search(searched_values = {})
{ groupings: groupings(split_keyword_by_blank(searched_values[:word])) }
end

def groupings(words)
words.map do |word|
{ description_cont_all: word }
end
end
end
end
columns_for_keyword_search :description

def receiver
self.question.user
Expand Down
23 changes: 9 additions & 14 deletions app/models/comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,15 @@ class Comment < ActiveRecord::Base

validates :description, presence: true

concerning :KeywordSearch do
class_methods do
private

def params_for_keyword_search(searched_values = {})
{ commentable_type_eq: searched_values[:commentable_type], groupings: groupings(split_keyword_by_blank(searched_values[:word])) }
end

def groupings(words)
words.map do |word|
{ description_cont_all: word }
end
end
end
columns_for_keyword_search :description

class << self
private

def params_for_keyword_search(searched_values = {})
groupings = super
{ commentable_type_in: searched_values[:commentable_type] }.merge(groupings)
end
end

def receiver
Expand Down
15 changes: 15 additions & 0 deletions app/models/concerns/searchable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,23 @@ def search_by_keywords(searched_values = {})
ransack(**params_for_keyword_search(searched_values)).result
end

def columns_for_keyword_search(*key_names)
define_singleton_method(:_grouping_condition) { "#{key_names.join("_or_")}_cont_all" }
end

private

def params_for_keyword_search(searched_values = {})
return {} if searched_values[:word].blank?
{ groupings: groupings(split_keyword_by_blank(searched_values[:word])) }
end

def groupings(words)
words.map do |word|
{ _grouping_condition => word }
end
end

def split_keyword_by_blank(word)
word.split(/[[:blank:]]/)
end
Expand Down
16 changes: 1 addition & 15 deletions app/models/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,5 @@ class Page < ActiveRecord::Base
validates :body, presence: true
paginates_per 20

concerning :KeywordSearch do
class_methods do
private

def params_for_keyword_search(searched_values = {})
{ groupings: groupings(split_keyword_by_blank(searched_values[:word])) }
end

def groupings(words)
words.map do |word|
{ title_or_body_cont_all: word }
end
end
end
end
columns_for_keyword_search :title, :body
end
16 changes: 1 addition & 15 deletions app/models/practice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,7 @@ class Practice < ActiveRecord::Base

scope :category_order, -> { includes(:category).order("categories.position").order(:position) }

concerning :KeywordSearch do
class_methods do
private

def params_for_keyword_search(searched_values = {})
{ groupings: groupings(split_keyword_by_blank(searched_values[:word])) }
end

def groupings(words)
words.map do |word|
{ title_or_description_or_goal_cont_all: word }
end
end
end
end
columns_for_keyword_search :title, :description, :goal

def status(user)
learnings = Learning.where(
Expand Down
16 changes: 1 addition & 15 deletions app/models/question.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,5 @@ class Question < ActiveRecord::Base
scope :solved, -> { joins(:correct_answer) }
scope :not_solved, -> { where.not(id: CorrectAnswer.pluck(:question_id)) }

concerning :KeywordSearch do
class_methods do
private

def params_for_keyword_search(searched_values = {})
{ groupings: groupings(split_keyword_by_blank(searched_values[:word])) }
end

def groupings(words)
words.map do |word|
{ title_or_description_cont_all: word }
end
end
end
end
columns_for_keyword_search :title, :description
end
16 changes: 1 addition & 15 deletions app/models/report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,7 @@ class Report < ActiveRecord::Base
after_update ReportCallbacks.new
after_destroy ReportCallbacks.new

concerning :KeywordSearch do
class_methods do
private

def params_for_keyword_search(searched_values = {})
{ groupings: groupings(split_keyword_by_blank(searched_values[:word])) }
end

def groupings(words)
words.map do |word|
{ title_or_description_cont_all: word }
end
end
end
end
columns_for_keyword_search :title, :description

def previous
Report.where(user: user)
Expand Down
20 changes: 4 additions & 16 deletions app/models/searcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ class Searcher
class << self
def search(word, document_type: :all)
case document_type
when all?
when :all
AVAILABLE_TYPES.flat_map { |type| result_for(type, word) }.sort_by { |result| result.created_at }.reverse
when commentable?
[document_type, :comments].flat_map { |type| result_for(type, word, commentable_type: model_name(document_type)) }.sort_by { |result| result.created_at }.reverse
when question?
when :questions
[document_type, :answers].flat_map { |type| result_for(type, word) }
else
result_for(document_type, word).sort_by { |result| result.created_at }.reverse
Expand All @@ -29,26 +29,14 @@ def search(word, document_type: :all)
private

def result_for(type, word, commentable_type: nil)
return [] unless available_type?(type)
type.to_s.capitalize.singularize.constantize.search_by_keywords(word: word, commentable_type: commentable_type)
end

def available_type?(type)
AVAILABLE_TYPES.find { |available_type| available_type == type }.present?
end

def all?
-> (document_type) { document_type == :all }
raise ArgumentError.new("#{type} is not available type") unless type.in?(AVAILABLE_TYPES)
model(type).search_by_keywords(word: word, commentable_type: commentable_type)
end

def commentable?
-> (document_type) { model(document_type).include?(Commentable) }
end

def question?
-> (document_type) { document_type == :questions }
end

def model(type)
model_name(type).constantize
end
Expand Down
16 changes: 16 additions & 0 deletions test/models/searcher_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,20 @@ class SearchableTest < ActiveSupport::TestCase
assert_includes(result, comments(:comment_6))
assert_not_includes(result, comments(:comment_5))
end

test "returns only comments associated to specified document_type" do
result = Searcher.search("コメント", document_type: :reports)
assert_equal [comments(:comment_11)], result
end

test "returns all comments when document_type is not specified" do
result = Searcher.search("コメント")
assert_includes(result, comments(:comment_8))
assert_includes(result, comments(:comment_10))
assert_includes(result, comments(:comment_11))
assert_includes(result, comments(:comment_12))
assert_includes(result, comments(:comment_13))
assert_includes(result, comments(:comment_14))
assert_equal(6, result.size)
end
end