Browse files

Is now aware of custom error messages, and uses built-in active recor…

…d messages instead of the validation library defaults.
  • Loading branch information...
1 parent 8835574 commit 49eb7679389b8b0d6604edc66fb4539dfff8757c @augustl committed Feb 15, 2009
View
3 lib/live_validations/adapter.rb
@@ -2,12 +2,13 @@ module LiveValidations
# The base class of an adapter.
class Adapter
- attr_reader :json, :tag_attributes, :extras, :active_record_instance
+ attr_reader :json, :tag_attributes, :extras, :messages, :active_record_instance
def initialize(active_record_instance)
@json = Hash.new {|hash, key| hash[key] = {} }
@tag_attributes = Hash.new {|hash, key| hash[key] = {} }
@extras = Hash.new {|hash, key| hash[key] = [] }
+ @messages = Hash.new {|hash, key| hash[key] = {} }
@active_record_instance = active_record_instance
active_record_instance.validation_callbacks.each do |callback|
View
8 lib/live_validations/adapter/validation_hook.rb
@@ -2,7 +2,7 @@ module LiveValidations
class Adapter
# The internal representation of each of the 'validates' blocks in the adapter implementation.
class ValidationHook
- attr_reader :json, :tag_attributes, :callback, :prefix, :adapter_instance
+ attr_reader :json, :tag_attributes, :messages, :callback, :prefix, :adapter_instance
def initialize(&block)
@proc = block
@@ -18,16 +18,22 @@ def run_validation(adapter_instance, callback)
json.each do |attribute, rules|
@adapter_instance.json["#{prefix}[#{attribute}]"].merge!(rules)
end
+
tag_attributes.each do |attribute, rules|
@adapter_instance.tag_attributes[attribute.to_sym].merge!(rules)
end
+
+ messages.each do |attribute, message|
+ @adapter_instance.messages["#{prefix}[#{attribute}]"].merge!(message)
+ end
end
private
def reset_data
@json = Hash.new {|hash, key| hash[key] = {} }
@tag_attributes = Hash.new {|hash, key| hash[key] = {} }
+ @messages = Hash.new {|hash, key| hash[key] = {} }
@prefix = @adapter_instance.active_record_instance.class.name.downcase
end
View
16 lib/live_validations/adapters/jquery_validations.rb
@@ -4,10 +4,12 @@ module Adapters
class JqueryValidations < LiveValidations::Adapter
validates :presence do |v, attribute|
v.json[attribute]['required'] = true
+ v.messages[attribute]['required'] = v.callback.options[:message] || I18n.translate('activerecord.errors.messages')[:blank]
end
validates :acceptance do |v, attribute|
v.json[attribute]['required'] = true
+ v.messages[attribute]['required'] = v.callback.options[:message] || I18n.translate('activerecord.errors.messages')[:accepted]
end
validates :length do |v, attribute|
@@ -39,16 +41,20 @@ class JqueryValidations < LiveValidations::Adapter
when Range
v.json[attribute]['range'] = [enum.first, enum.last]
when Array
- add_custom_rule(v, attribute, Digest::SHA1.hexdigest(enum.inspect), "var list = #{enum.to_json}; for (var i=0; i<list.length; i++){if(list[i] == value) { return true; }}", I18n.translate('activerecord.errors.messages')[:inclusion])
+ add_custom_rule(v, attribute, Digest::SHA1.hexdigest(enum.inspect), "var list = #{enum.to_json}; for (var i=0; i<list.length; i++){if(list[i] == value) { return true; }}", v.callback.options[:message] || I18n.translate('activerecord.errors.messages')[:inclusion])
end
end
+
+ # TODO: Exclusion. DRY!!111
validates :numericality do |v, attribute|
v.json[attribute]['digits'] = true
+ v.messages[attribute]['digits'] = v.callback.options[:message] || I18n.translate('activerecord.errors.messages')[:not_a_number]
end
validates :confirmation do |v, attribute|
v.json["#{attribute}_confirmation"]['equalTo'] = "##{v.prefix}_#{attribute}"
+ v.messages["#{attribute}_confirmation"]['equalTo'] = v.callback.options[:message] || I18n.translate('activerecord.errors.messages')[:confirmation]
end
validates :format do |v, attribute|
@@ -62,13 +68,14 @@ class JqueryValidations < LiveValidations::Adapter
# TODO: handle multiline as well
end
- add_custom_rule(v, attribute, Digest::SHA1.hexdigest(js_regex), "return #{js_regex}.test(value)", I18n.translate('activerecord.errors.messages')[:invalid])
+ add_custom_rule(v, attribute, Digest::SHA1.hexdigest(js_regex), "return #{js_regex}.test(value)", v.callback.options[:message] || I18n.translate('activerecord.errors.messages')[:invalid])
end
if supports_controller_hooks?
validates :uniqueness do |v, attribute|
model_class = v.adapter_instance.active_record_instance.class.name
v.json[attribute]['remote'] = "/live_validations/uniqueness?model_class=#{model_class}"
+ v.messages[attribute]['remote'] = v.callback.options[:message] || I18n.translate('activerecord.errors.messages')[:taken]
end
response :uniqueness do |r|
@@ -82,7 +89,10 @@ class JqueryValidations < LiveValidations::Adapter
dom_id = ActionController::RecordIdentifier.dom_id(a.active_record_instance)
%{
#{custom_rules(a)}
- $('##{dom_id}').validate(#{{'rules' => a.json}.to_json})
+ $('##{dom_id}').validate(#{{
+ 'rules' => a.json,
+ 'messages' => a.messages,
+ }.to_json})
}
end
View
10 test/jquery_validations_controller_output_test.rb
@@ -21,6 +21,14 @@ def test_json_output
assert_select 'script[type=text/javascript]'
assert @response.body.include?("$('#new_post').validate")
- assert @response.body.include?({'rules' => {'post[title]' => {'required' => true}}}.to_json)
+ expected_json = {
+ "messages" => {
+ "post[title]" => {"required" => "can't be blank"}
+ },
+ "rules" => {
+ "post[title]" => {"required" => true}
+ }
+ }
+ assert @response.body.include?(expected_json.to_json)
end
end

0 comments on commit 49eb767

Please sign in to comment.