plutonium 0.14.1 → 0.15.0.pre.rc1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README copy.md +1 -1
- data/README.md +1 -1
- data/app/assets/plutonium.css +1 -1
- data/app/views/{application → plutonium}/_resource_header.html copy.erb +1 -1
- data/app/views/{application → plutonium}/_resource_header.html.erb +1 -1
- data/app/views/{application → plutonium}/_resource_sidebar.html.erb +2 -0
- data/app/views/resource/_resource_details.html.erb +1 -36
- data/app/views/resource/_resource_form.html.erb +1 -5
- data/app/views/resource/_resource_table.html.erb +315 -85
- data/app/views/resource/edit.html.erb +1 -5
- data/app/views/resource/index.html.erb +1 -5
- data/app/views/resource/new.html.erb +1 -5
- data/app/views/resource/show.html.erb +1 -5
- data/config/initializers/pagy.rb +1 -0
- data/config/initializers/rabl.rb +27 -20
- data/gemfiles/rails_7.gemfile.lock +5 -1
- data/lib/generators/pu/core/install/templates/app/controllers/plutonium_controller.rb.tt +2 -0
- data/lib/generators/pu/core/install/templates/app/controllers/resource_controller.rb.tt +21 -1
- data/lib/generators/pu/core/install/templates/app/definitions/resource_definition.rb.tt +2 -0
- data/lib/generators/pu/core/install/templates/app/models/resource_record.rb.tt +0 -2
- data/lib/generators/pu/core/install/templates/config/initializers/plutonium.rb +1 -8
- data/lib/generators/pu/eject/shell/shell_generator.rb +2 -2
- data/lib/generators/pu/lib/plutonium_generators/concerns/actions.rb +1 -1
- data/lib/generators/pu/lib/plutonium_generators/concerns/logger.rb +1 -1
- data/lib/generators/pu/lib/plutonium_generators/generator.rb +5 -3
- data/lib/generators/pu/lib/plutonium_generators/model_generator_base.rb +26 -2
- data/lib/generators/pu/pkg/{feature/feature_generator.rb → package/package_generator.rb} +4 -4
- data/lib/generators/pu/pkg/{feature → package}/templates/app/controllers/resource_controller.rb.tt +0 -2
- data/lib/generators/pu/pkg/package/templates/app/definitions/resource_definition.rb.tt +4 -0
- data/lib/generators/pu/pkg/package/templates/app/query_objects/resource_query_object.rb.tt +4 -0
- data/lib/generators/pu/pkg/{app/app_generator.rb → portal/portal_generator.rb} +10 -8
- data/lib/generators/pu/pkg/{app → portal}/templates/app/controllers/concerns/controller.rb.tt +3 -7
- data/lib/generators/pu/pkg/{app → portal}/templates/app/controllers/dashboard_controller.rb.tt +1 -1
- data/lib/generators/pu/pkg/portal/templates/app/controllers/plutonium_controller.rb.tt +5 -0
- data/lib/generators/pu/pkg/{app/templates/app/controllers/controller.rb.tt → portal/templates/app/controllers/resource_controller.rb.tt} +1 -1
- data/lib/generators/pu/pkg/portal/templates/app/definitions/resource_definition.rb.tt +4 -0
- data/lib/generators/pu/pkg/{app → portal}/templates/app/views/package/dashboard/index.html.erb +2 -1
- data/lib/generators/pu/res/conn/conn_generator.rb +78 -3
- data/lib/generators/pu/res/conn/templates/app/controllers/resource_controller.rb.tt +1 -1
- data/lib/generators/pu/res/conn/templates/app/definitions/resource_definition.rb.tt +3 -0
- data/lib/generators/pu/res/conn/templates/app/policies/resource_policy.rb.tt +29 -1
- data/lib/generators/pu/res/conn/templates/app/presenters/resource_presenter.rb.tt +1 -1
- data/lib/generators/pu/res/conn/templates/app/query_objects/resource_query_object.rb.tt +1 -1
- data/lib/generators/pu/res/model/model_generator.rb +0 -7
- data/lib/generators/pu/res/model/templates/model.rb.tt +4 -1
- data/lib/generators/pu/res/scaffold/scaffold_generator.rb +22 -4
- data/lib/generators/pu/res/scaffold/templates/controller.rb.tt +0 -1
- data/lib/generators/pu/res/scaffold/templates/definition.rb.tt +4 -0
- data/lib/generators/pu/res/scaffold/templates/policy.rb.tt +2 -2
- data/lib/generators/pu/rodauth/templates/app/controllers/rodauth_controller.rb.tt +1 -1
- data/lib/generators/pu/rodauth/templates/app/rodauth/account_rodauth_plugin.rb.tt +270 -0
- data/lib/plutonium/action/README.md +0 -0
- data/lib/plutonium/action/base.rb +103 -0
- data/lib/plutonium/action/interactive.rb +117 -0
- data/lib/plutonium/action/route_options.rb +65 -0
- data/lib/plutonium/action/simple.rb +8 -0
- data/lib/plutonium/auth.rb +1 -1
- data/lib/plutonium/configuration.rb +0 -8
- data/lib/plutonium/core/actions/collection.rb +1 -1
- data/lib/plutonium/core/associations/renderers/factory.rb +3 -1
- data/lib/plutonium/core/controller.rb +110 -0
- data/lib/plutonium/core/controllers/authorizable.rb +12 -35
- data/lib/plutonium/core/controllers/bootable.rb +38 -7
- data/lib/plutonium/core/controllers/entity_scoping.rb +6 -2
- data/lib/plutonium/core/fields/renderers/association_renderer.rb +1 -1
- data/lib/plutonium/core/ui/collection.rb +1 -1
- data/lib/plutonium/core/ui/detail.rb +1 -1
- data/lib/plutonium/core/ui/form.rb +1 -1
- data/lib/plutonium/definition/actions.rb +50 -0
- data/lib/plutonium/definition/base.rb +92 -0
- data/lib/plutonium/definition/config_attr.rb +30 -0
- data/lib/plutonium/definition/defineable_props.rb +96 -0
- data/lib/plutonium/definition/search.rb +21 -0
- data/lib/plutonium/engine/validator.rb +30 -0
- data/lib/plutonium/engine.rb +25 -0
- data/lib/plutonium/helpers/form_helper.rb +1 -3
- data/lib/plutonium/interaction/README.md +369 -0
- data/lib/plutonium/interaction/base.rb +75 -0
- data/lib/plutonium/interaction/concerns/presentable.rb +61 -0
- data/lib/plutonium/interaction/concerns/workflow_dsl.rb +82 -0
- data/lib/plutonium/interaction/outcome.rb +129 -0
- data/lib/plutonium/interaction/response/base.rb +63 -0
- data/lib/plutonium/interaction/response/null.rb +33 -0
- data/lib/plutonium/interaction/response/redirect.rb +30 -0
- data/lib/plutonium/interaction/response/render.rb +28 -0
- data/lib/plutonium/lib/bit_flags.rb +70 -9
- data/lib/plutonium/{config → lib}/overlayed_hash.rb +1 -1
- data/lib/plutonium/lib/smart_cache.rb +171 -0
- data/lib/plutonium/models/has_cents.rb +170 -0
- data/lib/plutonium/{pkg/base.rb → package/engine.rb} +10 -2
- data/lib/plutonium/{application → portal}/controller.rb +3 -11
- data/lib/plutonium/{application → portal}/dynamic_controllers.rb +4 -4
- data/lib/plutonium/portal/engine.rb +15 -0
- data/lib/plutonium/railtie.rb +33 -1
- data/lib/plutonium/reloader.rb +5 -5
- data/lib/plutonium/resource/controller.rb +51 -34
- data/lib/plutonium/resource/controllers/authorizable.rb +128 -0
- data/lib/plutonium/{core → resource}/controllers/crud_actions.rb +23 -22
- data/lib/plutonium/resource/controllers/defineable.rb +26 -0
- data/lib/plutonium/{core → resource}/controllers/interactive_actions.rb +12 -12
- data/lib/plutonium/resource/controllers/presentable.rb +41 -0
- data/lib/plutonium/resource/controllers/queryable.rb +44 -0
- data/lib/plutonium/resource/definition.rb +6 -0
- data/lib/plutonium/resource/policy.rb +25 -13
- data/lib/plutonium/resource/query_object.rb +50 -51
- data/lib/plutonium/resource/record.rb +6 -89
- data/lib/plutonium/resource/register.rb +82 -0
- data/lib/plutonium/routing/mapper_extensions.rb +1 -1
- data/lib/plutonium/routing/resource_registration.rb +1 -1
- data/lib/plutonium/routing/route_set_extensions.rb +6 -18
- data/lib/plutonium/ui/action_button.rb +125 -0
- data/lib/plutonium/ui/breadcrumbs.rb +163 -0
- data/lib/plutonium/ui/component/base.rb +13 -0
- data/lib/plutonium/ui/component/behaviour.rb +38 -0
- data/lib/plutonium/ui/component/kit.rb +31 -0
- data/lib/plutonium/ui/component/methods.rb +54 -0
- data/lib/plutonium/ui/display/base.rb +25 -0
- data/lib/plutonium/ui/display/component/association.rb +26 -0
- data/lib/plutonium/ui/display/resource.rb +77 -0
- data/lib/plutonium/ui/display/theme.rb +27 -0
- data/lib/plutonium/ui/dyna_frame/content.rb +20 -0
- data/lib/plutonium/ui/empty_card.rb +20 -0
- data/lib/plutonium/ui/form/base.rb +37 -0
- data/lib/plutonium/ui/form/resource.rb +75 -0
- data/lib/plutonium/ui/form/theme.rb +42 -0
- data/lib/plutonium/ui/page/base.rb +112 -0
- data/lib/plutonium/ui/page/edit.rb +23 -0
- data/lib/plutonium/ui/page/index.rb +27 -0
- data/lib/plutonium/ui/page/new.rb +23 -0
- data/lib/plutonium/ui/page/show.rb +27 -0
- data/lib/plutonium/ui/page_header.rb +49 -0
- data/lib/plutonium/ui/table/base.rb +13 -0
- data/lib/plutonium/ui/table/components/pagy_info.rb +70 -0
- data/lib/plutonium/ui/table/components/pagy_page_info.rb +70 -0
- data/lib/plutonium/ui/table/components/pagy_pagination.rb +105 -0
- data/lib/plutonium/ui/table/components/scopes_bar.rb +136 -0
- data/lib/plutonium/ui/table/components/search_bar.rb +158 -0
- data/lib/plutonium/ui/table/display_theme.rb +21 -0
- data/lib/plutonium/ui/table/resource.rb +98 -0
- data/lib/plutonium/ui/table/theme.rb +35 -0
- data/lib/plutonium/ui.rb +9 -0
- data/lib/plutonium/version.rb +5 -1
- data/lib/plutonium.rb +14 -1
- data/package-lock.json +19 -22
- data/package.json +4 -4
- data/src/css/plutonium.css +15 -0
- data/tailwind.options.js +11 -3
- metadata +218 -81
- data/lib/generators/pu/core/install/templates/app/presenters/resource_presenter.rb.tt +0 -2
- data/lib/generators/pu/core/install/templates/app/query_objects/resource_query_object.rb.tt +0 -2
- data/lib/generators/pu/pkg/feature/templates/app/query_objects/resource_query_object.rb.tt +0 -4
- data/lib/plutonium/concerns/resource_validatable.rb +0 -34
- data/lib/plutonium/config.rb +0 -9
- data/lib/plutonium/core/controllers/base.rb +0 -101
- data/lib/plutonium/core/controllers/presentable.rb +0 -65
- data/lib/plutonium/core/controllers/queryable.rb +0 -28
- data/lib/plutonium/pkg/app.rb +0 -35
- data/lib/plutonium/pkg/concerns/resource_validatable.rb +0 -36
- data/lib/plutonium/pkg/feature.rb +0 -18
- data/lib/plutonium/policy/initializer.rb +0 -22
- data/lib/plutonium/policy/scope.rb +0 -19
- data/lib/plutonium/pundit/context.rb +0 -18
- data/lib/plutonium/pundit/policy_finder.rb +0 -25
- data/lib/plutonium/resource/policy_context.rb +0 -5
- data/lib/plutonium/resource_register.rb +0 -83
- data/lib/plutonium/smart_cache.rb +0 -151
- /data/app/views/{application → plutonium}/_flash.html.erb +0 -0
- /data/app/views/{application → plutonium}/_flash_alerts.html.erb +0 -0
- /data/app/views/{application → plutonium}/_flash_toasts.html.erb +0 -0
- /data/lib/generators/pu/pkg/{app/templates/app/views/package → package/templates}/.keep +0 -0
- /data/lib/generators/pu/pkg/{feature → package}/templates/app/interactions/resource_interaction.rb.tt +0 -0
- /data/lib/generators/pu/pkg/{feature → package}/templates/app/models/resource_record.rb.tt +0 -0
- /data/lib/generators/pu/pkg/{feature → package}/templates/app/policies/resource_policy.rb.tt +0 -0
- /data/lib/generators/pu/pkg/{feature → package}/templates/app/presenters/resource_presenter.rb.tt +0 -0
- /data/lib/generators/pu/pkg/{feature → package}/templates/lib/engine.rb.tt +0 -0
- /data/lib/generators/pu/pkg/{app → portal}/templates/app/policies/resource_policy.rb.tt +0 -0
- /data/lib/generators/pu/pkg/{app → portal}/templates/app/presenters/resource_presenter.rb.tt +0 -0
- /data/lib/generators/pu/pkg/{app → portal}/templates/app/query_objects/resource_query_object.rb.tt +0 -0
- /data/lib/generators/pu/pkg/{feature/templates → portal/templates/app/views/package}/.keep +0 -0
- /data/lib/generators/pu/pkg/{app → portal}/templates/config/routes.rb.tt +0 -0
- /data/lib/generators/pu/pkg/{app → portal}/templates/lib/engine.rb.tt +0 -0
@@ -0,0 +1,369 @@
|
|
1
|
+
# Interactions
|
2
|
+
|
3
|
+
## Table of Contents
|
4
|
+
|
5
|
+
1. [Introduction](#introduction)
|
6
|
+
2. [Key Concepts](#key-concepts)
|
7
|
+
3. [Core Components](#core-components)
|
8
|
+
4. [Setup](#setup)
|
9
|
+
5. [Usage](#usage)
|
10
|
+
6. [Best Practices](#best-practices)
|
11
|
+
7. [Testing](#testing)
|
12
|
+
8. [Advanced Features](#advanced-features)
|
13
|
+
9. [Examples](#examples)
|
14
|
+
|
15
|
+
## Introduction
|
16
|
+
|
17
|
+
Interactions allows us to leverage an architectural approach that focuses on organizing code around business actions or user interactions.
|
18
|
+
It builds upon the traditional MVC pattern by introducing additional layers that encapsulate business logic and improve separation of concerns.
|
19
|
+
|
20
|
+
### Key Benefits
|
21
|
+
|
22
|
+
- Clear separation of business logic from controllers
|
23
|
+
- Improved testability of business operations
|
24
|
+
- Consistent handling of success and failure cases
|
25
|
+
- Flexible and expressive way to chain operations
|
26
|
+
- Enhanced maintainability and readability of complex business processes
|
27
|
+
- Improved code organization and discoverability of business logic
|
28
|
+
|
29
|
+
## Key Concepts
|
30
|
+
|
31
|
+
### Interactions
|
32
|
+
|
33
|
+
Interactions are the core of this pattern. They represent specific use cases or business operations in your application. Each interaction is responsible for a single, well-defined task.
|
34
|
+
Interactions encapsulate the business logic, input validation, and outcome handling, providing a clean interface between the controller and the application's core functionality.
|
35
|
+
|
36
|
+
### Outcomes
|
37
|
+
|
38
|
+
Outcomes encapsulate the result of an interaction, providing a consistent interface for handling both success and failure scenarios. Outcomes can have an associated response, which can be set explicitly using the `with_response` method. The value of the outcome is separate from its response, allowing for more flexible handling of interaction results.
|
39
|
+
<!-- ### Workflows
|
40
|
+
|
41
|
+
Workflows allow you to compose multiple interactions into a larger, more complex business process while maintaining separation of concerns. -->
|
42
|
+
## Core Components
|
43
|
+
|
44
|
+
### Plutonium::Interaction::Base
|
45
|
+
|
46
|
+
The foundation for all interactions. It integrates with ActiveModel for attribute definition and validations.
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
class MyInteraction < Plutonium::Interaction::Base
|
50
|
+
attribute :some_input, :string
|
51
|
+
validates :some_input, presence: true
|
52
|
+
|
53
|
+
private
|
54
|
+
|
55
|
+
def execute
|
56
|
+
# Implementation
|
57
|
+
end
|
58
|
+
end
|
59
|
+
```
|
60
|
+
|
61
|
+
### Plutonium::Interaction::Outcome
|
62
|
+
|
63
|
+
Encapsulates the result of an interaction. It has two subclasses:
|
64
|
+
|
65
|
+
- `Success`: Represents a successful operation
|
66
|
+
- `Failure`: Represents a failed operation
|
67
|
+
|
68
|
+
### Plutonium::Interaction::Response
|
69
|
+
|
70
|
+
Represents controller operations that can be performed as a result of a successful interaction.
|
71
|
+
We ship with these out of the box:
|
72
|
+
|
73
|
+
- `Plutonium::Interaction::Response::Redirect`
|
74
|
+
- `Plutonium::Interaction::Response::Render`
|
75
|
+
- `Plutonium::Interaction::Response::Null`
|
76
|
+
|
77
|
+
## Usage
|
78
|
+
|
79
|
+
### Creating an Interaction
|
80
|
+
|
81
|
+
1. Create a new file in `app/interactions/`, e.g., `app/interactions/users/create_user.rb`:
|
82
|
+
|
83
|
+
```ruby
|
84
|
+
module Users
|
85
|
+
class CreateUser < Plutonium::Interaction::Base
|
86
|
+
attribute :first_name, :string
|
87
|
+
attribute :last_name, :string
|
88
|
+
attribute :email, :string
|
89
|
+
|
90
|
+
validates :first_name, :last_name, presence: true
|
91
|
+
validates :email, presence: true, format: { with: URI::MailTo::EMAIL_REGEXP }
|
92
|
+
|
93
|
+
private
|
94
|
+
|
95
|
+
def execute
|
96
|
+
user = User.new(attributes)
|
97
|
+
if user.save
|
98
|
+
success(user)
|
99
|
+
.with_response(Response::Redirect.new(user_path(user)))
|
100
|
+
.with_message("User was successfully created.")
|
101
|
+
else
|
102
|
+
failure(user.errors)
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
```
|
108
|
+
|
109
|
+
### Using an Interaction in a Controller
|
110
|
+
|
111
|
+
```ruby
|
112
|
+
class UsersController < ApplicationController
|
113
|
+
def create
|
114
|
+
process_outcome(Users::CreateUser.call(user_params))
|
115
|
+
end
|
116
|
+
|
117
|
+
private
|
118
|
+
|
119
|
+
def user_params
|
120
|
+
params.require(:user).permit(:first_name, :last_name, :email)
|
121
|
+
end
|
122
|
+
end
|
123
|
+
```
|
124
|
+
|
125
|
+
### Processing Outcomes
|
126
|
+
|
127
|
+
In your `ApplicationController`:
|
128
|
+
|
129
|
+
```ruby
|
130
|
+
class ApplicationController < ActionController::Base
|
131
|
+
private
|
132
|
+
|
133
|
+
def process_outcome(outcome)
|
134
|
+
if outcome.success?
|
135
|
+
outcome.to_response.process(self) do |value|
|
136
|
+
# Default response.
|
137
|
+
# Executed if the interaction does not produce a specific Response.
|
138
|
+
render json: value
|
139
|
+
end
|
140
|
+
else
|
141
|
+
outcome.messages.each { |msg, type| flash.now[type] = msg }
|
142
|
+
render json: { errors: outcome.errors }, status: :unprocessable_entity
|
143
|
+
end
|
144
|
+
end
|
145
|
+
end
|
146
|
+
```
|
147
|
+
|
148
|
+
### Executing an Interaction
|
149
|
+
|
150
|
+
Interactions can be executed using the call class method:
|
151
|
+
|
152
|
+
```ruby
|
153
|
+
outcome = MyInteraction.call(some_input: "value")
|
154
|
+
|
155
|
+
if outcome.success?
|
156
|
+
# Handle success case
|
157
|
+
else
|
158
|
+
# Handle failure case
|
159
|
+
end
|
160
|
+
```
|
161
|
+
|
162
|
+
Or within another interaction:
|
163
|
+
|
164
|
+
```ruby
|
165
|
+
def execute
|
166
|
+
MyInteraction.call(some_input: "value")
|
167
|
+
.and_then { |result| do_something_with(result) }
|
168
|
+
.with_response(Response::Redirect.new(some_path(final_result)))
|
169
|
+
.with_message("Operation completed successfully")
|
170
|
+
end
|
171
|
+
```
|
172
|
+
|
173
|
+
## Best Practices
|
174
|
+
|
175
|
+
1. Keep interactions focused on a single responsibility
|
176
|
+
2. Use meaningful names for interactions that describe the action being performed
|
177
|
+
3. Leverage the `and_then` method for clean and expressive operation chaining
|
178
|
+
4. Prefer small, composable interactions over large, monolithic ones
|
179
|
+
5. Use `with_response` to explicitly set the desired response type
|
180
|
+
6. Keep the interaction's core logic separate from response handling
|
181
|
+
|
182
|
+
## Testing
|
183
|
+
|
184
|
+
Interactions are easy to test in isolation. Here's an example using RSpec:
|
185
|
+
|
186
|
+
```ruby
|
187
|
+
RSpec.describe Users::CreateUser do
|
188
|
+
let(:valid_attributes) { { first_name: "John", last_name: "Doe", email: "john@example.com" } }
|
189
|
+
|
190
|
+
it "creates a user successfully" do
|
191
|
+
outcome = described_class.call(valid_attributes)
|
192
|
+
|
193
|
+
expect(outcome).to be_success
|
194
|
+
expect(outcome.value).to be_a(User)
|
195
|
+
expect(outcome.to_response).to be_a(Response::Redirect)
|
196
|
+
expect(User.last.email).to eq("john@example.com")
|
197
|
+
end
|
198
|
+
|
199
|
+
it "fails with invalid attributes" do
|
200
|
+
outcome = described_class.call(first_name: "", last_name: "Doe", email: "invalid")
|
201
|
+
|
202
|
+
expect(outcome).to be_failure
|
203
|
+
expect(outcome.errors).to include("First name can't be blank")
|
204
|
+
expect(outcome.errors).to include("Email is invalid")
|
205
|
+
end
|
206
|
+
end
|
207
|
+
```
|
208
|
+
|
209
|
+
## Advanced Features
|
210
|
+
|
211
|
+
<!--
|
212
|
+
### Workflows
|
213
|
+
|
214
|
+
Workflows allow you to compose multiple interactions into a larger business process:
|
215
|
+
|
216
|
+
```ruby
|
217
|
+
module Orders
|
218
|
+
class PlaceOrder < Plutonium::Interaction::Base
|
219
|
+
presents label: "Place Order",
|
220
|
+
icon: "shopping-cart",
|
221
|
+
description: "Process a new order"
|
222
|
+
|
223
|
+
attribute :user_id, :integer
|
224
|
+
attribute :product_ids, :string
|
225
|
+
attribute :payment_method, :string
|
226
|
+
|
227
|
+
validates :user_id, :product_ids, :payment_method, presence: true
|
228
|
+
|
229
|
+
workflow do
|
230
|
+
step :validate_products, ValidateProducts
|
231
|
+
step :check_inventory, CheckInventory
|
232
|
+
step :process_payment, ProcessPayment, if: ->(ctx) { ctx[:total_price] > 0 }
|
233
|
+
step :create_order, CreateOrder
|
234
|
+
step :send_confirmation, SendOrderConfirmation
|
235
|
+
end
|
236
|
+
|
237
|
+
private
|
238
|
+
|
239
|
+
def execute
|
240
|
+
execute_workflow(attributes.to_h)
|
241
|
+
.map { |ctx| Response::Redirect.new(order_path(ctx[:order])) }
|
242
|
+
.with_message("Order placed successfully.")
|
243
|
+
end
|
244
|
+
end
|
245
|
+
end
|
246
|
+
```
|
247
|
+
-->
|
248
|
+
|
249
|
+
### Presentable Concern
|
250
|
+
|
251
|
+
The `Presentable` concern allows you to add metadata to your interactions, which can be used for generating UI components or documentation:
|
252
|
+
|
253
|
+
```ruby
|
254
|
+
class MyInteraction < Plutonium::Interaction::Base
|
255
|
+
include Plutonium::Interaction::Concerns::Presentable
|
256
|
+
|
257
|
+
presents label: "My Interaction",
|
258
|
+
icon: "star",
|
259
|
+
description: "Does something awesome"
|
260
|
+
|
261
|
+
# ... rest of the interaction
|
262
|
+
end
|
263
|
+
```
|
264
|
+
|
265
|
+
## Examples
|
266
|
+
|
267
|
+
### Chaining Operations
|
268
|
+
|
269
|
+
```ruby
|
270
|
+
module Orders
|
271
|
+
class PlaceOrder < Plutonium::Interaction::Base
|
272
|
+
attribute :user_id, :integer
|
273
|
+
attribute :product_ids, :string
|
274
|
+
|
275
|
+
private
|
276
|
+
|
277
|
+
def execute
|
278
|
+
success(attributes)
|
279
|
+
.and_then { |attrs| find_user(attrs[:user_id]) }
|
280
|
+
.and_then { |user| find_products(user, attributes[:product_ids]) }
|
281
|
+
.and_then { |user, products| create_order(user, products) }
|
282
|
+
.with_response(Response::Redirect.new(order_path(order)))
|
283
|
+
.with_message("Order placed successfully.")
|
284
|
+
end
|
285
|
+
|
286
|
+
def find_user(user_id)
|
287
|
+
user = User.find_by(id: user_id)
|
288
|
+
user ? success(user) : failure(["User not found"])
|
289
|
+
end
|
290
|
+
|
291
|
+
def find_products(user, product_ids)
|
292
|
+
products = Product.where(id: product_ids.split(','))
|
293
|
+
products.empty? ? failure(["No valid products found"]) : success([user, products])
|
294
|
+
end
|
295
|
+
|
296
|
+
def create_order(user, products)
|
297
|
+
order = Order.create(user: user, products: products)
|
298
|
+
order.persisted? ? success(order) : failure(order.errors.full_messages)
|
299
|
+
end
|
300
|
+
end
|
301
|
+
end
|
302
|
+
```
|
303
|
+
|
304
|
+
This example demonstrates how to chain multiple operations, handle potential failures at each step, and return an appropriate outcome with a specific response type. Note how the `with_response` and `with_message` methods are used to set the response and add a message to the outcome.
|
305
|
+
|
306
|
+
By following these guidelines and examples, you can effectively implement and use the Interaction pattern in your Rails applications, leading to more maintainable and testable code.
|
307
|
+
|
308
|
+
<!--
|
309
|
+
|
310
|
+
This example demonstrates how to chain multiple operations, handle potential failures at each step, and return an appropriate outcome.
|
311
|
+
|
312
|
+
By following these guidelines and examples, you can effectively implement and use the Use Case Driven Design pattern in your Rails applications, leading to more maintainable and testable code.
|
313
|
+
|
314
|
+
|
315
|
+
### Example interaction with workflow
|
316
|
+
|
317
|
+
```ruby
|
318
|
+
module Orders
|
319
|
+
class PlaceOrder < Plutonium::Interaction::Base
|
320
|
+
presents label: "Place Order",
|
321
|
+
icon: "shopping-cart",
|
322
|
+
description: "Process a new order",
|
323
|
+
category: "Order Management"
|
324
|
+
|
325
|
+
attribute :user_id, :integer
|
326
|
+
attribute :product_ids, :string
|
327
|
+
attribute :payment_method, :string
|
328
|
+
|
329
|
+
validates :user_id, :product_ids, :payment_method, presence: true
|
330
|
+
|
331
|
+
workflow do
|
332
|
+
step :validate_products, use_case: ValidateProducts
|
333
|
+
step :check_inventory, use_case: CheckInventory
|
334
|
+
step :process_payment, use_case: ProcessPayment, if: ->(ctx) { ctx[:total_price] > 0 }
|
335
|
+
step :create_order, use_case: CreateOrder
|
336
|
+
step :send_confirmation, use_case: SendOrderConfirmation
|
337
|
+
end
|
338
|
+
|
339
|
+
private
|
340
|
+
|
341
|
+
def execute
|
342
|
+
execute_workflow(attributes.to_h)
|
343
|
+
.map { |ctx| Actions::RedirectAction.new(:order_path, id: ctx[:order].id) }
|
344
|
+
.with_message("Order placed successfully.")
|
345
|
+
end
|
346
|
+
end
|
347
|
+
|
348
|
+
class ValidateProducts < Plutonium::Interaction::Base
|
349
|
+
# Implementation...
|
350
|
+
end
|
351
|
+
|
352
|
+
class CheckInventory < Plutonium::Interaction::Base
|
353
|
+
# Implementation...
|
354
|
+
end
|
355
|
+
|
356
|
+
class ProcessPayment < Plutonium::Interaction::Base
|
357
|
+
# Implementation...
|
358
|
+
end
|
359
|
+
|
360
|
+
class CreateOrder < Plutonium::Interaction::Base
|
361
|
+
# Implementation...
|
362
|
+
end
|
363
|
+
|
364
|
+
class SendOrderConfirmation < Plutonium::Interaction::Base
|
365
|
+
# Implementation...
|
366
|
+
end
|
367
|
+
end
|
368
|
+
```
|
369
|
+
-->
|
@@ -0,0 +1,75 @@
|
|
1
|
+
require "active_model"
|
2
|
+
|
3
|
+
module Plutonium
|
4
|
+
module Interaction
|
5
|
+
# Base class for all interactions.
|
6
|
+
# Provides core functionality for validations, execution, and result handling.
|
7
|
+
#
|
8
|
+
# @example
|
9
|
+
# class MyInteraction < Plutonium::Interaction::Base
|
10
|
+
# attribute :user_id, :integer
|
11
|
+
# validates :user_id, presence: true
|
12
|
+
#
|
13
|
+
# private
|
14
|
+
#
|
15
|
+
# def execute
|
16
|
+
# user = User.find(user_id)
|
17
|
+
# success(user)
|
18
|
+
# end
|
19
|
+
# end
|
20
|
+
#
|
21
|
+
# @note Subclasses must implement the #execute method.
|
22
|
+
class Base
|
23
|
+
include ActiveModel::Model
|
24
|
+
include ActiveModel::Attributes
|
25
|
+
# include Concerns::Presentable
|
26
|
+
# include Concerns::WorkflowDSL
|
27
|
+
|
28
|
+
# Executes the interaction with the given arguments.
|
29
|
+
#
|
30
|
+
# @param args [Hash] The arguments to initialize the interaction.
|
31
|
+
# @return [Plutonium::Interaction::Outcome] The result of the interaction.
|
32
|
+
def self.call(**args)
|
33
|
+
new(**args).call
|
34
|
+
end
|
35
|
+
|
36
|
+
# Executes the interaction.
|
37
|
+
#
|
38
|
+
# @return [Plutonium::Interaction::Outcome] The result of the interaction.
|
39
|
+
def call
|
40
|
+
if valid?
|
41
|
+
execute
|
42
|
+
else
|
43
|
+
failure(errors)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
private
|
48
|
+
|
49
|
+
# Implement the main logic of the interaction.
|
50
|
+
#
|
51
|
+
# @abstract Subclass and override {#execute} to implement the interaction logic.
|
52
|
+
# @return [Plutonium::Interaction::Outcome] The result of the interaction.
|
53
|
+
# @raise [NotImplementedError] If the subclass doesn't implement this method.
|
54
|
+
def execute
|
55
|
+
raise NotImplementedError, "#{self.class} must implement #execute"
|
56
|
+
end
|
57
|
+
|
58
|
+
# Creates a successful outcome.
|
59
|
+
#
|
60
|
+
# @param value [Object] The value to be wrapped in the successful outcome.
|
61
|
+
# @return [Plutonium::Interaction::Success] A successful outcome.
|
62
|
+
def success(value)
|
63
|
+
Success.new(value)
|
64
|
+
end
|
65
|
+
|
66
|
+
# Creates a failure outcome.
|
67
|
+
#
|
68
|
+
# @param errors [ActiveModel::Errors, Array<String>] The errors to be wrapped in the failure outcome.
|
69
|
+
# @return [Plutonium::Interaction::Failure] A failure outcome.
|
70
|
+
def failure(errors)
|
71
|
+
Failure.new(errors)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
module Plutonium
|
2
|
+
module Interaction
|
3
|
+
module Concerns
|
4
|
+
# Provides presentation-related functionality for interactions.
|
5
|
+
#
|
6
|
+
# This module allows interactions to define metadata such as labels, icons,
|
7
|
+
# and descriptions, which can be used for UI generation or documentation.
|
8
|
+
#
|
9
|
+
# @example
|
10
|
+
# class MyInteraction < Plutonium::Interaction::Base
|
11
|
+
# include Plutonium::Interaction::Concerns::Presentable
|
12
|
+
#
|
13
|
+
# presents label: "My Interaction",
|
14
|
+
# icon: "star",
|
15
|
+
# description: "Does something awesome"
|
16
|
+
#
|
17
|
+
# # ... rest of the interaction
|
18
|
+
# end
|
19
|
+
module Presentable
|
20
|
+
extend ActiveSupport::Concern
|
21
|
+
|
22
|
+
included do
|
23
|
+
class_attribute :presentation_metadata, default: {}
|
24
|
+
end
|
25
|
+
|
26
|
+
class_methods do
|
27
|
+
# Defines presentation metadata for the interaction.
|
28
|
+
#
|
29
|
+
# @param options [Hash] The presentation options.
|
30
|
+
# @option options [String] :label The label for the interaction.
|
31
|
+
# @option options [String] :icon The icon for the interaction.
|
32
|
+
# @option options [String] :description The description of the interaction.
|
33
|
+
def presents(**options)
|
34
|
+
self.presentation_metadata = options
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
# Returns the label for the interaction.
|
39
|
+
#
|
40
|
+
# @return [String] The label defined in the presentation metadata or a default generated from the class name.
|
41
|
+
def label
|
42
|
+
self.class.presentation_metadata[:label] || self.class.name.demodulize.titleize
|
43
|
+
end
|
44
|
+
|
45
|
+
# Returns the icon for the interaction.
|
46
|
+
#
|
47
|
+
# @return [String, nil] The icon defined in the presentation metadata.
|
48
|
+
def icon
|
49
|
+
self.class.presentation_metadata[:icon]
|
50
|
+
end
|
51
|
+
|
52
|
+
# Returns the description for the interaction.
|
53
|
+
#
|
54
|
+
# @return [String, nil] The description defined in the presentation metadata.
|
55
|
+
def description
|
56
|
+
self.class.presentation_metadata[:description]
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
module Plutonium
|
2
|
+
module Interaction
|
3
|
+
module Concerns
|
4
|
+
# DO NOT USE
|
5
|
+
#
|
6
|
+
# Provides a Domain Specific Language (DSL) for defining workflows in interactions.
|
7
|
+
#
|
8
|
+
# This module allows interactions to define a series of steps that can be executed
|
9
|
+
# in sequence, with optional conditions for each step.
|
10
|
+
#
|
11
|
+
# @example
|
12
|
+
# class MyWorkflow < Plutonium::Interaction::Base
|
13
|
+
# include Plutonium::Interaction::Concerns::WorkflowDSL
|
14
|
+
#
|
15
|
+
# workflow do
|
16
|
+
# step :validate_input, ValidateInputInteraction
|
17
|
+
# step :process_data, ProcessDataInteraction, if: ->(ctx) { ctx[:data_valid] }
|
18
|
+
# step :send_notification, SendNotificationInteraction
|
19
|
+
# end
|
20
|
+
#
|
21
|
+
# private
|
22
|
+
#
|
23
|
+
# def execute
|
24
|
+
# execute_workflow(attributes.to_h)
|
25
|
+
# end
|
26
|
+
# end
|
27
|
+
module WorkflowDSL
|
28
|
+
extend ActiveSupport::Concern
|
29
|
+
|
30
|
+
included do
|
31
|
+
class_attribute :workflow_steps, default: []
|
32
|
+
end
|
33
|
+
|
34
|
+
class_methods do
|
35
|
+
# Defines the workflow for the interaction.
|
36
|
+
#
|
37
|
+
# @yield The block where workflow steps are defined.
|
38
|
+
def workflow(&block)
|
39
|
+
WorkflowBuilder.new(self).instance_eval(&block)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
# Helper class for building workflows.
|
44
|
+
class WorkflowBuilder
|
45
|
+
# @param use_case_class [Class] The interaction class where the workflow is being defined.
|
46
|
+
def initialize(use_case_class)
|
47
|
+
@use_case_class = use_case_class
|
48
|
+
end
|
49
|
+
|
50
|
+
# Adds a step to the workflow.
|
51
|
+
#
|
52
|
+
# @param name [Symbol] The name of the step.
|
53
|
+
# @param use_case [Class] The interaction class to be executed for this step.
|
54
|
+
# @param if [Proc, nil] An optional condition for executing the step.
|
55
|
+
def step(name, use_case, if: nil)
|
56
|
+
@use_case_class.workflow_steps << {
|
57
|
+
name:,
|
58
|
+
use_case:,
|
59
|
+
condition: binding.local_variable_get(:if)
|
60
|
+
}
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
# Executes the defined workflow.
|
65
|
+
#
|
66
|
+
# @param context [Hash] The initial context for the workflow.
|
67
|
+
# @return [Plutonium::Interaction::Outcome] The outcome of the last executed step.
|
68
|
+
def execute_workflow(context = {})
|
69
|
+
self.class.workflow_steps.reduce(Success.new(context)) do |result, step|
|
70
|
+
result.and_then do |ctx|
|
71
|
+
if step[:condition].nil? || instance_exec(ctx, &step[:condition])
|
72
|
+
step[:use_case].call(context: ctx).map { |outcome| ctx[step[:name]] = outcome }
|
73
|
+
else
|
74
|
+
Success.new(ctx)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|