Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Renaming and cleaning up a few things. No real changes.

* Renaming the internal `perform_validations` method to `run_validations`.
* Cleaning up the attribute hook code in the form builder.
* Improved the error message when passing a invalid form builder object to <% form_for(the_object, :live_validations => true) %>
  • Loading branch information...
commit 7f898ebd62e999f85c9a523816a5bc6d547680d8 1 parent feab301
@augustl authored
View
22 lib/live_validations/adapter.rb
@@ -1,13 +1,11 @@
module LiveValidations
# The base class of an adapter.
class Adapter
- attr_reader :data, :active_record_instance, :visible_attributes
+ attr_reader :data, :active_record_instance, :rendered_attributes
def initialize(active_record_instance)
@active_record_instance = active_record_instance
-
- # The list of attributes that the form is displaying.
- @visible_attributes = []
+ @rendered_attributes = []
# Initialize the data hash with the 'setup' call from
# the validator.
@@ -15,7 +13,7 @@ def initialize(active_record_instance)
self.class.setup_proc.call(self)
end
- def perform_validations
+ def run_validations
active_record_instance.validation_callbacks.each do |callback|
next unless callback_has_visible_attributes?(callback)
@@ -29,6 +27,18 @@ def perform_validations
end
end
+ # Is called whenever a field helper is called (<%= f.foo %>).
+ def renders_attribute(attribute)
+ @rendered_attributes << attribute
+ end
+
+ # Tells the form builder wether or not the adapter will
+ # alter tag attributes in the generated forms. Overrided
+ # per-adapter.
+ def alters_tag_attributes?
+ false
+ end
+
def [](key)
data[key]
end
@@ -78,7 +88,7 @@ def handle_form_for_options(options)
private
def callback_has_visible_attributes?(callback)
- callback.options[:attributes] && callback.options[:attributes].any? {|attribute| @visible_attributes.include?(attribute)}
+ callback.options[:attributes] && callback.options[:attributes].any? {|attribute| rendered_attributes.include?(attribute)}
end
def self.validation_hooks
View
4 lib/live_validations/adapters/validatious.rb
@@ -2,6 +2,10 @@ module LiveValidations
module Adapters
# Adapter for http://www.validatious.org/
class Validatious < LiveValidations::Adapter
+ def alters_tag_attributes?
+ true
+ end
+
setup do |v|
v[:tag_attributes] = Hash.new {|hash, key| hash[key] = {} }
end
View
20 lib/live_validations/form_builder.rb
@@ -11,14 +11,11 @@ def self.included(base)
helpers_with_one_option_hash.each do |helper|
define_method("#{helper}_with_live_validations") do |attribute, *args|
- if @template.adapter_instance
- tag_attributes = @template.adapter_instance[:tag_attributes] && @template.adapter_instance[:tag_attributes][attribute]
- @template.adapter_instance.visible_attributes << attribute
- end
+ @template.adapter_instance.renders_attribute(attribute) if @template.adapter_instance
- if tag_attributes
+ if @template.adapter_instance && @template.adapter_instance.alters_tag_attributes?
options = args.extract_options!
- options.merge!(tag_attributes)
+ options.merge!(@template.adapter_instance[:tag_attributes])
__send__("#{helper}_without_live_validations", attribute, *(args << options))
else
__send__("#{helper}_without_live_validations", attribute, *args)
@@ -30,24 +27,21 @@ def self.included(base)
helpers_with_two_option_hashes.each do |helper|
define_method("#{helper}_with_live_validations") do |attribute, *args|
- if @template.adapter_instance
- tag_attributes = @template.adapter_instance[:tag_attributes] && @template.adapter_instance[:tag_attributes][attribute]
- @template.adapter_instance.visible_attributes << attribute
- end
+ @template.adapter_instance.renders_attribute(attribute) if @template.adapter_instance
- if tag_attributes
+ if @template.adapter_instance && @template.adapter_instance.alters_tag_attributes?
# We have both options and html_options
if args[-1].is_a?(Hash) && args[-2].is_a?(Hash)
html_options = args.pop
options = args.pop
- html_options.merge!(tag_attributes)
+ html_options.merge!(@template.adapter_instance[:tag_attributes])
args << options
args << html_options
__send__("#{helper}_without_live_validations", *args)
# No html_options was specified
else
- html_options = tag_attributes
+ html_options = @template.adapter_instance[:tag_attributes]
args << {} unless args[-1].is_a?(Hash)
args << html_options
__send__("#{helper}_without_live_validations", attribute, *args)
View
4 lib/live_validations/view_helpers.rb
@@ -21,14 +21,14 @@ def form_for_with_live_validations(record_name_or_array, *args, &block)
raise InvalidFormBuilderObject, "`form_for(:some_symbol, :live_validations => true)` requires the instance variable `@some_symbol` to be assigned, and point to an active record instance."
end
else
- raise InvalidFormBuilderObject, "`form_for(x, :live_validotions => true)` requires `x` to be an array (e.g. [:admin, @post]) or an active record instance (e.g. @post) as its first argument. Got an instance of `#{record_name_or_array.class}`."
+ raise InvalidFormBuilderObject, "`form_for(x, :live_validotions => true)` requires `x` to be an array (e.g. [:admin, @post]), a symbol matching an instance variable name pointing to an active record instance (e.g. :post when @post has been set) or an active record instance (e.g. @post) as its first argument. Got an instance of `#{record_name_or_array.class}`."
end
self.adapter_instance = LiveValidations.current_adapter.new(record)
adapter_instance.handle_form_for_options(options)
form_for_without_live_validations(record_name_or_array, *(args << options), &block)
- adapter_instance.perform_validations
+ adapter_instance.run_validations
silence_warnings do
concat(%{<script type="text/javascript">#{adapter_instance.render_inline_javascript}</script>}, block.binding) if adapter_instance.utilizes_inline_javascript?
View
14 test/general_adapter_test.rb
@@ -103,6 +103,18 @@ def test_with_validation_that_has_no_attribute
# This test should probably be improved. The plugin used to raise an error when
# the model had attribute-less validations, which is what this tests tries to
# prove.
- assert_nothing_raised { adapter.perform_validations }
+ assert_nothing_raised { adapter.run_validations }
+ end
+
+ def test_various_types_of_form_helpers
+ render <<-eof
+ <% @post = Post.new %>
+ <% form_for :post, :live_validations => true do |f| %>
+ <%= f.text_field :title %>
+ <%= f.select :title, ["foo", "bar", "baz"] %>
+ <% end %>
+ eof
+
+ assert_html "script[type=text/javascript]"
end
end
View
8 test/jquery_validations_test.rb
@@ -25,7 +25,7 @@ def test_validates_format_of_without_custom_javascript_format
Post.validates_format_of :title, :with => /^[a-z0-9]+$/
validator = LiveValidations::Adapters::JqueryValidations.new(Post.new)
validator.expects(:callback_has_visible_attributes?).returns(true)
- validator.perform_validations
+ validator.run_validations
custom_validator_key = validator.render_inline_javascript[/jQuery\.validator\.addMethod\('([a-f0-9]+)/, 1]
expected_json = {:title => {custom_validator_key => true}}
@@ -36,7 +36,7 @@ def test_validates_format_of_with_custom_javascript_format
Post.validates_format_of :title, :with => /^some odd ruby specific thing$/, :live_validator => '[a-z0-9]'
validator = LiveValidations::Adapters::JqueryValidations.new(Post.new)
validator.expects(:callback_has_visible_attributes?).returns(true)
- validator.perform_validations
+ validator.run_validations
assert validator.render_inline_javascript.include?("[a-z0-9]")
assert !validator.render_inline_javascript.include?('some odd ruby specific thing')
@@ -82,7 +82,7 @@ def test_uniqueness
def assert_expected_json(expected_json)
validator = LiveValidations::Adapters::JqueryValidations.new(Post.new)
validator.expects(:callback_has_visible_attributes?).returns(true)
- validator.perform_validations
+ validator.run_validations
assert_equal expected_json, validator[:validators]
end
@@ -90,7 +90,7 @@ def assert_expected_json(expected_json)
def assert_custom_validator(attribute)
validator = LiveValidations::Adapters::JqueryValidations.new(Post.new)
validator.expects(:callback_has_visible_attributes?).returns(true)
- validator.perform_validations
+ validator.run_validations
custom_validator_key = validator.render_inline_javascript[/jQuery\.validator\.addMethod\('([a-f0-9]+)/, 1]
assert custom_validator_key, "The custom validator key was not found."
View
2  test/live_validation_dot_com_test.rb
@@ -84,7 +84,7 @@ def test_confirmation
def assert_validators(attribute, expected_validator, json = {})
validator = LiveValidations.current_adapter.new(Post.new)
validator.expects(:callback_has_visible_attributes?).returns(true)
- validator.perform_validations
+ validator.run_validations
assert validator[:validators][attribute].has_key?(expected_validator), "The validator did not include `#{expected_validator}'."
View
2  test/validatious_test.rb
@@ -24,7 +24,7 @@ def test_confirmation
def assert_expected_attributes_data(expected_attributes_data)
validator = LiveValidations::Adapters::Validatious.new(Post.new)
validator.expects(:callback_has_visible_attributes?).returns(true)
- validator.perform_validations
+ validator.run_validations
assert_equal expected_attributes_data, validator[:tag_attributes]
end
Please sign in to comment.
Something went wrong with that request. Please try again.