Skip to content

Commit

Permalink
You can now use strings and/or symbols to specify which adapter to us…
Browse files Browse the repository at this point in the history
…e, e.g. LiveValidations.use :jquery_validations.
  • Loading branch information
augustl committed Mar 9, 2009
1 parent d9b356a commit 86a4c9d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
6 changes: 3 additions & 3 deletions README
Expand Up @@ -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!
14 changes: 12 additions & 2 deletions 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)
Expand Down
24 changes: 24 additions & 0 deletions test/general_adapter_test.rb
Expand Up @@ -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

0 comments on commit 86a4c9d

Please sign in to comment.