Permalink
Browse files

Instead of making controller callbacks 2.3 only, I'm adding a generat…

…or so that pre 2.3 people (which is everyone at the moment) can use it. ./script/generate live_validator.
  • Loading branch information...
1 parent 11a982b commit dd13a2e9238437bea817b37da8a5c25cda9f536a @augustl committed Feb 26, 2009
@@ -1,6 +1,6 @@
class LiveValidationsController < ApplicationController
# 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.
+ # either 'true' or 'false'.
def uniqueness
responder = LiveValidations.current_adapter.validation_responses[:uniqueness]
render :text => responder.respond(params)
@@ -0,0 +1,28 @@
+class LiveValidatorGenerator < Rails::Generator::Base
+ def manifest
+ record do |m|
+ m.directory 'app/controllers'
+ m.template 'controller_template.rb', "app/controllers/live_validations_controller.rb"
+ m.route(%{map.connect "live_validations/:action", :controller => "live_validations"})
+ end
+ end
+end
+
+# Hijacked from restful_authentication
+Rails::Generator::Commands::Create.class_eval do
+ def route(spec)
+ logger.route(spec)
+ sentinel = 'ActionController::Routing::Routes.draw do |map|'
+ gsub_file 'config/routes.rb', /(#{Regexp.escape(sentinel)})/mi do |match|
+ %{#{match}\n #{spec}}
+ end
+ end
+end
+
+Rails::Generator::Commands::Destroy.class_eval do
+ def route(spec)
+ look_for = %{\n #{spec}}
+ logger.route(spec)
+ gsub_file "config/routes.rb", /(#{look_for})/mi, ""
+ end
+end
@@ -0,0 +1,6 @@
+class LiveValidationsController < ApplicationController
+ def uniqueness
+ responder = LiveValidations.current_adapter.validation_responses[:uniqueness]
+ render :text => responder.respond(params)
+ end
+end
@@ -37,15 +37,6 @@ def self.form_for_options(&block)
@form_for_options_proc = block
end
- # Controller hooks are only supported in 2.3 and above.
- def self.supports_controller_hooks?
- if Rails::VERSION::MAJOR == 2
- return Rails::VERSION::MINOR >= 3
- else
- return Rails::VERSION::MAJOR > 2
- end
- end
-
def render_json
self.class.json_proc.call(self)
end
@@ -73,18 +73,16 @@ class JqueryValidations < LiveValidations::Adapter
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|
- column = r.params[r.params[:model_class].downcase].keys.first
- value = r.params[r.params[:model_class].downcase][column]
- r.params[:model_class].constantize.count(:conditions => {column => value}) == 0
- end
+ 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|
+ column = r.params[r.params[:model_class].downcase].keys.first
+ value = r.params[r.params[:model_class].downcase][column]
+ r.params[:model_class].constantize.count(:conditions => {column => value}) == 0
end
json do |a|
@@ -1,32 +1,7 @@
require File.join(File.dirname(__FILE__), "test_helper")
class GeneralAdapterTest < ActiveSupport::TestCase
- def test_supports_controller_hooks
- Rails::VERSION.instance_eval {
- remove_const('MAJOR')
- remove_const('MINOR')
- const_set('MAJOR', 1)
- const_set('MINOR', 2)
- }
-
- assert !LiveValidations::Adapter.supports_controller_hooks?
-
- Rails::VERSION.instance_eval {
- remove_const('MAJOR')
- remove_const('MINOR')
- const_set('MAJOR', 2)
- const_set('MINOR', 3)
- }
-
- assert LiveValidations::Adapter.supports_controller_hooks?
-
- Rails::VERSION.instance_eval {
- remove_const('MAJOR')
- remove_const('MINOR')
- const_set('MAJOR', 3)
- const_set('MINOR', 0)
- }
-
- assert LiveValidations::Adapter.supports_controller_hooks?
+ def test_whoops
+ # There should probably be some tests here. Probably.
end
end

0 comments on commit dd13a2e

Please sign in to comment.