reform 1.2.2 → 1.2.3

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: 1a65ba4e715246d4be150394d9ee6fbcd34af3e8
4
- data.tar.gz: 8c12fd32b88fd52a7c20cbe2cf84f5a6bbf01c8d
3
+ metadata.gz: 777f16f9f2f9f029ad3d3b9ee1cdd2b12ec31bb7
4
+ data.tar.gz: a1dc7b0e61b13090a6a67bda43520104c58e502d
5
5
  SHA512:
6
- metadata.gz: 922aee7abf462ddcc510ffcce88f90a88d6450586df3c9eed27e174f5c6f73a8c6e43022147a6b730daa61fd61016bf3e97f620458546d5da5e5c6d30f134c68
7
- data.tar.gz: b752b34d4a88ab8ac02f594635fc793ea3f7d0f39c139971d1d944d26109aa921196b46242febbd7b68ea478e87e8b12cd06b0fe1765391b427cbfd0c7216a39
6
+ metadata.gz: 9ca591099b12b4b519311e74d1bb89c7251473b2f827115d7c56c9880886a8a9f76f4326d48d386dc4d36f42c753ccf74bedb088d714524764022a5a7b316dea
7
+ data.tar.gz: efb2ea5be6e19226fbe2ad6f9741380e9ab5a2b1630fcd96dabb146c0c26cb8478a7668ef7ff378170d1a7e66842a2c9b646ffb120d971bb37eaa438c9018116
data/CHANGES.md CHANGED
@@ -1,3 +1,13 @@
1
+ ## 1.2.3
2
+
3
+ * Make `ModelReflections` work with simple_form 3.1.0. (#176). It also provides `defined_enums` and `::reflect_on_association` now.
4
+ * `nil` values passed into `#validate` will now be written to the model in `#sync` (#175). Formerly, only blank strings and values evaluating to true were considered when syncing. This allows blanking fields of the model as follows.
5
+
6
+ ```ruby
7
+ form.validate(title: nil)
8
+ ```
9
+ * Calling `Form::reform_2_0!` will now properly inherit to nested forms.
10
+
1
11
  ## 1.2.2
2
12
 
3
13
  * Use new `uber` to allow subclassing `reform_2_0!` forms.
data/README.md CHANGED
@@ -48,7 +48,7 @@ Luckily, this can be shortened as follows.
48
48
  ```ruby
49
49
  class SongForm < Reform::Form
50
50
  property :title, validates: {presence: true}
51
- property :length, validates {numericality: true}
51
+ property :length, validates: {numericality: true}
52
52
  end
53
53
  ```
54
54
 
@@ -905,7 +905,7 @@ You can also provide the sync lambda at run-time.
905
905
  form.sync(title: lambda { |value, options| form.model.title = "HOT: #{value}" })
906
906
  ```
907
907
 
908
- This block is run in the caller's context allowing you to access environment variables. Note that the dynamic sync happens _before_ save, so the model id may unavailable.
908
+ This block is run in the caller's context allowing you to access environment variables. Note that the dynamic sync happens _before_ save, so the model id may be unavailable.
909
909
 
910
910
  You can do the same for saving.
911
911
 
data/TODO.md CHANGED
@@ -3,7 +3,7 @@
3
3
  * empty dont read, but write
4
4
  * read_only: read, don't write
5
5
 
6
-
6
+ * make SkipUnchanged default?
7
7
 
8
8
 
9
9
  * `validates :title, :presence => true`
data/database.sqlite3 CHANGED
Binary file
@@ -164,11 +164,6 @@ module Reform
164
164
  end
165
165
  end
166
166
 
167
- inheritable_attr :reform_2_0 # TODO: remove me in 2.0.
168
- def self.reform_2_0!
169
- self.reform_2_0= true
170
- end
171
-
172
167
  def self.register_feature(mod)
173
168
  features[mod] = true
174
169
  end
@@ -196,6 +191,19 @@ module Reform
196
191
 
197
192
  alias_method :aliased_model, :model
198
193
 
194
+ # TODO: remove me in 2.0.
195
+ module Reform20Switch
196
+ def self.included(base)
197
+ base.register_feature(Reform20Switch)
198
+ end
199
+ end
200
+ def self.reform_2_0!
201
+ include Reform20Switch
202
+ end
203
+ def self.reform_2_0
204
+ features[Reform20Switch]
205
+ end
206
+
199
207
 
200
208
  # Keeps values of the form fields. What's in here is to be displayed in the browser!
201
209
  # we need this intermediate object to display both "original values" and new input from the form after submitting.
@@ -5,14 +5,33 @@
5
5
  # doesn't have to "guess" what simple_form and other form helpers need.
6
6
  module Reform::Form::ModelReflections
7
7
  def self.included(base)
8
+ base.extend ClassMethods
8
9
  base.register_feature self # makes it work in nested forms.
9
10
  end
10
11
 
12
+ module ClassMethods
13
+ # Delegate reflect_on_association to the model class to support simple_form's
14
+ # association input.
15
+ def reflect_on_association(*args)
16
+ model_name.to_s.constantize.reflect_on_association(*args)
17
+ end
18
+ end
19
+
11
20
  # Delegate column for attribute to the model to support simple_form's
12
21
  # attribute type interrogation.
13
22
  def column_for_attribute(name)
14
23
  model_for_property(name).column_for_attribute(name)
15
24
  end
16
25
 
26
+ def has_attribute?(name)
27
+ model_for_property(name).has_attribute?(name)
28
+ end
29
+
30
+ def defined_enums
31
+ return model.defined_enums unless is_a?(Reform::Form::Composition)
32
+
33
+ model.each.with_object({}) { |m,h| h.merge! m.defined_enums }
34
+ end
35
+
17
36
  # this should also contain to_param and friends as this is used by the form helpers.
18
37
  end
@@ -27,11 +27,15 @@ private
27
27
  # Transforms form input into what actually gets written to model.
28
28
  # output: {title: "Mint Car", hit: <Form>}
29
29
  def input_representer
30
- self.class.representer(:input) do |dfn|
31
- dfn.merge!(
32
- :representable => false,
33
- :prepare => lambda { |obj, *| obj }
34
- )
30
+ self.class.representer(:input, :all => true) do |dfn|
31
+ if dfn[:form]
32
+ dfn.merge!(
33
+ :representable => false,
34
+ :prepare => lambda { |obj, *| obj },
35
+ )
36
+ else
37
+ dfn.merge!(:render_nil => true) # do sync nil values back to the model for scalars.
38
+ end
35
39
  end
36
40
  end
37
41
 
@@ -1,3 +1,3 @@
1
1
  module Reform
2
- VERSION = "1.2.2"
2
+ VERSION = "1.2.3"
3
3
  end
@@ -16,7 +16,19 @@ class ModelReflectionTest < MiniTest::Spec
16
16
 
17
17
  module ColumnForAttribute
18
18
  def column_for_attribute(*args)
19
- "#{self.class}: #{args.inspect}"
19
+ "#{self.class}: #{args.inspect}"
20
+ end
21
+ end
22
+
23
+ module HasAttribute
24
+ def has_attribute?(*args)
25
+ "#{self.class}: has #{args.inspect}"
26
+ end
27
+ end
28
+
29
+ module DefinedEnums
30
+ def defined_enums
31
+ {self.class => []}
20
32
  end
21
33
  end
22
34
 
@@ -35,6 +47,47 @@ class ModelReflectionTest < MiniTest::Spec
35
47
  end
36
48
  end
37
49
 
50
+ describe "#has_attribute?" do
51
+ let (:artist) { Artist.new }
52
+ let (:song) { Song.new(artist: artist) }
53
+ let (:form) { SongForm.new(song) }
54
+
55
+ # delegate to model.
56
+ it do
57
+ song.extend(HasAttribute)
58
+ artist.extend(HasAttribute)
59
+
60
+ form.has_attribute?(:title).must_equal "Song: has [:title]"
61
+ form.artist.has_attribute?(:name).must_equal "Artist: has [:name]"
62
+ end
63
+ end
64
+
65
+ describe "#defined_enums" do
66
+ let (:artist) { Artist.new }
67
+ let (:song) { Song.new(artist: artist) }
68
+ let (:form) { SongForm.new(song) }
69
+
70
+ # delegate to model.
71
+ it do
72
+ song.extend(DefinedEnums)
73
+ artist.extend(DefinedEnums)
74
+
75
+ form.defined_enums.must_include Song
76
+ form.artist.defined_enums.must_include Artist
77
+ end
78
+ end
79
+
80
+ describe ".reflect_on_association" do
81
+ let (:artist) { Artist.new }
82
+ let (:song) { Song.new(artist: artist) }
83
+ let (:form) { SongForm.new(song) }
84
+
85
+ # delegate to model class.
86
+ it do
87
+ reflection = form.class.reflect_on_association(:artist)
88
+ reflection.must_be_instance_of ActiveRecord::Reflection::AssociationReflection
89
+ end
90
+ end
38
91
 
39
92
  class SongWithArtistForm < Reform::Form
40
93
  include Reform::Form::ActiveRecord
@@ -62,4 +115,20 @@ class ModelReflectionTest < MiniTest::Spec
62
115
  form.column_for_attribute(:title).must_equal "Song: [:title]"
63
116
  end
64
117
  end
65
- end
118
+
119
+ describe "#defined_enums with composition" do
120
+ let (:artist) { Artist.new }
121
+ let (:song) { Song.new }
122
+ let (:form) { SongWithArtistForm.new(artist: artist, song: song) }
123
+
124
+ # delegates to respective model.
125
+ it do
126
+ song.extend(DefinedEnums)
127
+ artist.extend(DefinedEnums)
128
+
129
+
130
+ form.defined_enums.must_include Song
131
+ form.defined_enums.must_include Artist
132
+ end
133
+ end
134
+ end
data/test/sync_test.rb CHANGED
@@ -39,4 +39,18 @@ class SyncTest < BaseTest
39
39
  it { song2.title.must_equal "Roxanne" }
40
40
  it { label.name.must_equal "Polydor" }
41
41
  end
42
+
43
+ describe "with incoming nil value" do
44
+ it do
45
+ album = Album.new("GI")
46
+ form = ErrorsTest::AlbumForm.new(album)
47
+
48
+ form.title.must_equal "GI"
49
+
50
+ form.validate("title" => nil)
51
+ form.title.must_equal nil
52
+ form.sync
53
+ album.title.must_equal nil
54
+ end
55
+ end
42
56
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: reform
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.2
4
+ version: 1.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nick Sutterer
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-11-11 00:00:00.000000000 Z
12
+ date: 2014-12-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: representable