Skip to content

Commit 97ae014

Browse files
authored
Merge pull request #35 from hamajyotan/improve-readme
Improve README.md
2 parents 4c75e25 + 23ef988 commit 97ae014

1 file changed

Lines changed: 31 additions & 5 deletions

File tree

README.md

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# ActiveRecordCompose
22

33
ActiveRecordCompose lets you build form objects that combine multiple ActiveRecord models into a single, unified interface.
4-
It makes complex updates - such as user registration forms spanning multiple tables - easier to write, validate, and maintain.
4+
More than just a simple form object, it is designed as a **business-oriented composed model** that encapsulates complex operations-such as user registration spanning multiple tables-making them easier to write, validate, and maintain.
55

66
[![Gem Version](https://badge.fury.io/rb/active_record_compose.svg)](https://badge.fury.io/rb/active_record_compose)
77
![CI](https://github.com/hamajyotan/active_record_compose/workflows/CI/badge.svg)
@@ -37,7 +37,7 @@ This mixes unrelated concerns into one model, leading to unnecessary complexity.
3737

3838
`ActiveModel::Model` helps here — it provides the familiar API (`attribute`, `errors`, validations, callbacks) without persistence, so you can isolate logic per use case.
3939

40-
**ActiveRecordCompose** builds on `ActiveModel::Model` and acts as a first-class model within Rails:
40+
**ActiveRecordCompose** builds on `ActiveModel::Model` and is a powerful **business object** that acts as a first-class model within Rails.
4141
- Transparently accesses attributes across multiple models
4242
- Saves all associated models atomically in a transaction
4343
- Collects and exposes error information consistently
@@ -80,10 +80,10 @@ You can compose them into one form object:
8080

8181
```ruby
8282
class UserRegistration < ActiveRecordCompose::Model
83-
def initialize
83+
def initialize(attributes = {})
8484
@account = Account.new
8585
@profile = @account.build_profile
86-
super()
86+
super(attributes)
8787
models << account << profile
8888
end
8989

@@ -109,6 +109,7 @@ end
109109
Usage:
110110

111111
```ruby
112+
# === Standalone script ===
112113
registration = UserRegistration.new
113114
registration.update!(
114115
name: "foo",
@@ -119,6 +120,25 @@ registration.update!(
119120
email_confirmation: "bar@example.com",
120121
terms_of_service: true,
121122
)
123+
124+
# === Or, in a Rails controller with strong parameters ===
125+
class UserRegistrationsController < ApplicationController
126+
def create
127+
@registration = UserRegistration.new(user_registration_params)
128+
if @registration.save
129+
redirect_to root_path, notice: "Registered!"
130+
else
131+
render :new
132+
end
133+
end
134+
135+
private
136+
def user_registration_params
137+
params.require(:user_registration).permit(
138+
:name, :email, :firstname, :lastname, :age, :email_confirmation, :terms_of_service
139+
)
140+
end
141+
end
122142
```
123143

124144
Both `Account` and `Profile` will be updated **atomically in one transaction**.
@@ -182,6 +202,8 @@ en:
182202
record_invalid: 'Validation failed: %{errors}'
183203
```
184204
205+
For more complete usage patterns, see the [Sample Application](#sample-application) below.
206+
185207
## Advanced Usage
186208
187209
### Destroy Option
@@ -265,6 +287,11 @@ Instead, this behavior is documented here so that developers can make an informe
265287

266288
## Sample Application
267289

290+
The sample app demonstrates a more complete usage of ActiveRecordCompose
291+
(e.g., user registration flows involving multiple models).
292+
It is not meant to cover every possible pattern, but can serve as a reference
293+
for putting the library into practice.
294+
268295
Try it out in your browser with GitHub Codespaces (or locally):
269296

270297
- https://github.com/hamajyotan/active_record_compose-example
@@ -273,7 +300,6 @@ Try it out in your browser with GitHub Codespaces (or locally):
273300

274301
- [API Documentation (YARD)](https://hamajyotan.github.io/active_record_compose/)
275302
- [Blog article introducing the concept](https://dev.to/hamajyotan/smart-way-to-update-multiple-models-simultaneously-in-rails-51b6)
276-
- [Sample Application](https://github.com/hamajyotan/active_record_compose-example)
277303

278304
## Development
279305

0 commit comments

Comments
 (0)