Skip to content

Instantly share code, notes, and snippets.

@seiyawati
Created November 18, 2023 08:13
Show Gist options
  • Select an option

  • Save seiyawati/f237e671c6da12f5b51754dcc24c7876 to your computer and use it in GitHub Desktop.

Select an option

Save seiyawati/f237e671c6da12f5b51754dcc24c7876 to your computer and use it in GitHub Desktop.
Ruby Facade Pattern
# 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