rectify 0.4.5 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/rectify/build_form_from_model.rb +2 -0
- data/lib/rectify/form.rb +6 -0
- data/lib/rectify/version.rb +1 -1
- data/readme.md +26 -4
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9cd2e0e074614674af4e91e02a6024a635c23b9a
|
4
|
+
data.tar.gz: 4eac998668975b9a58266c3ff900bf8bd424a75c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 627070d61b3225e26466faa7a92786afac970d23a8a0648d325a282cbc2b77fdc5be65dbb6e6d9afc9fa35992ec1f6c3da3188623cede7632de38d1b15deb4de
|
7
|
+
data.tar.gz: 6c91917d08347230e38d117f76e820cded4037e5ca971499b6d5438d69770cfb084c257ecf8fc3f173ecd73f5c67d6e61d67cfd781643598eb3adf2610fabb68
|
data/lib/rectify/form.rb
CHANGED
@@ -70,6 +70,12 @@ module Rectify
|
|
70
70
|
super.except(:id)
|
71
71
|
end
|
72
72
|
|
73
|
+
def map_model(model)
|
74
|
+
# Implement this in your form object for custom mapping from model to form
|
75
|
+
# object as part of the `.from_model` call after matching attributes are
|
76
|
+
# populated (optional).
|
77
|
+
end
|
78
|
+
|
73
79
|
private
|
74
80
|
|
75
81
|
def form_attributes_valid?
|
data/lib/rectify/version.rb
CHANGED
data/readme.md
CHANGED
@@ -282,8 +282,9 @@ form.ip_address # => "1.2.3.4"
|
|
282
282
|
|
283
283
|
**Model**
|
284
284
|
|
285
|
-
The final way is to pass
|
286
|
-
values. This is useful
|
285
|
+
The final way is to pass a Ruby object instance (which is normally an ActiveModel
|
286
|
+
but can be any PORO) to the form to populate it's attribute values. This is useful
|
287
|
+
when editing a model:
|
287
288
|
|
288
289
|
```ruby
|
289
290
|
user = User.create(:first_name => "Andy", :last_name => "Pike")
|
@@ -295,8 +296,29 @@ form.first_name # => "Andy"
|
|
295
296
|
form.last_name # => "Pike"
|
296
297
|
```
|
297
298
|
|
299
|
+
This works by trying to match (deeply) the attributes of the form object with the
|
300
|
+
passed in object. If there is matching attribute or method in the model, then
|
301
|
+
whatever it returns will be assigned to the form attribute.
|
302
|
+
|
303
|
+
This works great for most cases, but sometimes you need more control and need the
|
304
|
+
ability to do custom mapping from the model to the form. When this is required,
|
305
|
+
you just need to implement the `#map_model` method in your form object:
|
306
|
+
|
307
|
+
```ruby
|
308
|
+
class UserForm < Rectify::Form
|
309
|
+
attribute :full_name, String
|
310
|
+
|
311
|
+
def map_model(model)
|
312
|
+
self.full_name = "#{model.first_name} #{model.last_name}"
|
313
|
+
end
|
314
|
+
end
|
315
|
+
```
|
316
|
+
|
317
|
+
The `#map_model` method is called as part of `.from_model` after all the automatic
|
318
|
+
attribute assignment is complete.
|
319
|
+
|
298
320
|
One important thing that is different about Rectify forms is that they are not
|
299
|
-
bound
|
321
|
+
bound to a model. You can use a model to populate the form's attributes but that
|
300
322
|
is all it will do. It does not keep a reference to the model or interact with
|
301
323
|
it.
|
302
324
|
|
@@ -1021,7 +1043,7 @@ using normal(ish) commands from Rails:
|
|
1021
1043
|
```
|
1022
1044
|
rake db:migrate # => Migrates the test database
|
1023
1045
|
rake db:schema # => Dumps database schema
|
1024
|
-
rake g:migration # => Create a new migration file
|
1046
|
+
rake g:migration # => Create a new migration file (use snake_case name)
|
1025
1047
|
```
|
1026
1048
|
|
1027
1049
|
### Releasing a new version
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rectify
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andy Pike
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-05-
|
11
|
+
date: 2016-05-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: virtus
|