guardsjs-rails 1.2.1 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * Guards JavaScript jQuery Plugin v1.2.1
2
+ * Guards JavaScript jQuery Plugin v1.3.0
3
3
  * https://github.com/on-site/guards.js
4
4
  *
5
5
  * Copyright 2010-2014, On-Site.com, http://www.on-site.com/
@@ -8,7 +8,7 @@
8
8
  * Includes code for email and phone number validation from the jQuery
9
9
  * Validation plugin. http://docs.jquery.com/Plugins/Validation
10
10
  *
11
- * Date: Wed Mar 12 12:25:31 2014 -0400
11
+ * Date: Wed Apr 9 15:06:00 2014 -0700
12
12
  */
13
13
 
14
14
  /**
@@ -48,7 +48,7 @@
48
48
  return $.guards.add(selector);
49
49
  };
50
50
 
51
- $.guard.version = "1.2.1";
51
+ $.guard.version = "1.3.0";
52
52
 
53
53
  $.Guards = function() {
54
54
  var self = this;
@@ -255,7 +255,7 @@
255
255
  * </div>
256
256
  * </div>
257
257
  */
258
- this.name("dateUS").using(this.aggregate(this.isAllValid, this.isValidDateUS)).message("Please use: dd/mm/yyyy.")
258
+ this.name("dateUS").using(this.aggregate(this.isAllValid, this.isValidDateUS)).message("Please use: dd/mm/yyyy.");
259
259
 
260
260
  /**
261
261
  * @page Named Guards
@@ -280,7 +280,7 @@
280
280
  * </div>
281
281
  * </div>
282
282
  */
283
- this.name("timeUS").using(this.aggregate(this.isAllValid, this.isValidTimeUS)).message("Please use: hh:mm&nbsp;am/pm.")
283
+ this.name("timeUS").using(this.aggregate(this.isAllValid, this.isValidTimeUS)).message("Please use: hh:mm&nbsp;am/pm.");
284
284
 
285
285
  /**
286
286
  * @page Named Guards
@@ -674,7 +674,7 @@
674
674
  * This version of guards.js library as a string, like <code>"1.0.0"</code>.
675
675
  * </p>
676
676
  */
677
- $.Guards.prototype.version = "1.2.1";
677
+ $.Guards.prototype.version = "1.3.0";
678
678
 
679
679
  $.Guards.prototype.parentContext = function(element) {
680
680
  var $element = $(element);
@@ -719,7 +719,7 @@
719
719
  * </div>
720
720
  */
721
721
  $.Guards.prototype.name = function(name) {
722
- var guard = new $.Guard(null, this, true);
722
+ var guard = new $.Guard({ guards: this, named: true, name: name });
723
723
  this.named[name] = guard;
724
724
  return guard;
725
725
  };
@@ -784,6 +784,11 @@
784
784
  };
785
785
  };
786
786
 
787
+ // Check if the name is valid as a data attribute based guard name
788
+ $.Guards.prototype.isValidDataName = function(name) {
789
+ return name && /^[a-zA-Z0-9_-]+$/.test(name);
790
+ };
791
+
787
792
  // Alias for console.log, but check that such a thing exists.
788
793
  $.Guards.prototype.log = function(message) {
789
794
  if (console && console.log) {
@@ -798,10 +803,8 @@
798
803
  $(document).on(event, selector, callback);
799
804
  } else if ($.fn.delegate) {
800
805
  $(document).delegate(selector, event, callback);
801
- } else if ($.fn.live) {
802
- $(selector).live(event, callback);
803
806
  } else {
804
- this.log("Could not bind live handlers, probably because jQuery is too old.");
807
+ this.log("Could not bind event handlers, probably because jQuery is too old.");
805
808
  }
806
809
  };
807
810
 
@@ -812,10 +815,8 @@
812
815
  $(document).off(event, selector, callback);
813
816
  } else if ($.fn.undelegate) {
814
817
  $(document).undelegate(selector, event, callback);
815
- } else if ($.fn.die) {
816
- $(selector).die(event, callback);
817
818
  } else {
818
- this.log("Could not unbind live handlers, probably because jQuery is too old.");
819
+ this.log("Could not unbind event handlers, probably because jQuery is too old.");
819
820
  }
820
821
  };
821
822
 
@@ -841,6 +842,14 @@
841
842
  this.off(selector, "change blur", this.defaults.liveCallback);
842
843
  };
843
844
 
845
+ $.Guards.prototype.camelize = function(word) {
846
+ // This code is from jQuery, but it is not a public function
847
+ // so let's juse reuse it.
848
+ return word.replace(/-([\da-z])/gi, function(all, letter) {
849
+ return letter.toUpperCase();
850
+ });
851
+ };
852
+
844
853
  /**
845
854
  * Format all arguments into the first argument. This is a
846
855
  * convenience function similar to the C sprintf function, though
@@ -1231,7 +1240,7 @@
1231
1240
  /**
1232
1241
  * Validates for a valid US date.
1233
1242
  */
1234
- $.Guards.prototype.isValidDateUS = function(value, options) {
1243
+ $.Guards.prototype.isValidDateUS = function(value) {
1235
1244
  value = $.trim(value);
1236
1245
 
1237
1246
  if (value === "") {
@@ -1244,7 +1253,7 @@
1244
1253
  /**
1245
1254
  * Validates for a valid US time.
1246
1255
  */
1247
- $.Guards.prototype.isValidTimeUS = function(value, options) {
1256
+ $.Guards.prototype.isValidTimeUS = function(value) {
1248
1257
  value = $.trim(value);
1249
1258
 
1250
1259
  if (value === "") {
@@ -1345,7 +1354,7 @@
1345
1354
  * .message("Don't use the keyword 'invalid'.");
1346
1355
  */
1347
1356
  $.Guards.prototype.add = function(selector) {
1348
- var guard = new $.Guard(selector, this);
1357
+ var guard = new $.Guard({ selector: selector, guards: this });
1349
1358
  this._guards.push(guard);
1350
1359
  return guard;
1351
1360
  };
@@ -1381,6 +1390,14 @@
1381
1390
  }
1382
1391
  });
1383
1392
 
1393
+ $.each(this.named, function(name, guard) {
1394
+ var fields = callback(guard);
1395
+
1396
+ if (fields !== false && !self.test(guard, fields)) {
1397
+ result = false;
1398
+ }
1399
+ });
1400
+
1384
1401
  return result;
1385
1402
  };
1386
1403
 
@@ -1404,13 +1421,18 @@
1404
1421
  return result;
1405
1422
  };
1406
1423
 
1407
- $.Guard = function(selector, guards, named) {
1408
- this._named = named;
1409
- this._guards = guards || $.guards;
1410
- this._selector = selector;
1424
+ $.Guard = function(options) {
1425
+ this.name = options.name;
1426
+ this._named = options.named;
1427
+ this._guards = options.guards || $.guards;
1428
+ this._selector = options.selector;
1411
1429
  this._guard = null;
1412
1430
 
1413
- if (!named) {
1431
+ if (options.named && !options.selector && this._guards.isValidDataName(options.name)) {
1432
+ this._selector = "[data-guard~='" + options.name + "']";
1433
+ }
1434
+
1435
+ if (!options.named) {
1414
1436
  this.using(this._guards.defaults.guard);
1415
1437
  }
1416
1438
  };
@@ -1438,17 +1460,9 @@
1438
1460
  copyAttribute("_target");
1439
1461
  copyAttribute("_precondition");
1440
1462
  this._guard = namedGuard._guard;
1463
+ this._guardArguments = args;
1441
1464
  this.name = guard;
1442
-
1443
- if (this._guard.acceptsArguments) {
1444
- this._guard = this._guard.apply(this._guards, args);
1445
- }
1446
-
1447
- if ($.isFunction(namedGuard._message)) {
1448
- return this.message(namedGuard._message.apply(this._guards, args));
1449
- } else {
1450
- return this.message(namedGuard._message);
1451
- }
1465
+ return this.message(namedGuard._message);
1452
1466
  };
1453
1467
 
1454
1468
  /**
@@ -1787,8 +1801,17 @@
1787
1801
 
1788
1802
  $.Guard.prototype.resetMessageFn = function() {
1789
1803
  var self = this;
1790
- return this.messageFn(function() {
1791
- return $('<' + self.getTag() + ' class="' + self.getMessageClass() + '"/>').html(self._message);
1804
+ return this.messageFn(function(elements) {
1805
+ var msg = self._message;
1806
+ var dataMsg = self.getGuardDataArguments(elements, "message", true);
1807
+
1808
+ if (dataMsg !== null) {
1809
+ msg = dataMsg;
1810
+ } else if ($.isFunction(msg)) {
1811
+ msg = msg.apply(self, self.getGuardArguments(elements));
1812
+ }
1813
+
1814
+ return $('<' + self.getTag() + ' class="' + self.getMessageClass() + '"/>').html(msg);
1792
1815
  });
1793
1816
  };
1794
1817
 
@@ -1797,8 +1820,8 @@
1797
1820
  return this;
1798
1821
  };
1799
1822
 
1800
- $.Guard.prototype.errorElement = function() {
1801
- var element = this._messageFn();
1823
+ $.Guard.prototype.errorElement = function(elements) {
1824
+ var element = this._messageFn(elements);
1802
1825
  element[0].isGuardError = true;
1803
1826
  return element;
1804
1827
  };
@@ -1903,11 +1926,107 @@
1903
1926
  return $(element).filter(this._selector).size() > 0;
1904
1927
  };
1905
1928
 
1929
+ $.Guard.prototype.getGuardDataArguments = function(elements, attributeName, includeForm) {
1930
+ if (this._guards.isNullOrUndefined(elements)) {
1931
+ return null;
1932
+ }
1933
+
1934
+ var $elements = $(elements);
1935
+
1936
+ if ($elements.size() === 0 || this._guards.isNullOrUndefined(this.name) || !this._guards.isValidDataName(this.name)) {
1937
+ return null;
1938
+ }
1939
+
1940
+ var dashedAttrPrefix = "guard-" + this.name + "-";
1941
+
1942
+ if (attributeName) {
1943
+ dashedAttrPrefix = dashedAttrPrefix + attributeName;
1944
+ }
1945
+
1946
+ var result = null;
1947
+ var attrPrefix = this._guards.camelize(dashedAttrPrefix.replace(/-+$/, ""));
1948
+ var data = $elements.data() || {};
1949
+
1950
+ if (includeForm) {
1951
+ var formData = $elements.parents("form:first").data() || {};
1952
+ data = $.extend({}, formData, data);
1953
+ }
1954
+
1955
+ $.each(data, function(key, value) {
1956
+ var isDashed = key.indexOf(dashedAttrPrefix) === 0;
1957
+
1958
+ if (key.indexOf(attrPrefix) !== 0 && !isDashed) {
1959
+ return;
1960
+ }
1961
+
1962
+ var attrName;
1963
+
1964
+ if (isDashed) {
1965
+ attrName = key.substring(dashedAttrPrefix.length, key.length);
1966
+ } else {
1967
+ attrName = key.substring(attrPrefix.length, key.length);
1968
+ }
1969
+
1970
+ if (!isDashed) {
1971
+ // Un-capitalize the first letter
1972
+ attrName = attrName.replace(/^(.)/, function(all, letter) {
1973
+ return letter.toLowerCase();
1974
+ });
1975
+ }
1976
+
1977
+ if (attributeName) {
1978
+ // Grabbing just this attribute, so must be an exact
1979
+ // match, and only 1 result
1980
+ if (attrName === "") {
1981
+ result = value;
1982
+ return false;
1983
+ } else {
1984
+ return;
1985
+ }
1986
+ }
1987
+
1988
+ if (result === null) {
1989
+ result = {};
1990
+ }
1991
+
1992
+ result[attrName] = value;
1993
+ });
1994
+
1995
+ return result;
1996
+ };
1997
+
1998
+ $.Guard.prototype.getGuardArguments = function(elements) {
1999
+ var result = this._guardArguments;
2000
+
2001
+ if (this._guards.isNullOrUndefined(result)) {
2002
+ result = [];
2003
+ }
2004
+
2005
+ // Currently only support single argument hash types for data
2006
+ // attribute arguments overriding javascript arguments
2007
+ if (result.length > 1 || (result.length === 1 && $.type(result[0]) !== "object")) {
2008
+ return result;
2009
+ }
2010
+
2011
+ var dataArgs = this.getGuardDataArguments(elements);
2012
+
2013
+ if (!this._guards.isNullOrUndefined(dataArgs)) {
2014
+ if (result.length === 1) {
2015
+ result[0] = $.extend({}, result[0], dataArgs);
2016
+ } else {
2017
+ result.push(dataArgs);
2018
+ }
2019
+ }
2020
+
2021
+ return result;
2022
+ };
2023
+
1906
2024
  // Tests this guard against element(s). Element(s) should be field elements. Returns false
1907
2025
  // but doesn't apply guard if there are already errors detected. Returns true if the selector
1908
2026
  // defined for this guard doesn't apply to this element(s). Otherwise applies and adds an
1909
2027
  // error if it fails.
1910
2028
  $.Guard.prototype.test = function(element) {
2029
+ var self = this;
1911
2030
  var $elements = $(element).filter(this._selector);
1912
2031
 
1913
2032
  if ($elements.size() === 0) {
@@ -1927,7 +2046,7 @@
1927
2046
  elements = [];
1928
2047
 
1929
2048
  $elements.each(function() {
1930
- values.push($(this).inputValue(this._guards));
2049
+ values.push($(this).inputValue(self._guards));
1931
2050
  elements.push(this);
1932
2051
  });
1933
2052
  } else {
@@ -1939,7 +2058,13 @@
1939
2058
  result = true;
1940
2059
  } else {
1941
2060
  try {
1942
- result = this._guard(values, elements);
2061
+ var guardFn = this._guard;
2062
+
2063
+ if (guardFn.acceptsArguments) {
2064
+ guardFn = this._guard.apply(this._guards, this.getGuardArguments(elements));
2065
+ }
2066
+
2067
+ result = guardFn(values, elements);
1943
2068
  } catch(e) {
1944
2069
  this._guards.log("A guard threw an error: " + e);
1945
2070
  result = false;
@@ -2193,7 +2318,7 @@
2193
2318
  return this;
2194
2319
  }
2195
2320
 
2196
- var element = guard.errorElement();
2321
+ var element = guard.errorElement(this);
2197
2322
  guard.attachError(this, element);
2198
2323
  this.addClass(guard.getInvalidClass());
2199
2324
  var linked = [];
@@ -2609,4 +2734,8 @@
2609
2734
  $(this).clearErrors();
2610
2735
  }
2611
2736
  });
2737
+
2738
+ // Data driven guards
2739
+ $.liveGuard("[data-live-guarded]");
2740
+ $.enableGuards("[data-guarded]");
2612
2741
  })(jQuery);
@@ -1,5 +1,5 @@
1
1
  module GuardsJS
2
2
  module Rails
3
- VERSION = "1.2.1"
3
+ VERSION = "1.3.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: guardsjs-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.1
4
+ version: 1.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-04-03 00:00:00.000000000 Z
12
+ date: 2014-04-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: railties
@@ -59,7 +59,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
59
59
  version: '0'
60
60
  segments:
61
61
  - 0
62
- hash: 1870834343649666414
62
+ hash: 986596030085484236
63
63
  required_rubygems_version: !ruby/object:Gem::Requirement
64
64
  none: false
65
65
  requirements: