rectify 0.12.0 → 0.13.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
- SHA1:
3
- metadata.gz: 2917c203f981798ec8aece021edc988fa3401407
4
- data.tar.gz: d8e3dd7970418ebf72d27d21c64e458e58eed93f
2
+ SHA256:
3
+ metadata.gz: 746036ed910a8095cff3044cbda94ee43a8879f9387e2d34e9020651ceff587f
4
+ data.tar.gz: 9b3883bf9a2af976f15edfc678d032b3c878ac3a5b11ca95eab2b853d38fe1ea
5
5
  SHA512:
6
- metadata.gz: 0a2e6048a2d9489e94c2420d924fd9f1b5d0450684d9970f99a22ddd7523a1a5260307b7ba93079a94682a7a42debbdbe585011400f59a79e617512445512746
7
- data.tar.gz: 6ef948555bf0bb6a7f20c114e860cad00036987a5879a877d2bf22bcd14e4b744eed19a445da307bc2bab7546427a38337a2780e7634afd9e299043b12221a98
6
+ metadata.gz: 2edb94003a8a75bdb2d050e8e78167f1cd021300f9daed422a93b9b10afc4667024508ae818cde5363722d5f9f8f69d6cbf47864bae06ee60020facfe2bcb496
7
+ data.tar.gz: 6002583baf035ed5011f152429f984c4e07b2da47833b4b3eb889b9732fb3e4d8bb3a03c90f8e2df1a4196c82b21e6a0a1a15603cbcedade620a5c3dc2af29fc
@@ -9,10 +9,10 @@ module Rectify
9
9
 
10
10
  def self.from_params(params, additional_params = {})
11
11
  params_hash = hash_from(params)
12
+ mimicked_params = ensure_hash(params_hash[mimicked_model_name])
12
13
 
13
14
  attributes_hash = params_hash
14
- .fetch(mimicked_model_name, {})
15
- .merge(params_hash)
15
+ .merge(mimicked_params)
16
16
  .merge(additional_params)
17
17
 
18
18
  formatted_attributes = FormatAttributesHash
@@ -54,6 +54,14 @@ module Rectify
54
54
  params.with_indifferent_access
55
55
  end
56
56
 
57
+ def self.ensure_hash(object)
58
+ if object.is_a?(Hash)
59
+ object
60
+ else
61
+ {}
62
+ end
63
+ end
64
+
57
65
  def persisted?
58
66
  id.present? && id.to_i > 0
59
67
  end
@@ -16,16 +16,28 @@ module Rectify
16
16
  attr_reader :attribute_set
17
17
 
18
18
  def convert_indexed_hashes_to_arrays(attributes_hash)
19
- array_attribute_names.each do |name|
19
+ array_attributes.each do |array_attribute|
20
+ name = array_attribute.name
20
21
  attribute = attributes_hash[name]
21
22
  next unless attribute.is_a?(Hash)
22
23
 
23
- attributes_hash[name] = attribute.values
24
+ attributes_hash[name] = transform_values_for_type(
25
+ attribute.values,
26
+ array_attribute.member_type.primitive
27
+ )
24
28
  end
25
29
  end
26
30
 
27
- def array_attribute_names
28
- attribute_set.select { |a| a.primitive == Array }.map { |a| a.name.to_s }
31
+ def transform_values_for_type(values, element_type)
32
+ return values unless element_type < Rectify::Form
33
+
34
+ values.map do |value|
35
+ self.class.new(element_type.attribute_set).format(value)
36
+ end
37
+ end
38
+
39
+ def array_attributes
40
+ attribute_set.select { |attribute| attribute.primitive == Array }
29
41
  end
30
42
 
31
43
  def convert_hash_keys(value)
@@ -1,3 +1,3 @@
1
1
  module Rectify
2
- VERSION = "0.12.0".freeze
2
+ VERSION = "0.13.0".freeze
3
3
  end
data/readme.md CHANGED
@@ -473,6 +473,27 @@ other data in your params hash is ignored.
473
473
  Take a look at [Virtus](https://github.com/solnic/virtus) for more information
474
474
  about how to build a form object.
475
475
 
476
+ ### I18n
477
+
478
+ Regarding internationalization, the main affected classes when coercing are `Date` and `Time` classes. This is coercing Strings into `Date`, `DateTime` and `Time`. Texts don't usually need to be coerced as they are simple String attributes with nothing special in them.
479
+
480
+ When coercing dates and times in a multi-language application, each locale will have its own date and time formats, and these formats should be taken into account when coercing strings (inputs entered by the user, or comming form external sources).
481
+
482
+ So for `Date`, `DateTime` and `Time` classes, Rectify does not support I18n by default. But there are some ways to achieve it indirectly.
483
+
484
+ Probably the best is to define custom `Virtus::Attribute`s for each kind of temporal class. For exmaple:
485
+
486
+ ```ruby
487
+ class LocalizedDate < Virtus::Attribute
488
+ def coerce(value)
489
+ return value unless value.is_a?(String)
490
+ Date.strptime(value, I18n.t("date.formats.short"))
491
+ rescue ArgumentError
492
+ nil
493
+ end
494
+ end
495
+ ```
496
+
476
497
  ## Commands
477
498
 
478
499
  Commands (also known as Service Objects) are the home of your business logic.
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.12.0
4
+ version: 0.13.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: 2018-05-22 00:00:00.000000000 Z
11
+ date: 2018-10-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -142,14 +142,14 @@ dependencies:
142
142
  requirements:
143
143
  - - "~>"
144
144
  - !ruby/object:Gem::Version
145
- version: '3.4'
145
+ version: '3.8'
146
146
  type: :development
147
147
  prerelease: false
148
148
  version_requirements: !ruby/object:Gem::Requirement
149
149
  requirements:
150
150
  - - "~>"
151
151
  - !ruby/object:Gem::Version
152
- version: '3.4'
152
+ version: '3.8'
153
153
  - !ruby/object:Gem::Dependency
154
154
  name: rspec-collection_matchers
155
155
  requirement: !ruby/object:Gem::Requirement
@@ -242,7 +242,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
242
242
  version: '0'
243
243
  requirements: []
244
244
  rubyforge_project:
245
- rubygems_version: 2.4.5.1
245
+ rubygems_version: 2.7.6
246
246
  signing_key:
247
247
  specification_version: 4
248
248
  summary: Improvements for building Rails apps