README.md in tramway-0.4.1 vs README.md in tramway-0.4.2
- old
+ new
@@ -132,13 +132,11 @@
*app/forms/user_form.rb
```ruby
class UserForm < Tramway::BaseForm
properties :email, :password, :first_name, :last_name, :phone
- def password=(value)
- object.password = value if value.present?
- end
+ normalizes :email, ->(value) { value.strip.downcase }
end
```
**Controllers without Tramway Form**
@@ -247,40 +245,56 @@
end
end
end
```
+### Normalizes
+
+Tramway Form supports `normalizes` method. It's almost the same [as in Rails](https://edgeapi.rubyonrails.org/classes/ActiveRecord/Normalization.html)
+
+```ruby
+class UserForm < Tramway::BaseForme
+ properties :email, :first_name, :last_name
+
+ normalizes :email, with: ->(value) { value.strip.downcase }
+ normalizes :first_name, :last_name, with: ->(value) { value.strip }
+end
+```
+
+`normalizes` method arguments:
+* `*properties` - collection of properties that will be normalized
+* `with:` - a proc with a normalization
+* `apply_on_nil` - by default is `false`. When `true` Tramway Form applies normalization on `nil` values
+
### Form inheritance
-Tramway Form supports inheritance of `properties`
+Tramway Form supports inheritance of `properties` and `normalizations`
**Example**
```ruby
class UserForm < TramwayForm
properties :email, :password
+
+ normalizes :email, with: ->(value) { value.strip.downcase }
end
class AdminForm < UserForm
properties :permissions
end
AdminForm.properties # returns [:email, :password, :permissions]
+AdminForm.normalizations # contains the normalization of :email
```
### Make flexible and extendable forms
Tramway Form properties are not mapped to a model. You're able to make extended forms.
*app/forms/user_form.rb*
```ruby
class UserForm < Tramway::BaseForm
- properties :email, :password, :full_name
-
- # RULE: in case password is empty, don't save
- def password=(value)
- object.password = value if value.present?
- end
+ properties :email, :full_name
# EXTENDED FIELD: full name
def full_name=(value)
object.first_name = value.split(' ').first
object.last_name = value.split(' ').last