form_input 0.9.0.pre2 → 0.9.0

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.
data/README.md CHANGED
@@ -3120,14 +3120,14 @@ Trivial indeed, isn't it?
3120
3120
 
3121
3121
  ### Supported Locales
3122
3122
 
3123
- The `FormInput` currently includes translations of builtin error messages for the following locales:
3123
+ The `FormInput` currently includes translations of builtin error messages for the following languages:
3124
3124
 
3125
3125
  * English
3126
3126
  * Czech
3127
3127
  * Slovak
3128
3128
  * Polish
3129
3129
 
3130
- To add support for another locale,
3130
+ To add support for another language,
3131
3131
  simply copy the content of one of the most similar files found in the `form_input/r18n` directory
3132
3132
  to the appropriate translation file in your project,
3133
3133
  and translate it as you see fit.
data/form_input.gemspec CHANGED
@@ -4,7 +4,7 @@ require File.expand_path( '../lib/form_input/version', __FILE__ )
4
4
 
5
5
  Gem::Specification.new do |s|
6
6
  s.name = 'form_input'
7
- s.version = FormInput::Version::STRING + '.pre2'
7
+ s.version = FormInput::Version::STRING
8
8
  s.summary = 'Form helper which sanitizes, transforms, validates and encapsulates web request input.'
9
9
  s.description = <<EOT
10
10
  This gem allows you to describe your forms using a simple DSL
@@ -40,7 +40,7 @@ class FormInput
40
40
  end
41
41
 
42
42
  # Get string containing YAML representation of the default R18n translation for all/given FormInput classes.
43
- def self.default_translation(forms = self.forms)
43
+ def self.default_translation( forms = self.forms )
44
44
  hash = Hash[ forms.map{ |x| [ x.translation_name, x.translation_hash ] }.reject{ |k, v| v.empty? } ]
45
45
  YAML::dump( { forms: hash }.stringify_keys )
46
46
  end
data/test/test_core.rb CHANGED
@@ -190,6 +190,28 @@ describe FormInput do
190
190
  end
191
191
  end
192
192
 
193
+ should 'allow defining parameters by several means' do
194
+ c = Class.new( FormInput )
195
+ c.param :p
196
+ c.param! :rp
197
+ c.array :a
198
+ c.array! :ra
199
+ c.hash :h
200
+ c.hash! :rh
201
+ c.param :p2, hash: false
202
+ c.param :rp2, array: false, required: true
203
+ c.param :a2, array: true
204
+ c.param :ra2, array: true, required: true
205
+ c.param :h2, hash: true
206
+ c.param :rh2, hash: true, required: true
207
+ f = c.new
208
+ names( f.scalar_params ).should == [ :p, :rp, :p2, :rp2 ]
209
+ names( f.array_params ).should == [ :a, :ra, :a2, :ra2 ]
210
+ names( f.hash_params ).should == [ :h, :rh, :h2, :rh2 ]
211
+ names( f.optional_params ).should == [ :p, :a, :h, :p2, :a2, :h2 ]
212
+ names( f.required_params ).should == [ :rp, :ra, :rh, :rp2, :ra2, :rh2 ]
213
+ end
214
+
193
215
  should 'complain about incorrect parameter definition' do
194
216
  ->{ TestForm.param :x, "test", "test" }.should.raise( ArgumentError )
195
217
  ->{ TestForm.param :x, { type: :email }, :extra }.should.raise( ArgumentError )
@@ -375,6 +397,7 @@ describe FormInput do
375
397
 
376
398
  f = c.new( request( "?str=1.5&int=1.5&float=1.5&date=2011-12-31&time=31.12.2000+10:24:05&bool=true&str2=Abc&arr=a,b&hsh=1:2,3:4" ) )
377
399
  f.should.be.valid
400
+ names( f.blank_params ).should == [ :int2, :float2 ]
378
401
  f.to_h.should == f.to_hash
379
402
  f.to_hash.should == {
380
403
  str: "1.5",
@@ -428,6 +451,8 @@ describe FormInput do
428
451
  f.hsh.should == {}
429
452
  names( f.incorrect_params ).should == []
430
453
  names( f.invalid_params ).should == []
454
+ f.empty_params.should == f.params
455
+ f.blank_params.should == f.params
431
456
  f.to_hash.should == {}
432
457
  f.url_params.should == {}
433
458
  f.url_query.should == ""
@@ -612,6 +637,7 @@ describe FormInput do
612
637
  c.param :single, data: ->{ 2.times.map{ |i| [ i, ( 65 + i ).chr ] } }, class: Integer do to_i end
613
638
  c.array :multi, data: ->{ 4.times.map{ |i| [ i, ( 65 + i ).chr ] } }, class: Integer do to_i end
614
639
  c.param :x, filter: ->{ to_i }, class: Integer
640
+ c.hash :h
615
641
 
616
642
  f = c.new( single: 1, multi: [ 1, 3 ], x: 5 )
617
643
  f.should.be.valid
@@ -646,23 +672,46 @@ describe FormInput do
646
672
  p.form_name.should == "x"
647
673
  p.form_value.should == "5"
648
674
 
649
- f = c.new( request( "?single=0&multi[]=0&multi[]=2&x=3" ) ) ;
675
+ f = c.new( request( "?single=0&multi[]=0&multi[]=2&x=3" ) )
650
676
  f.should.be.valid
651
677
  f.to_hash.should == { single: 0, multi: [ 0, 2 ], x: 3 }
652
678
  f.url_params.should == { single: "0", multi: [ "0", "2" ], x: "3" }
653
679
  f.url_query.should == "single=0&multi[]=0&multi[]=2&x=3"
654
680
 
655
- f = c.new( request( "?single=5&multi[]=5" ) ) ;
681
+ f = c.new( request( "?single=5&multi[]=5" ) )
656
682
  f.should.be.valid
657
683
  f.to_hash.should == { single: 5, multi: [ 5 ] }
658
684
  f.url_params.should == { single: "5", multi: [ "5" ] }
659
685
  f.url_query.should == "single=5&multi[]=5"
660
686
 
661
- f = c.new( request( "" ) ) ;
687
+ f = c.new( request( "" ) )
662
688
  f.should.be.valid
663
689
  f.to_hash.should == {}
664
690
  f.url_params.should == {}
665
691
  f.url_query.should == ""
692
+
693
+ p = f.param( :single )
694
+ p.selected?( nil ).should.be.false
695
+ p.selected?( 0 ).should.be.false
696
+ p.selected?( 1 ).should.be.false
697
+ p.selected?( 2 ).should.be.false
698
+ p.selected?( 3 ).should.be.false
699
+
700
+ f = c.new( request( "?h[0]=" ) )
701
+ f.should.be.valid
702
+ f.to_hash.should == { h: { 0 => "" } }
703
+ f.url_params.should == { h: { "0" => "" } }
704
+ f.url_query.should == "h[0]="
705
+
706
+ p = f.param( :h )
707
+ p.data.should == []
708
+ p.code.should == :h
709
+ ->{ p.form_name }.should.raise ArgumentError
710
+ p.form_name( 0 ).should == "h[0]"
711
+ p.form_value.should == { "0" => "" }
712
+ p.selected?( nil ).should.be.false
713
+ p.selected?( 0 ).should.be.false
714
+ p.selected?( "" ).should.be.false
666
715
  end
667
716
 
668
717
  should 'classify parameters' do
@@ -1075,7 +1124,7 @@ describe FormInput do
1075
1124
  f.url_params.should == { q: s }
1076
1125
  f.url_query.should == "q=%FF"
1077
1126
 
1078
- f = TestForm.new( Rack::Request.new( Rack::MockRequest.env_for( "?q=%ff" ) ) )
1127
+ f = TestForm.new( request( "?q=%ff" ) )
1079
1128
  ->{ f.validate }.should.not.raise
1080
1129
  f.should.not.be.valid
1081
1130
  f.error_messages.should == [ "q must use valid encoding" ]
@@ -1085,6 +1134,15 @@ describe FormInput do
1085
1134
  f.url_query.should == "q=%FF"
1086
1135
  end
1087
1136
 
1137
+ should 'reject unexpected values in request input' do
1138
+ ->{ TestForm.new.import( q: "", opts: [], on: {} ) }.should.not.raise
1139
+ ->{ TestForm.new.import( q: [], opts: {}, on: "" ) }.should.not.raise
1140
+ ->{ TestForm.new.import( q: {}, opts: "", on: [] ) }.should.not.raise
1141
+ ->{ TestForm.new.import( q: 1 ) }.should.raise TypeError
1142
+ ->{ TestForm.new.import( q: :x ) }.should.raise TypeError
1143
+ ->{ TestForm.new.import( q: TestForm ) }.should.raise TypeError
1144
+ end
1145
+
1088
1146
  should 'make it easy to create URLs' do
1089
1147
  f = TestForm.new( query: "x", opts: [ 0, 0, 1 ], on: { 0 => 1 }, age: 10, email: nil, password: "", text: " " )
1090
1148
  f.url_params.should == { q: "x", age: "10", text: " ", opts: [ "0", "0", "1" ], on: { "0" => "1" } }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: form_input
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0.pre2
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Patrik Rak
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-06-21 00:00:00.000000000 Z
11
+ date: 2016-06-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack
@@ -79,7 +79,9 @@ executables: []
79
79
  extensions: []
80
80
  extra_rdoc_files: []
81
81
  files:
82
+ - ".codeclimate.yml"
82
83
  - ".gitignore"
84
+ - ".rubocop.yml"
83
85
  - ".travis.gemfile"
84
86
  - ".travis.yml"
85
87
  - ".yardopts"
@@ -148,9 +150,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
148
150
  version: 2.0.0
149
151
  required_rubygems_version: !ruby/object:Gem::Requirement
150
152
  requirements:
151
- - - ">"
153
+ - - ">="
152
154
  - !ruby/object:Gem::Version
153
- version: 1.3.1
155
+ version: '0'
154
156
  requirements: []
155
157
  rubyforge_project:
156
158
  rubygems_version: 2.4.5.1