rectify 0.8.0 → 0.9.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: 2d2a653fa353d25d4303b71cddb5858ad2562355
4
- data.tar.gz: 783cacad47dd54af9d9adb22afc5399214715443
3
+ metadata.gz: 4f81f92c6b9b526d6000d455dbb9ba6fa54f2918
4
+ data.tar.gz: 941296c66a7deedccde2e30cd90021edbaab2980
5
5
  SHA512:
6
- metadata.gz: 7240ab43419d70d321e9000473639e3397752ed5d3dc6b8a589c86af2193b5f17dab94f7f4628f904f174a147f1500104e27f317c9f2ed85ebd408040699e305
7
- data.tar.gz: b56649c7dcac2d0fbebd93fe78c1f4757afb93f0db3653d7d9143860bf5e2b3e59941b4da004ad09b328fdaeb5feae77183abb463b7ad2f9274ef7d72cd233cc
6
+ metadata.gz: e5824327074db708534c6d3009769b7b4b7669bd294fa609e5020784fded58738a363eba433b23376530cd754200d2e5f1c3a372a3203fcffcfb2c77996cca7e
7
+ data.tar.gz: aa430bbd17f4ed0075f918fe203da94fc73d04a4a23c8602945c84f3f19d58c439b9ad6a10e526c7b43fca2eb78b73188d9f5e89399e9678ff2913b3295baf55
data/lib/rectify/form.rb CHANGED
@@ -17,22 +17,7 @@ module Rectify
17
17
  .merge(params_hash.slice(*attribute_names))
18
18
  .merge(additional_params)
19
19
 
20
- convert_indexed_hashes_to_arrays(attributes_hash)
21
-
22
- new(attributes_hash)
23
- end
24
-
25
- def self.convert_indexed_hashes_to_arrays(attributes_hash)
26
- array_attribute_names.each do |name|
27
- attribute = attributes_hash[name]
28
- next unless attribute.is_a?(Hash)
29
-
30
- attributes_hash[name] = attribute.values
31
- end
32
- end
33
-
34
- def self.array_attribute_names
35
- attribute_set.select { |a| a.primitive == Array }.map { |a| a.name.to_s }
20
+ new(FormatAttributesHash.new(attribute_set).format(attributes_hash))
36
21
  end
37
22
 
38
23
  def self.from_model(model)
@@ -93,6 +78,10 @@ module Rectify
93
78
  super.except(:id)
94
79
  end
95
80
 
81
+ def attributes_with_values
82
+ attributes.reject { |attribute| public_send(attribute).nil? }
83
+ end
84
+
96
85
  def map_model(model)
97
86
  # Implement this in your form object for custom mapping from model to form
98
87
  # object as part of the `.from_model` call after matching attributes are
@@ -0,0 +1,46 @@
1
+ # Based on http://stackoverflow.com/questions/8706930/converting-nested-hash-keys-from-camelcase-to-snake-case-in-ruby
2
+
3
+ module Rectify
4
+ class FormatAttributesHash
5
+ def initialize(attribute_set)
6
+ @attribute_set = attribute_set
7
+ end
8
+
9
+ def format(params)
10
+ convert_indexed_hashes_to_arrays(params)
11
+ convert_hash_keys(params)
12
+ end
13
+
14
+ private
15
+
16
+ attr_reader :attribute_set
17
+
18
+ def convert_indexed_hashes_to_arrays(attributes_hash)
19
+ array_attribute_names.each do |name|
20
+ attribute = attributes_hash[name]
21
+ next unless attribute.is_a?(Hash)
22
+
23
+ attributes_hash[name] = attribute.values
24
+ end
25
+ end
26
+
27
+ def array_attribute_names
28
+ attribute_set.select { |a| a.primitive == Array }.map { |a| a.name.to_s }
29
+ end
30
+
31
+ def convert_hash_keys(value)
32
+ case value
33
+ when Array
34
+ value.map { |v| convert_hash_keys(v) }
35
+ when Hash
36
+ Hash[value.map { |k, v| [underscore_key(k), convert_hash_keys(v)] }]
37
+ else
38
+ value
39
+ end
40
+ end
41
+
42
+ def underscore_key(k)
43
+ k.to_s.underscore.to_sym
44
+ end
45
+ end
46
+ end
@@ -1,3 +1,3 @@
1
1
  module Rectify
2
- VERSION = "0.8.0".freeze
2
+ VERSION = "0.9.0".freeze
3
3
  end
data/lib/rectify.rb CHANGED
@@ -9,6 +9,7 @@ require "active_record"
9
9
  require "rectify/version"
10
10
  require "rectify/form"
11
11
  require "rectify/form_attribute"
12
+ require "rectify/format_attributes_hash"
12
13
  require "rectify/build_form_from_model"
13
14
  require "rectify/command"
14
15
  require "rectify/presenter"
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.8.0
4
+ version: 0.9.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-12-05 00:00:00.000000000 Z
11
+ date: 2017-03-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: virtus
@@ -192,6 +192,7 @@ files:
192
192
  - lib/rectify/errors.rb
193
193
  - lib/rectify/form.rb
194
194
  - lib/rectify/form_attribute.rb
195
+ - lib/rectify/format_attributes_hash.rb
195
196
  - lib/rectify/null_query.rb
196
197
  - lib/rectify/presenter.rb
197
198
  - lib/rectify/query.rb