Comments (15)
Hi Bryce,
it depends on the context you're working in: reform is an ORM-agnostic framework, so it doesn't know anything about the underlying database. That's why it pushes input data to the attributes using public setters.
In an ActiveRecord
environment, however, model.save
is called for you automatically. Are you working in an AR context?
from reform.
I am working in an ActiveRecord environment, yes. But I don't like the idea of model.save being automatically called; all I want a form object to do is allow me to map my user interface to my domain models. Why should the form decide for me that I want to persist those models to the database?
Take the case where the workflow is
- Get user input
- Map input into three models, and pass those to a service that runs some decisioning logic
- Based on that logic, decide to persist none/some/all of them to our database
- Tell our controller how to respond.
That might be fairly uncommon, but it illustrates why my form shouldn't be calling functions that deal with persistence. Is there another method that ONLY maps the form input to a model, and returns and un-persisted AR model?
from reform.
The basic form without the ActiveRecord
module mixed in does just that. Maybe we shouldn't mix in the ActiveRecord
modules automatically. Have a look at the link I posted in my last comment, it shows what happens on top of the basic form in AR.
from reform.
I understand the link you posted in your last comment. My point is that I question WHY you would do that. You're essentially saying that Reform is now going to try AR model objects and non-AR model objects differently. That doesn't make a ton of sense to me.
If the point of Reform is to undo some of the coupling that accepts_nested_attributes
introduces, why are you introducing additional coupling? To illustrate, say ActiveRecord changes the name of its save function to be called "commit". Now you have to change your gem? That doesn't feel very clean, but it's just my two cents.
from reform.
I see your point. The basic reform gem is totally decoupled from AR, it doesn't know anything about it. Now, when you include the Form::ActiveRecord
module, you explicitly say that want the coupling to AR - it is totally ok to have dependencies in this optional layer.
The only problem is that this "optional" layer is automatically included in Rails :) Should we introduce a switch?
from reform.
I see your point as well. I mean, I can definitely see why someone might want it as a feature; I just personally don't like the idea of my forms handling persistence as well.
My argument would be that if you want to to automatically save to AR in Rails, you should have to optionally specify that. Or at least, there should be an option to prevent it. I'm curious what others think though.
from reform.
We're on the same page here - I hate this feature and introduced it only some days ago as lots of users were confused why reform would not save the underlying model. That's exactly why I have this in an optional module.
The more I think about this the more I agree we should have this configurable.
config.reform.save_model = true
from reform.
How about naming it @form.sync
instead of @form.save
? The confusion is solely based on the name save
. It misleadingly suggests a behavior that's not there.
So in my understanding the usage in the controller would be
if @form.validate(params[:song])
@form.sync
@song.save
end
from reform.
That makes a lot of sense to me. Though for context, I'm new to using reform, so changing the naming convention doesn't really hurt my workflow much. Others might be upset about it. But yeah, I think that name is definitely more accurate of a description of what is happening.
from reform.
This boils down to aliasing #save
to #sync
. We could then document that sync
in combination with the Form::ActiveRecord
feature internally calls save
on the model.
from reform.
@apotonick - Hrm, that's different from where I thought that this was going. My understanding of @thomasritz 's suggestion was that the form support two methods - #save
and #sync
. #save
would call save on the underlying models (assuming either a Rails environment or the inclusion of Form::ActiveRecord
), while #sync
would applying the form values to the underlying models while not calling save.
I see value in that implementation, but that's just my two cents.
from reform.
Since #save
is already part of the public API we'd have to alias it to #sync
anyway to make it consistent.
Following your proposal, #save
internally had to call #sync
and then save
on the model (the latter only in an ActiveRecord/DM/.. environment).
I think an amended README could clarify the "syncing" behaviour of the #save
method. Adding another public method might add more confusion than we already have. What do you think?
from reform.
I added some docs: https://github.com/apotonick/reform#saving-forms
from reform.
All at sudden I got it! You wanna sync but not auto-save your data in an AR environment, right?
from reform.
Yes, that's exactly it! Syncing would just be coercing the form data into models without trying to save it to the database. So #save
could remain the documented feature that it is today, while #sync
would have this new behavior.
from reform.
Related Issues (20)
- Out of the box dry-validation not working HOT 1
- Error with dry-validation 1.3 HOT 5
- Reform 2.3 breaks `require 'reform/form/active_model/validations'` HOT 6
- Regression in 2.3 when adding errors in an overridden setter method
- Dynamic form fields and stack level too deep HOT 3
- Validation block option :form incorrectly memoized between tests (in different form instances?) HOT 12
- Skipped property is still passed for validation
- Calling `#name` unexpectedly … HOT 1
- populator adding just one record HOT 3
- Form returns merged errors for `collection` after validation HOT 1
- Using custom validation message adds `Base` to message
- How to validate JSON object where keys are variable and unknown? HOT 3
- Errors of nested collection are not shown. HOT 1
- `self` in default value for properties is broken when Reform 2.6 is with trailblazer-context HOT 5
- Cast strings to nil without nilify. HOT 1
- Updating record with nested ActiveStorage attachments fails HOT 13
- validation blocks with inherit: true option ignore other options HOT 3
- Problem with delegation in forms in 2.6.1 / disposable 0.6.0 HOT 26
- Destroying nested form objects HOT 1
- Using a nested form with populator. HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from reform.