rectify 0.4.5 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d078cea0461b65b902da31291260082c39abf97e
4
- data.tar.gz: 725a7ddeb4fcce8395b2b9b101ae4c3ff7ea29a5
3
+ metadata.gz: 9cd2e0e074614674af4e91e02a6024a635c23b9a
4
+ data.tar.gz: 4eac998668975b9a58266c3ff900bf8bd424a75c
5
5
  SHA512:
6
- metadata.gz: 594da9a6e14242b9c637a4bd4946f7f0182ec3983330cbc82ae340a7feab081d471d1cc7467c66152052039c28b690fdb3ba86e87901a5d2cfe5bdf5a247f319
7
- data.tar.gz: bc79820e8625dc68a8dc934331a04127010e93b341b5a6870ec5a3861faf4087e20ae3da940f43f8c3828f12db4e540d0355a2bf9af94468f88c7e25146225fc
6
+ metadata.gz: 627070d61b3225e26466faa7a92786afac970d23a8a0648d325a282cbc2b77fdc5be65dbb6e6d9afc9fa35992ec1f6c3da3188623cede7632de38d1b15deb4de
7
+ data.tar.gz: 6c91917d08347230e38d117f76e820cded4037e5ca971499b6d5438d69770cfb084c257ecf8fc3f173ecd73f5c67d6e61d67cfd781643598eb3adf2610fabb68
@@ -11,6 +11,8 @@ module Rectify
11
11
  model_value = model.public_send(a.name)
12
12
  form.public_send("#{a.name}=", a.value_from(model_value))
13
13
  end
14
+
15
+ form.map_model(model)
14
16
  end
15
17
  end
16
18
 
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?
@@ -1,3 +1,3 @@
1
1
  module Rectify
2
- VERSION = "0.4.5"
2
+ VERSION = "0.5.0"
3
3
  end
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 an ActiveModel to the form to populate it's attribute
286
- values. This is useful when editing a model:
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 by a model. You can use a model to populate the forms attributes but that
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.5
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-14 00:00:00.000000000 Z
11
+ date: 2016-05-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: virtus