Skip to content
Browse files

Refactoring the livevalidations.com adapter, now that we got this new…

… awesome generic system.
  • Loading branch information...
1 parent 1c4865a commit 05744fa2c8a44a2503e03eb818cc410946413e43 @augustl augustl committed Feb 28, 2009
View
2 lib/live_validations/adapter/validation_hook.rb
@@ -61,7 +61,7 @@ def format_regex
private
def recursively_merge_hashes(h1, h2)
- h1.merge!(h2) {|key, _old, _new| if _old.class == Hash then recursively_merge_hash(_old, _new) else _new end }
+ h1.merge!(h2) {|key, _old, _new| if _old.class == Hash then recursively_merge_hashes(_old, _new) else _new end }
end
def reset_data
View
47 lib/live_validations/adapters/livevalidation_dot_com.rb
@@ -3,62 +3,51 @@ module Adapters
# Adapter for http://www.livevalidation.com/ (d'oh)
class LivevalidationDotCom < LiveValidations::Adapter
setup do |v|
- v[:validators] = []
+ v[:validators] = Hash.new {|hash, key| hash[key] = {} }
end
validates :presence do |v, attribute|
- v[:validators] << %{
- var validator = new LiveValidation('#{v.prefix}_#{attribute}');
- validator.add(Validate.Presence, {failureMessage: "#{v.message_for(:blank)}"});
- }
+ v[:validators][attribute]['Presence'] = {:failureMessage => v.message_for(:blank)}
end
validates :format do |v, attribute|
- v[:validators] << %{
- var validator = new LiveValidation('#{v.prefix}_#{attribute}');
- validator.add(Validate.Format, {pattern: #{v.format_regex}, failureMessage: "#{v.message_for(:invalid)}"});
- }
+ # FIXME: The regexp outputs as a string, not a regex, in the javascripts.
+ v[:validators][attribute]['Format'] = {:pattern => v.format_regex, :failureMessage => v.message_for(:invalid)}
end
validates :numericality do |v, attribute|
- v[:validators] << %{
- var validator = new LiveValidation('#{v.prefix}_#{attribute}');
- validator.add(Validate.Numericality, {onlyInteger: true, failureMessage: "#{v.message_for(:not_a_number)}"})
- }
+ v[:validators][attribute]["Numericality"] = {:onlyInteger => true, :failureMessage => v.message_for(:not_a_number)}
end
validates :length do |v, attribute|
if v.callback.options[:minimum]
- v[:validators] << %{
- var validator = new LiveValidation('#{v.prefix}_#{attribute}');
- validator.add(Validate.Length, {minimum: #{v.callback.options[:minimum]}});
- }
+ v[:validators][attribute]["Length"] = {:minimum => v.callback.options[:minimum]}
end
if v.callback.options[:maximum]
- v[:validators] << %{
- var validator = new LiveValidation('#{v.prefix}_#{attribute}');
- validator.add(Validate.Length, {maximum: #{v.callback.options[:maximum]}});
- }
+ v[:validators][attribute]["Length"] = {:maximum => v.callback.options[:maximum]}
end
if v.callback.options[:within]
- v[:validators] << %{
- var validator = new LiveValidation('#{v.prefix}_#{attribute});
- validator.add(Validate.Length, {minimum: #{v.callback.options[:within].first}, maximum: #{v.callback.options[:within].last}});
+ v[:validators][attribute]["Length"] = {
+ :minimum => v.callback.options[:within].first,
+ :maximum => v.callback.options[:within].last
}
end
if v.callback.options[:is]
- v[:validators] << %{
- var validator = new LiveValidation('#{v.prefix}_#{attribute}');
- validator.add(Validate.Length, {is: #{v.callback.options[:is]}});
- }
+ v[:validators][attribute]["Length"] = {:is => v.callback.options[:is]}
end
end
renders_inline do |a|
- a[:validators].join
+ a[:validators].map do |attribute, options|
+ validators = options.map {|v, attrs| %{validator.add(Validate.#{v}, #{attrs.to_json});} }.join("\n")
+ %{
+ var validator = new LiveValidation('#{a.prefix}_#{attribute}');
+ #{validators}
+ }
+ end.join
end
end
end
View
28 test/live_validation_dot_com_controller_output_test.rb
@@ -0,0 +1,28 @@
+require File.join(File.dirname(__FILE__), "test_helper")
+
+class LiveValidationDotComControllerOutputTest < ActionController::TestCase
+ def setup
+ @controller = PostsController.new
+ @request = ActionController::TestRequest.new
+ @response = ActionController::TestResponse.new
+
+ LiveValidations.use(LiveValidations::Adapters::LivevalidationDotCom)
+
+ reset_callbacks Post
+ end
+
+ def teardown
+ restore_callbacks Post
+ end
+
+ def test_json_output
+ Post.validates_presence_of :title
+
+ get :new
+ assert_response :success
+
+ assert_select 'script[type=text/javascript]'
+ assert @response.body.include?(%{new LiveValidation('post_title');})
+ assert @response.body.include?(%{Validate.Presence, {"failureMessage": "blank"}})
+ end
+end
View
24 test/live_validation_dot_com_test.rb
@@ -12,41 +12,45 @@ def teardown
def test_presence
Post.validates_presence_of :title
- assert_json_output "Validate.Presence", "LiveValidation('post_title')"
+ assert_validators :title, "Presence"
end
def test_format
Post.validates_format_of :title, :with => /ohai/
- assert_json_output "Validate.Format", "/ohai/"
+ assert_validators :title, "Format", :pattern => "/ohai/"
end
def test_numericality
Post.validates_numericality_of :age
- assert_json_output "Validate.Numericality"
+ assert_validators :age, "Numericality"
end
-
+
def test_length_with_minimum
Post.validates_length_of :title, :minimum => 10
- assert_json_output "Validate.Length", "minimum: 10"
+ assert_validators :title, "Length", :minimum => 10
end
def test_length_with_maximum
Post.validates_length_of :title, :maximum => 10
- assert_json_output "Validate.Length", "maximum: 10"
+ assert_validators :title, "Length", :maximum => 10
end
def test_length_with_range
Post.validates_length_of :title, :within => 10..20
- assert_json_output "Validate.Length", "minimum: 10", "maximum: 20"
+ assert_validators :title, "Length", :minimum => 10, :maximum => 20
end
def test_length_with_is
Post.validates_length_of :title, :is => 20
- assert_json_output "Validate.Length", "is: 20"
+ assert_validators :title, "Length", :is => 20
end
- def assert_json_output(*outputs)
+ def assert_validators(attribute, expected_validator, json = {})
validator = LiveValidations.current_adapter.new(Post.new)
- outputs.each {|o| assert validator[:validators][0].include?(o) }
+ assert validator[:validators][attribute].has_key?(expected_validator), "The validator did not include `#{expected_validator}'."
+
+ json.each do |key, value|
+ assert_equal value, validator[:validators][attribute][expected_validator][key]
+ end
end
end

0 comments on commit 05744fa

Please sign in to comment.
Something went wrong with that request. Please try again.