reform 0.2.3 → 0.2.4

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: 9ce0b79a877613cd7bcd9cfd868d0a33414c531a
4
- data.tar.gz: f0e06d22099e1d335f692e865aff069428e9dc75
3
+ metadata.gz: 201a01642c1743227a0209c716240374d884189f
4
+ data.tar.gz: 4327aa39fbfae302b0078c6787a0df387aa08080
5
5
  SHA512:
6
- metadata.gz: 4f9f35619dea63561e09239ff40ec7765695a9f93638a32f1f5b743f6752aafce757497af1c28e9244fcb03cdaaf19cf07616dccb093881ce7b3b39b0d3f8af1
7
- data.tar.gz: 30d28814ca8b99abba0385efb9c3ca391066402a39f81f45cacba4e927d52beb1e74516072fdb42a3946f4964099395bd48b12bd6ceb0aa1c2da0eb019c7927d
6
+ metadata.gz: 42bad26c32be288977aae6cc6be0f634382b5bdc0cc733ce5b0f2084096db897df33b7126923b58f9d3eee4f50e63b1ee0095650a2c414c12a2c1e9fbc5148fd
7
+ data.tar.gz: 94ebe84171e96c49fff4adbdc704d5c94cebc0713a27a35b86941ffc33e23947e7371afe9b8d6fce3f7bffee1cd6ba2363418635abd87f65230bbebf7b2bf385
data/CHANGES.md CHANGED
@@ -1,3 +1,8 @@
1
+ ## 0.2.4
2
+
3
+ * Accessors for properties (e.g. `title` and `title=`) can now be overridden in the form *and* call `super`. This is extremely helpful if you wanna do "manual coercion" since the accessors are invoked in `#validate`. Thanks to @cj for requesting this.
4
+ * Inline forms now know their class name from the property that defines them. This is needed for I18N where `ActiveModel` queries the class name to compute translation keys. If you're not happy with it, use `::model`.
5
+
1
6
  ## 0.2.3
2
7
 
3
8
  * `#form_for` now properly recognizes a nested form when declared using `:form` (instead of an inline form).
data/README.md CHANGED
@@ -386,7 +386,6 @@ form.save do |f, nested|
386
386
  f.country #=> "Australia"
387
387
  ```
388
388
 
389
-
390
389
  ## Agnosticism: Mapping Data
391
390
 
392
391
  Reform doesn't really know whether it's working with a PORO, an `ActiveRecord` instance or a `Sequel` row.
@@ -482,6 +481,21 @@ property :song, form: SongForm`
482
481
  The nested `SongForm` is a stand-alone form class you have to provide.
483
482
 
484
483
 
484
+ ### Overriding Accessors
485
+
486
+ When "real" coercion is too much and you simply want to convert incoming data yourself, override the setter.
487
+
488
+ ```ruby
489
+ class SongForm < Reform::Form
490
+ property :title
491
+
492
+ def title=(v)
493
+ super(v.upcase)
494
+ end
495
+ ```
496
+
497
+ This will capitalize the title _after_ calling `form.validate` but _before_ validation happens. Note that you can use `super` to call the original setter.
498
+
485
499
  ## Support
486
500
 
487
501
  If you run into any trouble chat with us on irc.freenode.org#trailblazer.
data/database.sqlite3 CHANGED
Binary file
data/lib/reform/form.rb CHANGED
@@ -49,7 +49,13 @@ module Reform
49
49
 
50
50
  private
51
51
  def create_accessor(name)
52
- delegate [name, "#{name}="] => :fields
52
+ # Make a module that contains these very accessors, then include it
53
+ # so they can be overridden but still are callable with super.
54
+ accessors = Module.new do
55
+ extend Forwardable # DISCUSS: do we really need Forwardable here?
56
+ delegate [name, "#{name}="] => :fields
57
+ end
58
+ include accessors
53
59
  end
54
60
 
55
61
  def process_options(name, options) # DISCUSS: do we need that hook?
@@ -55,12 +55,16 @@ module Reform
55
55
  @representable_attrs = attrs
56
56
  end
57
57
 
58
- def self.inline_representer(base_module, &block) # DISCUSS: separate module?
58
+ def self.inline_representer(base_module, name, options, &block)
59
+ name = name.to_s.singularize.camelize
60
+
59
61
  Class.new(Form) do
60
62
  instance_exec &block
61
63
 
62
- def self.name # FIXME: needed by ActiveModel::Validation - why?
63
- "AnonInlineForm"
64
+ @form_name = name
65
+
66
+ def self.name # needed by ActiveModel::Validation and I18N.
67
+ @form_name
64
68
  end
65
69
  end
66
70
  end
@@ -1,3 +1,3 @@
1
1
  module Reform
2
- VERSION = "0.2.3"
2
+ VERSION = "0.2.4"
3
3
  end
data/reform.gemspec CHANGED
@@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
- spec.add_dependency "representable", "~> 1.7.0"
21
+ spec.add_dependency "representable", ">= 1.7.5"
22
22
  spec.add_dependency "activemodel"
23
23
  spec.add_development_dependency "bundler", "~> 1.3"
24
24
  spec.add_development_dependency "rake", ">= 10.1.0"
@@ -30,6 +30,48 @@ class NewActiveModelTest < MiniTest::Spec # TODO: move to test/rails/
30
30
 
31
31
  it { class_with_model.model_name.must_be_kind_of ActiveModel::Name }
32
32
  it { class_with_model.model_name.to_s.must_equal "Album" }
33
+
34
+ describe "inline with model" do
35
+ let (:form_class) {
36
+ Class.new(Reform::Form) do
37
+ include Reform::Form::ActiveModel
38
+
39
+ property :song do
40
+ include Reform::Form::ActiveModel
41
+ model :hit
42
+ end
43
+ end
44
+ }
45
+
46
+ let (:inline) { form_class.new(OpenStruct.new).song }
47
+
48
+ it { inline.class.model_name.must_be_kind_of ActiveModel::Name }
49
+ it { inline.class.model_name.to_s.must_equal "Hit" }
50
+ end
51
+
52
+ describe "inline without model" do
53
+ let (:form_class) {
54
+ Class.new(Reform::Form) do
55
+ include Reform::Form::ActiveModel
56
+
57
+ property :song do
58
+ include Reform::Form::ActiveModel
59
+ end
60
+
61
+ collection :hits do
62
+ include Reform::Form::ActiveModel
63
+ end
64
+ end
65
+ }
66
+
67
+ let (:form) { form_class.new(OpenStruct.new(:hits=>[OpenStruct.new], :song => OpenStruct.new)) }
68
+
69
+ it { form.song.class.model_name.must_be_kind_of ActiveModel::Name }
70
+ it { form.song.class.model_name.to_s.must_equal "Song" }
71
+ it "singularizes collection name" do
72
+ form.hits.first.class.model_name.to_s.must_equal "Hit"
73
+ end
74
+ end
33
75
  end
34
76
  end
35
77
 
data/test/reform_test.rb CHANGED
@@ -272,4 +272,21 @@ class ReadonlyAttributesTest < MiniTest::Spec
272
272
 
273
273
  hash.must_equal("country"=> "Germany")
274
274
  end
275
+ end
276
+
277
+ class OverridingAccessorsTest < MiniTest::Spec
278
+ class SongForm < Reform::Form
279
+ property :title
280
+
281
+ def title=(v)
282
+ super v.upcase
283
+ end
284
+ end
285
+
286
+
287
+ it "allows overriding accessors while keeping super" do
288
+ form = SongForm.new(OpenStruct.new)
289
+ form.validate("title" => "Hey Little World")
290
+ form.title.must_equal "HEY LITTLE WORLD"
291
+ end
275
292
  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: 0.2.3
4
+ version: 0.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nick Sutterer
@@ -9,22 +9,22 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-12-16 00:00:00.000000000 Z
12
+ date: 2013-12-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: representable
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  requirements:
18
- - - ~>
18
+ - - '>='
19
19
  - !ruby/object:Gem::Version
20
- version: 1.7.0
20
+ version: 1.7.5
21
21
  type: :runtime
22
22
  prerelease: false
23
23
  version_requirements: !ruby/object:Gem::Requirement
24
24
  requirements:
25
- - - ~>
25
+ - - '>='
26
26
  - !ruby/object:Gem::Version
27
- version: 1.7.0
27
+ version: 1.7.5
28
28
  - !ruby/object:Gem::Dependency
29
29
  name: activemodel
30
30
  requirement: !ruby/object:Gem::Requirement