formeze 1.2.0 → 1.3.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
@@ -169,3 +169,36 @@ end
169
169
 
170
170
  Formeze will automatically define optional "utf8" and "authenticity_token"
171
171
  fields on every form so that you don't have to specify those manually.
172
+
173
+
174
+ Integration with I18n
175
+ ---------------------
176
+
177
+ Formeze integrates with [I18n](http://edgeguides.rubyonrails.org/i18n.html)
178
+ so that you can define custom error messages and field labels within your
179
+ locales (useful both for localization, and when working with designers).
180
+
181
+ For example, here is how you would change the "required" error message
182
+ (which defaults to "is required"):
183
+
184
+ ```yaml
185
+ # config/locales/en.yml
186
+ en:
187
+ formeze:
188
+ errors:
189
+ required: "cannot be blank"
190
+ ```
191
+
192
+ And here is an example of how you would set a custom label for fields named
193
+ "first_name" (for which the default label would be "First name"):
194
+
195
+ ```yaml
196
+ # config/locales/en.yml
197
+ en:
198
+ formeze:
199
+ labels:
200
+ first_name: "First Name"
201
+ ```
202
+
203
+ Labels defined in this way apply globally to all Formeze forms, but can be
204
+ overridden using the label field option which will take precedence.
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'formeze'
3
- s.version = '1.2.0'
3
+ s.version = '1.3.0'
4
4
  s.platform = Gem::Platform::RUBY
5
5
  s.authors = ['Tim Craft']
6
6
  s.email = ['mail@timcraft.com']
@@ -8,5 +8,6 @@ Gem::Specification.new do |s|
8
8
  s.description = 'A little library for handling form data/input'
9
9
  s.summary = 'See description'
10
10
  s.files = Dir.glob('{lib,spec}/**/*') + %w(README.md Rakefile.rb formeze.gemspec)
11
+ s.add_development_dependency('i18n', ['~> 0.6.0'])
11
12
  s.require_path = 'lib'
12
13
  end
@@ -18,22 +18,26 @@ module Formeze
18
18
  @name, @options = name, options
19
19
  end
20
20
 
21
- def validate(value, &error)
21
+ def validate(value, &block)
22
22
  if value !~ /\S/ # blank
23
- error.call(:'is required') if required?
23
+ block.call(error(:required, 'is required')) if required?
24
24
  else
25
- error.call(:'has too many lines') if !multiline? && value.lines.count > 1
25
+ block.call(error(:not_multiline, 'cannot contain newlines')) if !multiline? && value.lines.count > 1
26
26
 
27
- error.call(:'has too many characters') if value.chars.count > char_limit
27
+ block.call(error(:too_long, 'is too long')) if value.chars.count > char_limit
28
28
 
29
- error.call(:'has too many words') if word_limit? && value.scan(/\w+/).length > word_limit
29
+ block.call(error(:too_long, 'is too long')) if word_limit? && value.scan(/\w+/).length > word_limit
30
30
 
31
- error.call(:'is invalid') if pattern? && value !~ pattern
31
+ block.call(error(:no_match, 'is invalid')) if pattern? && value !~ pattern
32
32
 
33
- error.call(:'is invalid') if values? && !values.include?(value)
33
+ block.call(error(:bad_value, 'is invalid')) if values? && !values.include?(value)
34
34
  end
35
35
  end
36
36
 
37
+ def error(i18n_key, default)
38
+ translate(i18n_key, scope: [:formeze, :errors], default: default)
39
+ end
40
+
37
41
  def key
38
42
  @key ||= @name.to_s
39
43
  end
@@ -43,7 +47,11 @@ module Formeze
43
47
  end
44
48
 
45
49
  def label
46
- @label ||= @options.fetch(:label) { Label.new(name) }
50
+ if @options.has_key?(:label)
51
+ @options[:label]
52
+ else
53
+ translate(name, scope: [:formeze, :labels], default: Label.new(name))
54
+ end
47
55
  end
48
56
 
49
57
  def required?
@@ -101,6 +109,14 @@ module Formeze
101
109
  def defined_unless
102
110
  @options.fetch(:defined_unless)
103
111
  end
112
+
113
+ def translate(key, options)
114
+ if defined?(I18n)
115
+ I18n.translate(key, options)
116
+ else
117
+ options.fetch(:default)
118
+ end
119
+ end
104
120
  end
105
121
 
106
122
  module ArrayAttrAccessor
@@ -1,6 +1,7 @@
1
1
  require 'minitest/autorun'
2
2
 
3
- require 'formeze'
3
+ require_relative '../lib/formeze'
4
+ require 'i18n'
4
5
 
5
6
  class FormWithField
6
7
  Formeze.setup(self)
@@ -480,3 +481,29 @@ describe 'RailsForm' do
480
481
  end
481
482
  end
482
483
  end
484
+
485
+ describe 'I18n integration' do
486
+ before do
487
+ I18n.backend = I18n::Backend::Simple.new
488
+ end
489
+
490
+ after do
491
+ I18n.backend = I18n::Backend::Simple.new
492
+ end
493
+
494
+ it 'should be possible to override the default error messages' do
495
+ I18n.backend.store_translations :en, {formeze: {errors: {required: 'cannot be blank'}}}
496
+
497
+ form = FormWithField.new
498
+ form.parse('title=')
499
+ form.errors.first.to_s.must_equal('Title cannot be blank')
500
+ end
501
+
502
+ it 'should be possible to set labels globally' do
503
+ I18n.backend.store_translations :en, {formeze: {labels: {title: 'TITLE'}}}
504
+
505
+ form = FormWithField.new
506
+ form.parse('title=')
507
+ form.errors.first.to_s.must_equal('TITLE is required')
508
+ end
509
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: formeze
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,8 +9,24 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-14 00:00:00.000000000 Z
13
- dependencies: []
12
+ date: 2012-08-21 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: i18n
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 0.6.0
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 0.6.0
14
30
  description: A little library for handling form data/input
15
31
  email:
16
32
  - mail@timcraft.com
@@ -43,7 +59,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
43
59
  version: '0'
44
60
  requirements: []
45
61
  rubyforge_project:
46
- rubygems_version: 1.8.16
62
+ rubygems_version: 1.8.24
47
63
  signing_key:
48
64
  specification_version: 3
49
65
  summary: See description