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

Feat/search #65

Closed
wants to merge 10 commits into from
Closed

Feat/search #65

wants to merge 10 commits into from

Conversation

shoutarou123
Copy link
Collaborator

対応するissue

対応内容

 りくやさんと一緒に実装した内容にrspecのテスト内容を追加しました。
またrubocopで構文エラーが表示されたため、controllerのsearchメソッドを切り分けるなど変更対応しております。

@kakeru-one kakeru-one added the backend バックエンドのissues label Jul 21, 2024
Copy link
Contributor

@kakeru-one kakeru-one left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shoutarou123
横からコメントしました 🙏

Comment on lines +45 to +46
# 検索gem
gem "ransack"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

横からすみません、コード見た感じ、ransack使うメリットがあまりなさそうなので、
使わずに実装してもらうことはできますでしょうか? 🙇

ransackを使わない方がいいと思った理由は以下の通りです。

  • 他の開発者がコードを読むときに認知負荷がかかる。
    • ransackユーザーはそこまで多くないため。
  • 今回くらいの要件であれば、簡単に検索ロジックを組めそう。
  • 今後のキャリアを考えるのであれば、勉強のために、ransackを使わない方が良さそう。

Copy link
Contributor

@kakeru-one kakeru-one Jul 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

以下のようなmoduleを実装すればいいかなと思います!

参考: https://docs.ruby-lang.org/ja/latest/method/Enumerable/i/inject.html

class Memo
  module SearchResolver
    FILTERS = %i[TitleFilter ContentFilter OrderFilter].freeze
    private_constant :FILTERS

    # @param filter_collection [ActiveRecord::Relation[Memo]]
    # @param params [ActionController::Parameters]
    # @return [ActiveRecord::Relation[Memo]]
    def self.resolve(filter_collection:, params:)
      FILTERS.reduce(filter_collection) do |scope, filter|
        filter.resolve(scope: scope, params: params)
      end
    end

    private

    def resolve
      FILTERS.reduce(filter_collection) do |scope, filter|
        filter.resolve(scope: scope, params: params)
      end
    end

    module TitleFilter
      def resolve(scope:, params:)
        return scope if params[:title].blank?

        # TODO: LIKE検索に書き換える
        scope.where(title: title)
      end
    end
    private_constant :TitleFilter

    module ContentFilter
      def resolve(scope:, params:)
        return scope if params[:title].blank?

        # TODO: LIKE検索に書き換える
        scope.where(content: content)
      end
    end
    private_constant :ContentFilter
    
    module OrderFilter
      def resolve(scope:, params:)
        return scope if params[:title].blank?

        scope.order(id: params[:order])
      end
    end
    private_constant :OrderFilter
  end
end

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ochi-sho-private-study
確認ありがとうございます。
再度実装したいと思いますので、よろしくお願いします

@kakeru-one kakeru-one closed this Aug 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend バックエンドのissues
Projects
None yet
Development

Successfully merging this pull request may close these issues.

メモの検索機能を実装する
2 participants