Permalink
Browse files

Work in progress validates_uniqueness_of hook. Works! But the control…

…ler code needs to be fixed so that all adapters can use it, so there's no tests for the controller yet.
  • Loading branch information...
1 parent 0a993d0 commit 746d0004c6b344b7e7333e9956b20961f4788018 @augustl committed Feb 4, 2009
@@ -0,0 +1,13 @@
+class LiveValidationsController < ApplicationController
+ session :off
+
+ # Poll with /live_validations/uniqueness?model_class=User&column=username&value=theusername. Returns
+ # either 'true' or 'false'. This should probably be converted to some adapter specific code.
+ def uniqueness
+ model_class = params[:model_class].constantize
+ column = params[params[:model_class].downcase].keys.first
+ value = params[params[:model_class].downcase][column]
+
+ render :text => (model_class.count(:conditions => {column => value}) == 0)
+ end
+end
View
@@ -0,0 +1,3 @@
+ActionController::Routing::Routes.draw do |map|
+ map.connect 'live_validations/:action', :controller => 'live_validations'
+end
@@ -60,7 +60,8 @@ class JqueryValidations < LiveValidations::Adapter
end
validates :uniqueness do |v|
- # Next version. We need to do AJAX callbacks here.
+ model_class = v.adapter_instance.active_record_instance.class.name
+ v.json['remote'] = "/live_validations/uniqueness?model_class=#{model_class}"
end
json do |a|
@@ -88,6 +88,11 @@ def test_inclusion_as_range
assert_expected_json "post[title]" => {"range" => [5, 10]}
end
+ def test_uniqueness
+ Post.validates_uniqueness_of :title
+ assert_expected_json "post[title]" => {"remote" => "/live_validations/uniqueness?model_class=Post"}
+ end
+
def assert_expected_json(expected_json)
validator = LiveValidations::Adapters::JqueryValidations.new(Post.new)
assert_equal expected_json, validator.json_data

0 comments on commit 746d000

Please sign in to comment.