Created
November 18, 2023 08:13
-
-
Save seiyawati/f237e671c6da12f5b51754dcc24c7876 to your computer and use it in GitHub Desktop.
Ruby Facade Pattern
This file contains hidden or 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
| # FacadeパターンはControllerとViewからプレゼンテーションロジックを抽出したPlain Ruby Objectのインターフェイス | |
| # 参考↓ | |
| # https://refactoring.guru/ja/design-patterns/facade/ruby/example | |
| # https://blog.appsignal.com/2020/03/18/facade-pattern-in-rails-for-performance-and-maintainability.html | |
| ## Facade未使用 | |
| class BooksController < ApplicationController | |
| def index | |
| @books = if params[:query].present? | |
| current_user.books.where('name ILIKE ?', "%#{params[:query]}%") | |
| elsif params[:isbn].present? | |
| current_user.books.where(isbn: params[:isbn]) | |
| else | |
| current_user.books | |
| end | |
| @books.order(created_at: :desc).page(params[:page]) | |
| @recommended = @books.where(some_complex_query: true) | |
| end | |
| end | |
| ## Facade使用 | |
| class BooksController < ApplicationController | |
| def index | |
| @index_facade = Books::IndexFacade.new(user: current_user, params: params) | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment