From 86a4c9df37b5716b33a6bd65377e4b59621c6db1 Mon Sep 17 00:00:00 2001 From: August Lilleaas Date: Mon, 9 Mar 2009 20:15:48 +0100 Subject: [PATCH] You can now use strings and/or symbols to specify which adapter to use, e.g. LiveValidations.use :jquery_validations. --- README | 6 +++--- lib/live_validations.rb | 14 ++++++++++++-- test/general_adapter_test.rb | 24 ++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 5 deletions(-) diff --git a/README b/README index bef151d..9a60a83 100644 --- a/README +++ b/README @@ -10,14 +10,14 @@ Installation instructions The plugin is adapter based. There are currently adapters for the following libraries: -* http://bassistance.de/jquery-plugins/jquery-plugin-validation/ (LiveValidations::Adapters::JqueryValidations) -* http://livevalidation.com (LiveValidations::Adapters::LivevalidationDotCom) +* http://bassistance.de/jquery-plugins/jquery-plugin-validation/ (:jquery_validations) +* http://livevalidation.com (:livevalidation_dot_com) In parentheses is the class name of the adapter. When you have decided which adapter you want to use, do this: * Download the javascripts for the adapter you've chosen, and make sure to include them in your application layout. * Run "ruby script/plugin install git://github.com/leethal/live-validations.git" * Run "ruby script/generate live_validator" (skip this step if you're on 2.3) -* Add LiveValidations(the-class-name-of-the-adapter) in an initializer or at the bottom of environment.rb See the list above if you're unsure which class name you should use. +* Add LiveValidations.use(the-name-of-the-adapter) in an initializer or at the bottom of environment.rb. The list of adapters above shows the adapter names you can use h ere. * Add :live_validations => true to the forms you want live validations on. Example: <% form_for(@post, :live_validations => true) %> * There is no step 6! \ No newline at end of file diff --git a/lib/live_validations.rb b/lib/live_validations.rb index 4b52230..58563b6 100644 --- a/lib/live_validations.rb +++ b/lib/live_validations.rb @@ -1,17 +1,27 @@ module LiveValidations class AdapterNotSpecified < StandardError; end + class AdapterNotFound < StandardError; end # Set which adapter to use. Pass the adapter class directly. Example: # # LiveValidation.use(LiveValidations::Adapters::JQueryValidations) def use(adapter_klass) - self.current_adapter = adapter_klass + case adapter_klass + when String, Symbol + adapter_name = "LiveValidations::Adapters::" + adapter_klass.to_s.camelize + self.current_adapter = adapter_name.constantize + when Class + self.current_adapter = adapter_klass + end + + rescue NameError => e + raise AdapterNotFound, "The adapter `#{adapter_klass}' (#{adapter_name}) was not found." end def current_adapter adapter = @_current_adapter - return adapter || raise(AdapterNotSpecified, "Please specify an adapter with `LiveValidations.use(AdapterClassHere)'.") + return adapter || raise(AdapterNotSpecified, "Please specify an adapter with `LiveValidations.use :adapter_name'.") end def current_adapter=(adapter) diff --git a/test/general_adapter_test.rb b/test/general_adapter_test.rb index c6ded17..c170faf 100644 --- a/test/general_adapter_test.rb +++ b/test/general_adapter_test.rb @@ -59,4 +59,28 @@ def test_format_regex_using_ruby_regex @hook.expects(:callback).times(2).returns(@post.validation_callbacks.first) assert_equal /foo/, @hook.format_regex end + + def test_not_specifying_an_adapter + LiveValidations.current_adapter = nil + assert_raises(LiveValidations::AdapterNotSpecified) { LiveValidations.current_adapter } + end + + def test_specifying_adapter_as_class + LiveValidations.use(LiveValidations::Adapters::JqueryValidations) + assert_equal LiveValidations::Adapters::JqueryValidations, LiveValidations.current_adapter + end + + def test_specifying_adapter_as_string + LiveValidations.use("jquery_validations") + assert_equal LiveValidations::Adapters::JqueryValidations, LiveValidations.current_adapter + end + + def test_specifying_adapter_as_symbol + LiveValidations.use(:jquery_validations) + assert_equal LiveValidations::Adapters::JqueryValidations, LiveValidations.current_adapter + end + + def test_specifying_invalid_adapter_as_symbol + assert_raises(LiveValidations::AdapterNotFound) { LiveValidations.use(:meh) } + end end \ No newline at end of file