https://github.com/takeruun/rails-domain
Domain-driven development with useacase implemented in rails
https://github.com/takeruun/rails-domain
interactor rails usecase
Last synced: 2 days ago
JSON representation
Domain-driven development with useacase implemented in rails
- Host: GitHub
- URL: https://github.com/takeruun/rails-domain
- Owner: takeruun
- Created: 2022-10-22T04:45:34.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2022-10-23T07:43:56.000Z (over 3 years ago)
- Last Synced: 2025-06-24T10:49:51.049Z (10 months ago)
- Topics: interactor, rails, usecase
- Language: Ruby
- Homepage:
- Size: 48.8 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# About
Usecase/Interactor を利用した Rails-DDD Todo アプリケーション
## Interactorオブジェクト
Interactorオブジェクトとは「**デザインパターンのひとつで、ビジネスロジックをカプセル化するためのモデル層に属するクラス群**」。
ひとつのInteractorオブジェクトはひとつの責務をもつ。
※「ひとつの責務」とは、たとえば「記事を投稿する」「決済を行う」という、それ以上分割できない責務。
下記は、「ユーザーを見つける」Interactor です。
```ruby
module UserDomain
module Usecases
module Interactors
class FindUserInteractor < ApplicationInteractor
delegate :params, to: :context, private: true
def call
user = Models::User.find_by(email: params[:email])
if user.nil?
context.fail!(message: 'ユーザーが見つかりません。')
else
context[:user] = user
end
end
end
end
end
end
```
## Usecaseオブジェクト
Usecaseオブジェクトとは複数のInteractorを構成するクラスです。
「ユーザーを見つける」「パスワードを検証する」「セッションに保存する」の Interactor の使用を定義します。
```ruby
module UserDomain
module Usecases
class SignIn < ApplicationUsecase
include InteractorTransactional
organize Interactors::FindUserInteractor,
Interactors::PasswordVerificationInteractor,
Interactors::UpdateSessionInteractor
end
end
end
```
**参考サイト**
- https://applis.io/posts/rails-design-pattern-interactor-objects