rectify 0.8.0 → 0.9.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/form.rb +5 -16
- data/lib/rectify/format_attributes_hash.rb +46 -0
- data/lib/rectify/version.rb +1 -1
- data/lib/rectify.rb +1 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4f81f92c6b9b526d6000d455dbb9ba6fa54f2918
|
4
|
+
data.tar.gz: 941296c66a7deedccde2e30cd90021edbaab2980
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
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
|
data/lib/rectify/version.rb
CHANGED
data/lib/rectify.rb
CHANGED
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.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:
|
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
|