Skip to content

Railsジェネレーター

Servactoryは一般的なタスクのためのRailsジェネレーターを提供します。

インストールジェネレーター 2.5.0以降

基本的なサービスインフラストラクチャをセットアップします。

shell
bundle exec rails g servactory:install

生成されるファイル

ファイル説明
app/services/application_service/base.rb基底サービスクラス
app/services/application_service/exceptions.rb例外クラス
app/services/application_service/result.rb結果クラス

オプション

オプションデフォルト説明
--pathapp/services出力ディレクトリ
--namespaceApplicationService基底ネームスペース

shell
# Default installation
bundle exec rails g servactory:install

# Custom namespace
bundle exec rails g servactory:install --namespace=MyApp::Services

# Custom path
bundle exec rails g servactory:install --path=lib/services

サービスジェネレーター 2.5.0以降

型付きinputを持つ新しいサービスを作成します。

shell
bundle exec rails g servactory:service NAME [inputs...]

オプション

オプションデフォルト説明
--pathapp/services出力ディレクトリ

型のショートカット

構文結果
nameまたはname:stringinput :name, type: String
age:integerinput :age, type: Integer
active:booleaninput :active, type: [TrueClass, FalseClass]
user:Userinput :user, type: User
items:arrayinput :items, type: Array
data:hashinput :data, type: Hash

shell
# Basic service
bundle exec rails g servactory:service users_service/create

# With typed inputs
bundle exec rails g servactory:service orders_service/process user:User amount:integer

# Nested namespace
bundle exec rails g servactory:service admin/reports/generate started_on:date ended_on:date

RSpecジェネレーター 2.5.0以降

サービス用のRSpecテストファイルを作成します。

shell
bundle exec rails g servactory:rspec NAME [inputs...]

オプション

オプションデフォルト説明
--pathspec/services出力ディレクトリ

shell
# Generate spec matching service inputs
bundle exec rails g servactory:rspec users_service/create first_name last_name email

# For existing service
bundle exec rails g servactory:rspec orders_service/process

拡張機能ジェネレーター 3.0.0以降

新しい拡張機能モジュールを作成します。

shell
bundle exec rails g servactory:extension NAME

オプション

オプションデフォルト説明
--pathapp/services/application_service/extensions出力ディレクトリ
--namespaceApplicationService基底ネームスペース

shell
# Basic extension
bundle exec rails g servactory:extension Auditable

# Nested namespace
bundle exec rails g servactory:extension Admin::AuditTrail

# Custom path
bundle exec rails g servactory:extension Cacheable --path=lib/extensions

使用方法の詳細は拡張機能を参照してください。