rectify 0.4.0 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/rectify.rb +2 -0
- data/lib/rectify/build_form_from_model.rb +35 -0
- data/lib/rectify/form.rb +1 -1
- data/lib/rectify/form_attribute.rb +35 -0
- data/lib/rectify/version.rb +1 -1
- data/readme.md +10 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4511c35c01c0537574a5a687cdd29ca0b920d368
|
4
|
+
data.tar.gz: f6cc201bcf222a71f5698f1a14e58d2a3d6f6ce8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 59ab549209b787188e97bb0ac3914e6cb833f5d83256f49f6c786b721bbe0447c036043c1bd671209d35d7450068f3a03b2267109476f1a2b5dd06b2a925c596
|
7
|
+
data.tar.gz: 14e6e8fa08b122f14816bbdef5b521c16376c1f737eec97970d1d673290e48bb638bf15f3e8a227b0f51686913f0831ae1363e0c7aa51ec72a8ee6535c6260b4
|
data/lib/rectify.rb
CHANGED
@@ -0,0 +1,35 @@
|
|
1
|
+
module Rectify
|
2
|
+
class BuildFormFromModel
|
3
|
+
def initialize(form_class, model)
|
4
|
+
@form_class = form_class
|
5
|
+
@model = model
|
6
|
+
end
|
7
|
+
|
8
|
+
def build
|
9
|
+
form.tap do
|
10
|
+
matching_attributes.each do |a|
|
11
|
+
model_value = model.public_send(a.name)
|
12
|
+
form.public_send("#{a.name}=", a.value_from(model_value))
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
attr_reader :form_class, :form, :model
|
20
|
+
|
21
|
+
def form
|
22
|
+
@form ||= form_class.new
|
23
|
+
end
|
24
|
+
|
25
|
+
def attribute_set
|
26
|
+
form_class.attribute_set
|
27
|
+
end
|
28
|
+
|
29
|
+
def matching_attributes
|
30
|
+
attribute_set
|
31
|
+
.select { |a| model.respond_to?(a.name) }
|
32
|
+
.map { |a| FormAttribute.new(a) }
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
data/lib/rectify/form.rb
CHANGED
@@ -0,0 +1,35 @@
|
|
1
|
+
module Rectify
|
2
|
+
class FormAttribute < SimpleDelegator
|
3
|
+
def value_from(model_value)
|
4
|
+
return declared_class.from_model(model_value) if form_object?
|
5
|
+
|
6
|
+
if collection_of_form_objects?
|
7
|
+
return model_value.map { |child| element_class.from_model(child) }
|
8
|
+
end
|
9
|
+
|
10
|
+
model_value
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def form_object?
|
16
|
+
declared_class.respond_to?(:from_model)
|
17
|
+
end
|
18
|
+
|
19
|
+
def collection_of_form_objects?
|
20
|
+
collection? && element_class.respond_to?(:from_model)
|
21
|
+
end
|
22
|
+
|
23
|
+
def collection?
|
24
|
+
type.respond_to?(:member_type)
|
25
|
+
end
|
26
|
+
|
27
|
+
def element_class
|
28
|
+
type.member_type
|
29
|
+
end
|
30
|
+
|
31
|
+
def declared_class
|
32
|
+
primitive
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
data/lib/rectify/version.rb
CHANGED
data/readme.md
CHANGED
@@ -1022,3 +1022,13 @@ rake db:migrate # => Migrates the test database
|
|
1022
1022
|
rake db:schema # => Dumps database schema
|
1023
1023
|
rake g:migration # => Create a new migration file
|
1024
1024
|
```
|
1025
|
+
|
1026
|
+
### Releasing a new version
|
1027
|
+
|
1028
|
+
Bump the version in `lib/rectify/version.rb` then do the following:
|
1029
|
+
|
1030
|
+
```
|
1031
|
+
bundle
|
1032
|
+
gem build rectify.gemspec
|
1033
|
+
gem push rectify-0.0.0.gem
|
1034
|
+
```
|
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.
|
4
|
+
version: 0.4.1
|
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-04-
|
11
|
+
date: 2016-04-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: virtus
|
@@ -200,10 +200,12 @@ extra_rdoc_files: []
|
|
200
200
|
files:
|
201
201
|
- LICENSE.txt
|
202
202
|
- lib/rectify.rb
|
203
|
+
- lib/rectify/build_form_from_model.rb
|
203
204
|
- lib/rectify/command.rb
|
204
205
|
- lib/rectify/controller_helpers.rb
|
205
206
|
- lib/rectify/errors.rb
|
206
207
|
- lib/rectify/form.rb
|
208
|
+
- lib/rectify/form_attribute.rb
|
207
209
|
- lib/rectify/null_query.rb
|
208
210
|
- lib/rectify/presenter.rb
|
209
211
|
- lib/rectify/query.rb
|