better_form 0.4.1 → 0.4.2

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.
@@ -45,39 +45,41 @@ module BetterForm
45
45
 
46
46
  def generate_validations(object, attribute)
47
47
  validations = {}
48
+ @attribute = attribute
48
49
 
49
50
  # Don't try to add validations for an object that doesn't have _validators
50
51
  return {} unless object.respond_to?(:_validators)
51
52
 
52
53
  # Iterate over each validator for this attribute, and add the appropriate HTML5 data-* attributes
53
54
  object._validators[attribute].each do |validator|
55
+ @options = validator.options
54
56
  validation = case validator
55
57
  when ActiveModel::Validations::AcceptanceValidator
56
- if validation_applies?(validator.options)
57
- { 'data-validates-acceptance' => true }
58
+ if validation_applies?
59
+ { 'data-validates-acceptance' => validation_message('must be accepted') }
58
60
  end
59
61
  when ActiveModel::Validations::ConfirmationValidator
60
- if validation_applies?(validator.options)
62
+ if validation_applies?
61
63
  {}
62
64
  end
63
65
  when ActiveModel::Validations::ExclusionValidator
64
66
  {}
65
67
  when ActiveModel::Validations::FormatValidator
66
- if validation_applies?(validator.options)
67
- { 'data-validates-format' => true, 'data-validates-format-with' => validator.options[:with].inspect }
68
+ if validation_applies?
69
+ { 'data-validates-format' => validation_message, 'data-validates-format-with' => validator.options[:with].inspect }
68
70
  end
69
71
  when ActiveModel::Validations::InclusionValidator
70
72
  {}
71
73
  when ActiveModel::Validations::LengthValidator
72
- if validation_applies?(validator.options)
74
+ if validation_applies?
73
75
  {}
74
76
  end
75
77
  when ActiveModel::Validations::NumericalityValidator
76
- if validation_applies?(validator.options)
77
- { 'data-validates-numericality' => true }
78
+ if validation_applies?
79
+ { 'data-validates-numericality' => validation_message('is not a number') }
78
80
  end
79
81
  when ActiveModel::Validations::PresenceValidator
80
- { 'data-validates-presence' => true }
82
+ { 'data-validates-presence' => validation_message('is required') }
81
83
  end
82
84
  validations.merge!(validation ||= {})
83
85
  end
@@ -85,12 +87,16 @@ module BetterForm
85
87
  validations
86
88
  end
87
89
 
88
- def validation_applies?(options)
89
- case options[:on]
90
+ def validation_applies?
91
+ case @options[:on]
90
92
  when :create then true if @object.new_record?
91
93
  when :update then true if !@object.new_record?
92
94
  else true
93
95
  end
94
96
  end
97
+
98
+ def validation_message(suffix = 'is invalid')
99
+ @options[:message] ? @options[:message] : "#{@attribute.to_s.humanize} #{suffix}"
100
+ end
95
101
  end
96
102
  end
@@ -1,3 +1,3 @@
1
1
  module BetterForm
2
- VERSION = "0.4.1"
2
+ VERSION = "0.4.2"
3
3
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: better_form
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.4.1
5
+ version: 0.4.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - Nicholas Firth-McCoy
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-03-17 00:00:00 +11:00
13
+ date: 2011-03-23 00:00:00 +11:00
14
14
  default_executable:
15
15
  dependencies: []
16
16
 
@@ -54,7 +54,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
54
54
  requirements: []
55
55
 
56
56
  rubyforge_project:
57
- rubygems_version: 1.5.0
57
+ rubygems_version: 1.6.2
58
58
  signing_key:
59
59
  specification_version: 3
60
60
  summary: A Better Rails 3 FormBuilder