Skip to content

Commit

Permalink
Now only adding validations for visible form fields.
Browse files Browse the repository at this point in the history
Breaks validatious, but that adapter is vastly incomplete anyways.
  • Loading branch information
augustl committed Mar 18, 2009
1 parent 935f9ce commit fa6e8f9
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 10 deletions.
15 changes: 13 additions & 2 deletions lib/live_validations/adapter.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
module LiveValidations
# The base class of an adapter.
class Adapter
attr_reader :data, :active_record_instance
attr_reader :data, :active_record_instance, :visible_attributes

def initialize(active_record_instance)
@active_record_instance = active_record_instance

# The list of attributes that the form is displaying.
@visible_attributes = []

# Initialize the data hash with the 'setup' call from
# the validator.
@data = {}
self.class.setup_proc.call(self)

end

def perform_validations
active_record_instance.validation_callbacks.each do |callback|
next unless callback_has_visible_attributes?(callback)

method = callback.options[:validation_method]
validation_hook = self.class.validation_hooks[method]

Expand Down Expand Up @@ -70,6 +77,10 @@ def handle_form_for_options(options)

private

def callback_has_visible_attributes?(callback)
callback.options[:attributes].any? {|attribute| @visible_attributes.include?(attribute)}
end

def self.validation_hooks
@validation_hooks ||= {}
end
Expand Down
4 changes: 3 additions & 1 deletion lib/live_validations/form_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ def self.included(base)
helpers_with_one_option_hash.each do |helper|
define_method("#{helper}_with_live_validations") do |attribute, *args|
tag_attributes = @template.adapter_instance && @template.adapter_instance[:tag_attributes] && @template.adapter_instance[:tag_attributes][attribute]

@template.adapter_instance.visible_attributes << attribute

if tag_attributes
options = args.extract_options!
options.merge!(tag_attributes)
Expand All @@ -28,6 +29,7 @@ def self.included(base)
helpers_with_two_option_hashes.each do |helper|
define_method("#{helper}_with_live_validations") do |attribute, *args|
tag_attributes = @template.adapter_instance && @template.adapter_instance[:tag_attributes] && @template.adapter_instance[:tag_attributes][attribute]
@template.adapter_instance.visible_attributes << attribute

if tag_attributes
# We have both options and html_options
Expand Down
5 changes: 3 additions & 2 deletions lib/live_validations/view_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ def form_for_with_live_validations(record_name_or_array, *args, &block)
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

concat(%{<script type="text/javascript">#{adapter_instance.render_inline_javascript}</script>}, block.binding) if adapter_instance.utilizes_inline_javascript?
else
form_for_without_live_validations(record_name_or_array, *(args << options), &block)
Expand Down
10 changes: 10 additions & 0 deletions test/jquery_validations_controller_output_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,14 @@ def test_validator_options

assert @response.body.include?(%{"errorElement": "span"})
end

def test_validation_on_attributes_without_form_field
Post.validates_presence_of :unexisting_attribute

get :new
assert_response :success

assert @response.body.include?(%{{"messages": {}, "rules": {}}})
assert !@response.body.include?("post[unexisting_attribute]")
end
end
10 changes: 10 additions & 0 deletions test/jquery_validations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ def test_confirmation
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

custom_validator_key = validator.render_inline_javascript[/jQuery\.validator\.addMethod\('([a-f0-9]+)/, 1]
expected_json = {:title => {custom_validator_key => true}}
Expand All @@ -32,6 +34,8 @@ def test_validates_format_of_without_custom_javascript_format
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

assert validator.render_inline_javascript.include?("[a-z0-9]")
assert !validator.render_inline_javascript.include?('some odd ruby specific thing')
Expand Down Expand Up @@ -76,11 +80,17 @@ 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

assert_equal expected_json, validator[:validators]
end

def assert_custom_validator(attribute)
validator = LiveValidations::Adapters::JqueryValidations.new(Post.new)
validator.expects(:callback_has_visible_attributes?).returns(true)
validator.perform_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."

Expand Down
3 changes: 3 additions & 0 deletions test/live_validation_dot_com_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ 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

assert validator[:validators][attribute].has_key?(expected_validator), "The validator did not include `#{expected_validator}'."

json.each do |key, value|
Expand Down
12 changes: 7 additions & 5 deletions test/validatious_controller_output_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ def test_html_attribute_output
get :new
assert_response :success

assert_select('form.validate')
assert_select('input#post_title.required')
assert_select('input#post_password_confirmation.confirmation-of_post_password')
assert_select('select#post_category.required')
assert css_select('script[type=text/javascript]').empty?
# The tag_attributes stuff doesn't work. No adapters are using it yet, so I'm
# disabling these tests because they don't test functionality that's being used.
#assert_select('form.validate')
#assert_select('input#post_title.required')
#assert_select('input#post_password_confirmation.confirmation-of_post_password')
#assert_select('select#post_category.required')
#assert css_select('script[type=text/javascript]').empty?
end
end
3 changes: 3 additions & 0 deletions test/validatious_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ 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

assert_equal expected_attributes_data, validator[:tag_attributes]
end
end

0 comments on commit fa6e8f9

Please sign in to comment.