validatious-on-rails 0.4.3 → 0.4.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -18,11 +18,14 @@ v2.Rails.performRemoteValidation = function performRemoteValidation(name, field,
18
18
  var field_element = field.__elements[0];
19
19
  var url = v2.Rails.remoteValidationUrlFor(name, field_element, value, params);
20
20
 
21
+ v2.Rails.initializeLastResult(name, field_element.id);
22
+
21
23
  var xmlHttpRequest = new XMLHttpRequest;
22
24
  xmlHttpRequest.open('GET', url, true);
23
25
  xmlHttpRequest.onreadystatechange = function() {
24
26
  if (this.readyState == XMLHttpRequest.DONE) {
25
27
  var validationResult = (this.responseText == 'true' || this.responseText == '1') ? true : false;
28
+ v2.Rails.lastRemoteValidationResult[name][field_element.id] = validationResult;
26
29
  /* console.log('Validation result: ' + validationResult); */
27
30
 
28
31
  /* Get all validators for this field, except the current validator. */
@@ -36,7 +39,23 @@ v2.Rails.performRemoteValidation = function performRemoteValidation(name, field,
36
39
  };
37
40
  };
38
41
  xmlHttpRequest.send(null);
39
- return true;
42
+ return v2.Rails.lastRemoteValidationResult[name][field_element.id];
43
+ };
44
+
45
+ /**
46
+ * Initialize data structure for holding info about last remote AJAX validation result.
47
+ * We need this to make Validatious play well with remote validations.
48
+ */
49
+ v2.Rails.initializeLastResult = function initializeLastResult(validator_name, field_id) {
50
+ if (typeof v2.Rails.lastRemoteValidationResult == 'undefined') {
51
+ v2.Rails.lastRemoteValidationResult = new Array();
52
+ }
53
+ if (typeof v2.Rails.lastRemoteValidationResult[validator_name] == 'undefined') {
54
+ v2.Rails.lastRemoteValidationResult[validator_name] = new Array();
55
+ };
56
+ if (typeof v2.Rails.lastRemoteValidationResult[validator_name][field_id] == 'undefined') {
57
+ v2.Rails.lastRemoteValidationResult[validator_name][field_id] = false;
58
+ };
40
59
  };
41
60
 
42
61
  /**
@@ -34,7 +34,7 @@ module ValidatiousOnRails
34
34
 
35
35
  if validator_klass.present?
36
36
  # Perform validation.
37
- if record = ((params[:id].present? && params[:id] > 0) ? record_klass.find(params[:id]) : record_klass.new)
37
+ if record = (((params[:id].present? && params[:id].to_i > 0) ? record_klass.find(params[:id]) : record_klass.new) rescue nil)
38
38
  validation_result = validator_klass.perform_validation(record, params[:attribute].to_sym, params[:value], params)
39
39
  ::ValidatiousOnRails.log "#{validator_klass} validation result: #{validation_result.to_s.upcase}. #{record_klass}##{params[:attribute]} => #{params[:value].inspect}", :info
40
40
  # validation_result = (validation_result ? 1 : 0)
@@ -77,8 +77,7 @@ module ValidatiousOnRails
77
77
  else
78
78
  object_or_class
79
79
  end
80
- # FIXME: Forgot how to check this on the class instead of an instance of the class.
81
- return validators unless klass.new.is_a?(::ActiveRecord::Base)
80
+ return validators unless klass.respond_to?(:reflect_on_validations_for)
82
81
  rescue
83
82
  ::ValidatiousOnRails.log "Missing constant: #{object_or_class}", :debug
84
83
  return validators
@@ -16,19 +16,6 @@ module ActionView # :nodoc:
16
16
  define_with_validatious_support(field_type)
17
17
  end
18
18
 
19
- # Adds the title attribute to label tags when there is no title
20
- # set, and the label text is provided. The title is set to object_name.humanize
21
- #
22
- def label_with_title(object_name, method, text = nil, options = {})
23
- begin
24
- options[:title] ||= object_name.to_s.classify.constantize.human_attribute_name(method.to_s) unless text.nil?
25
- rescue
26
- # skip
27
- end
28
- label_without_title(object_name, method, text, options)
29
- end
30
- alias_method_chain :label, :title
31
-
32
19
  end
33
20
 
34
21
  module FormOptionsHelper # :nodoc:
@@ -230,26 +230,6 @@ class FormHelperTest < ::ActionView::TestCase
230
230
  # TODO: The other validators...
231
231
  end
232
232
 
233
- # TODO: Re-visit label - needed still?
234
- context "label" do
235
- test "regular" do
236
- # Using helper
237
- assert_equal "<label for=\"bogus_item_name\">Name</label>", label(:bogus_item, :name)
238
- end
239
-
240
- test "with title" do
241
- # Using helper
242
- assert_equal "<label for=\"bogus_item_name\" title=\"craaazy\">Name</label>",
243
- label(:bogus_item, :name, nil, :title => "craaazy")
244
- end
245
-
246
- test "without title" do
247
- # Using helper
248
- assert_equal "<label for=\"bogus_item_name\" title=\"Name\">Your name</label>",
249
- label(:bogus_item, :name, "Your name")
250
- end
251
- end
252
-
253
233
  private
254
234
 
255
235
  def protect_against_forgery?
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: validatious-on-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.3
4
+ version: 0.4.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jonas Grimfelt