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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 76e80de9044d3a1b477393b19707c90ca78e64e2
4
- data.tar.gz: e7f8fa99d59f1adf320f322d4fc237f4927e9c34
3
+ metadata.gz: 4511c35c01c0537574a5a687cdd29ca0b920d368
4
+ data.tar.gz: f6cc201bcf222a71f5698f1a14e58d2a3d6f6ce8
5
5
  SHA512:
6
- metadata.gz: d4f18bb20decef78424111d079cee4762da7c2e864c91a89e1429e6ce4fc688f9295f96f10735ca4f1d832a2a9dba3e400300070b1f5f62287c6a4702f6b2390
7
- data.tar.gz: 8309c1d194bf83f7d1484b1c201317cadd54083ea867e87ac0a7fbc73c4b5a26e93f24b5888b14768d67a891e4d81276e07e76d4434e07fcffada6cd914a185a
6
+ metadata.gz: 59ab549209b787188e97bb0ac3914e6cb833f5d83256f49f6c786b721bbe0447c036043c1bd671209d35d7450068f3a03b2267109476f1a2b5dd06b2a925c596
7
+ data.tar.gz: 14e6e8fa08b122f14816bbdef5b521c16376c1f737eec97970d1d673290e48bb638bf15f3e8a227b0f51686913f0831ae1363e0c7aa51ec72a8ee6535c6260b4
data/lib/rectify.rb CHANGED
@@ -8,6 +8,8 @@ require "active_record"
8
8
 
9
9
  require "rectify/version"
10
10
  require "rectify/form"
11
+ require "rectify/form_attribute"
12
+ require "rectify/build_form_from_model"
11
13
  require "rectify/command"
12
14
  require "rectify/presenter"
13
15
  require "rectify/query"
@@ -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
@@ -16,7 +16,7 @@ module Rectify
16
16
  end
17
17
 
18
18
  def self.from_model(model)
19
- new(model.attributes)
19
+ Rectify::BuildFormFromModel.new(self, model).build
20
20
  end
21
21
 
22
22
  def self.mimic(model_name)
@@ -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
@@ -1,3 +1,3 @@
1
1
  module Rectify
2
- VERSION = "0.4.0"
2
+ VERSION = "0.4.1"
3
3
  end
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.0
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-18 00:00:00.000000000 Z
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