rectify 0.7.1 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0bcd5f022557ed0df7cc716ed09c8b9001c0303d
4
- data.tar.gz: f021048068ca88137eb53b3b023972305f791cb5
3
+ metadata.gz: 2d2a653fa353d25d4303b71cddb5858ad2562355
4
+ data.tar.gz: 783cacad47dd54af9d9adb22afc5399214715443
5
5
  SHA512:
6
- metadata.gz: 18d2d0ea2a75fd8d3b462a26be50fb6c13d0094289d5f21e427d0b9dd3ea4b67317d1e9a976e1b6aec47e0f3eef9d0506243d8e0c82dc920e955022af3241cf5
7
- data.tar.gz: 38dc5dffd208fb1540b607589626711897b18ab6454e89e5f476eae477bbb87fc001c38a053a45c6feb8df88cdcb479371ce8aa5a2aee69c3bce8356c6502f39
6
+ metadata.gz: 7240ab43419d70d321e9000473639e3397752ed5d3dc6b8a589c86af2193b5f17dab94f7f4628f904f174a147f1500104e27f317c9f2ed85ebd408040699e305
7
+ data.tar.gz: b56649c7dcac2d0fbebd93fe78c1f4757afb93f0db3653d7d9143860bf5e2b3e59941b4da004ad09b328fdaeb5feae77183abb463b7ad2f9274ef7d72cd233cc
data/lib/rectify/form.rb CHANGED
@@ -3,6 +3,8 @@ module Rectify
3
3
  include Virtus.model
4
4
  include ActiveModel::Validations
5
5
 
6
+ attr_reader :context
7
+
6
8
  attribute :id, Integer
7
9
 
8
10
  def self.from_params(params, additional_params = {})
@@ -102,24 +104,48 @@ module Rectify
102
104
  # some processing before validation happens (optional).
103
105
  end
104
106
 
107
+ def with_context(new_context)
108
+ @context = if new_context.is_a?(Hash)
109
+ OpenStruct.new(new_context)
110
+ else
111
+ new_context
112
+ end
113
+
114
+ attributes_that_respond_to(:with_context)
115
+ .each { |f| f.with_context(context) }
116
+
117
+ array_attributes_that_respond_to(:with_context)
118
+ .each { |f| f.with_context(context) }
119
+
120
+ self
121
+ end
122
+
105
123
  private
106
124
 
107
125
  def form_attributes_valid?
108
- attributes
109
- .each_value
110
- .select { |f| f.respond_to?(:valid?) }
126
+ attributes_that_respond_to(:valid?)
111
127
  .map(&:valid?)
112
128
  .all?
113
129
  end
114
130
 
115
131
  def arrays_attributes_valid?
132
+ array_attributes_that_respond_to(:valid?)
133
+ .map(&:valid?)
134
+ .all?
135
+ end
136
+
137
+ def attributes_that_respond_to(message)
138
+ attributes
139
+ .each_value
140
+ .select { |f| f.respond_to?(message) }
141
+ end
142
+
143
+ def array_attributes_that_respond_to(message)
116
144
  attributes
117
145
  .each_value
118
146
  .select { |a| a.is_a?(Array) }
119
147
  .flatten
120
- .select { |f| f.respond_to?(:valid?) }
121
- .map(&:valid?)
122
- .all?
148
+ .select { |f| f.respond_to?(message) }
123
149
  end
124
150
  end
125
151
  end
@@ -26,9 +26,7 @@ module Rectify
26
26
  end
27
27
 
28
28
  def view_context
29
- @view_context ||= begin
30
- controller && controller.view_context
31
- end
29
+ @view_context ||= controller.view_context
32
30
  end
33
31
  end
34
32
  end
@@ -1,3 +1,3 @@
1
1
  module Rectify
2
- VERSION = "0.7.1".freeze
2
+ VERSION = "0.8.0".freeze
3
3
  end
data/readme.md CHANGED
@@ -371,6 +371,61 @@ your form as well as any (deeply) nested form objects and array attributes that
371
371
  contain form objects. There is also an `#invalid?` method that returns the
372
372
  opposite of `#valid?`.
373
373
 
374
+ ### Deep Context
375
+
376
+ It's sometimes useful to have some context within your form objects when performing
377
+ validations or some other type of data manipulation of the input. For example, you
378
+ might want to check that the current user owns a particular resource as part of your
379
+ validations. You could add the current user as an additional contextual option as
380
+ the example shows above. However, sometimes you need this context to be available
381
+ at all levels within your form not just at the root form object. You might have nested
382
+ forms or arrays of form objects and they all might need access to this context. As
383
+ there is no link up the chain from child to parent forms, we need a way to supply
384
+ some context and make it available to all child forms.
385
+
386
+ You can do that using the `#with_context` method.
387
+
388
+ ```ruby
389
+ form = UserForm.from_params(params).with_context(:user => current_user)
390
+ ```
391
+
392
+ This allows us to access `#context` in any form, and use the information within
393
+ it when we perform validations or other work:
394
+
395
+ ```ruby
396
+ class PostForm < Rectify::Form
397
+ attribute :blog_id, Integer
398
+ attribute :title, String
399
+ attribute :body, String
400
+ attribute :tags, Array[TagForm]
401
+
402
+ validate :check_blog_ownership
403
+
404
+ def check_blog_ownership
405
+ return if context.user.blogs.exists?(:id => blog_id)
406
+
407
+ errors.add(:blog_id, "not owned by this user")
408
+ end
409
+ end
410
+
411
+ class TagForm < Rectify::Form
412
+ attribute :name, String
413
+ attribute :category_id, Integer
414
+
415
+ validate :check_category
416
+
417
+ def check_category
418
+ return if context.user.categories.exists?(:id => category_id)
419
+
420
+ errors.add(:category_id, "not a category for this user")
421
+ end
422
+ end
423
+ ```
424
+
425
+ The context is passed to all nested forms within a form object to make it easy
426
+ to perform all the validations and data conversions you might need from within
427
+ the form object without having to do this as part of the command.
428
+
374
429
  ### Strong Parameters
375
430
 
376
431
  Did you notice in the example above that there was no mention of Strong
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.7.1
4
+ version: 0.8.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-11-01 00:00:00.000000000 Z
11
+ date: 2016-12-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: virtus