pcai_client_side_validations 3.1.5 → 3.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. metadata +20 -113
  2. data/client_side_validations.gemspec +0 -42
  3. data/lib/client_side_validations.rb +0 -13
  4. data/lib/client_side_validations/action_view.rb +0 -14
  5. data/lib/client_side_validations/action_view/form_builder.rb +0 -157
  6. data/lib/client_side_validations/action_view/form_helper.rb +0 -86
  7. data/lib/client_side_validations/action_view/form_tag_helper.rb +0 -12
  8. data/lib/client_side_validations/active_model.rb +0 -60
  9. data/lib/client_side_validations/active_model/acceptance.rb +0 -10
  10. data/lib/client_side_validations/active_model/exclusion.rb +0 -15
  11. data/lib/client_side_validations/active_model/format.rb +0 -10
  12. data/lib/client_side_validations/active_model/inclusion.rb +0 -15
  13. data/lib/client_side_validations/active_model/length.rb +0 -24
  14. data/lib/client_side_validations/active_model/numericality.rb +0 -31
  15. data/lib/client_side_validations/active_model/presence.rb +0 -10
  16. data/lib/client_side_validations/active_record.rb +0 -11
  17. data/lib/client_side_validations/active_record/middleware.rb +0 -33
  18. data/lib/client_side_validations/active_record/uniqueness.rb +0 -28
  19. data/lib/client_side_validations/core_ext.rb +0 -3
  20. data/lib/client_side_validations/core_ext/range.rb +0 -10
  21. data/lib/client_side_validations/core_ext/regexp.rb +0 -14
  22. data/lib/client_side_validations/engine.rb +0 -6
  23. data/lib/client_side_validations/files.rb +0 -8
  24. data/lib/client_side_validations/formtastic.rb +0 -21
  25. data/lib/client_side_validations/middleware.rb +0 -81
  26. data/lib/client_side_validations/mongo_mapper.rb +0 -9
  27. data/lib/client_side_validations/mongo_mapper/middleware.rb +0 -20
  28. data/lib/client_side_validations/mongo_mapper/uniqueness.rb +0 -28
  29. data/lib/client_side_validations/mongoid.rb +0 -9
  30. data/lib/client_side_validations/mongoid/middleware.rb +0 -20
  31. data/lib/client_side_validations/mongoid/uniqueness.rb +0 -28
  32. data/lib/client_side_validations/simple_form.rb +0 -24
  33. data/lib/client_side_validations/version.rb +0 -3
  34. data/lib/generators/client_side_validations/copy_asset_generator.rb +0 -36
  35. data/lib/generators/client_side_validations/install_generator.rb +0 -45
  36. data/lib/generators/templates/client_side_validations/README.rails.3.0 +0 -6
  37. data/lib/generators/templates/client_side_validations/README.rails.3.1 +0 -7
  38. data/lib/generators/templates/client_side_validations/initializer.rb +0 -14
  39. data/vendor/assets/javascripts/rails.validations.js +0 -410
@@ -1,86 +0,0 @@
1
- module ClientSideValidations::ActionView::Helpers
2
- module FormHelper
3
- class Error < StandardError; end
4
-
5
- def form_for(record_or_name_or_array, *args, &proc)
6
- options = args.extract_options!
7
- if options[:validate]
8
-
9
- content_for_name = options[:validate] unless options[:validate] == true
10
-
11
- # Always turn off HTML5 Validations
12
- options[:html] ||= {}
13
- options[:html][:novalidate] = 'novalidate'
14
-
15
- case record_or_name_or_array
16
- when String, Symbol
17
- raise ClientSideValidations::ActionView::Helpers::FormHelper::Error, 'Using form_for(:name, @resource) is deprecated in Rails and is not supported with ClientSideValidations. Please use form_for(@resource, :as => :name) instead.'
18
- when Array
19
- object = record_or_name_or_array.last
20
- else
21
- object = record_or_name_or_array
22
- end
23
- end
24
-
25
- @validators = {}
26
- # Order matters here. Rails mutates the options object
27
- script = client_side_form_settings(object, options)
28
- form = super(record_or_name_or_array, *(args << options), &proc)
29
- # Because of the load order requirement above this sub is necessary
30
- # Would be nice to not do this
31
- script = insert_validators_into_script(script)
32
- if content_for_name
33
- content_for(content_for_name) { script.html_safe }
34
- script = nil
35
- end
36
- "#{form}#{script}".html_safe
37
- end
38
-
39
- def apply_form_for_options!(object_or_array, options)
40
- super
41
- options[:html][:validate] = true if options[:validate]
42
- end
43
-
44
- def fields_for(record_or_name_or_array, *args, &block)
45
- output = super
46
- @validators.merge!(args.last ? args.last[:validators] : {}) if @validators
47
- output
48
- end
49
-
50
- private
51
-
52
- def insert_validators_into_script(script)
53
- # There is probably a more performant way of doing this
54
- # But using String#sub has some issues. Undocumented "features"
55
- if script
56
- script = script.split(/"validator_hash"/)
57
- script = "#{script[0]}#{@validators.to_json}#{script[1]}"
58
- end
59
-
60
- script
61
- end
62
-
63
- def client_side_form_settings(object, options)
64
- if options[:validate]
65
- builder = options[:builder] || ActionView::Base.default_form_builder
66
-
67
- if options[:html] && options[:html][:id]
68
- var_name = options[:html][:id]
69
- else
70
- var_name = if object.respond_to?(:persisted?) && object.persisted?
71
- options[:as] ? "#{options[:as]}_edit" : dom_id(object, :edit)
72
- else
73
- options[:as] ? "#{options[:as]}_new" : dom_id(object)
74
- end
75
- end
76
-
77
- content_tag(:script) do
78
- "window['#{var_name}'] = #{builder.client_side_form_settings(options, self).merge(:validators => 'validator_hash').to_json};".html_safe
79
- end
80
-
81
- end
82
- end
83
-
84
- end
85
- end
86
-
@@ -1,12 +0,0 @@
1
- module ClientSideValidations::ActionView::Helpers
2
- module FormTagHelper
3
- private
4
- def html_options_for_form(url_for_options, options, *parameters_for_url)
5
- options.stringify_keys!
6
- html_options = {}
7
- html_options['data-validate'] = options.delete('validate') if options['validate']
8
- html_options.merge!(super(url_for_options, options, *parameters_for_url))
9
- end
10
- end
11
- end
12
-
@@ -1,60 +0,0 @@
1
- require 'client_side_validations/core_ext'
2
-
3
- module ClientSideValidations::ActiveModel
4
- module Validator
5
-
6
- def client_side_hash(model, attribute)
7
- options = self.options.dup
8
- { :message => model.errors.generate_message(attribute, message_type, options) }.merge(options.except(*::ActiveModel::Errors::CALLBACKS_OPTIONS - [:allow_blank, :if, :unless]))
9
- end
10
-
11
- private
12
-
13
- def message_type
14
- kind
15
- end
16
- end
17
-
18
- module Validations
19
- def client_side_validation_hash
20
- @client_side_validation_hash ||= _validators.inject({}) do |attr_hash, attr|
21
- unless [nil, :block].include?(attr[0])
22
-
23
- validator_hash = attr[1].inject({}) do |kind_hash, validator|
24
- client_side_hash = validator.client_side_hash(self, attr[0])
25
- # Yeah yeah, #new_record? is not part of ActiveModel :p
26
- if (can_use_for_client_side_validation?(client_side_hash, validator))
27
- kind_hash.merge!(validator.kind => client_side_hash.except(:on))
28
- else
29
- kind_hash.merge!({})
30
- end
31
- end
32
-
33
- if validator_hash.present?
34
- attr_hash.merge!(attr[0] => validator_hash)
35
- else
36
- attr_hash
37
- end
38
- else
39
- attr_hash
40
- end
41
- end
42
- end
43
-
44
- private
45
-
46
- def can_use_for_client_side_validation?(client_side_hash, validator)
47
- ((self.respond_to?(:new_record?) && validator.options[:on] == (self.new_record? ? :create : :update)) || validator.options[:on].nil?) && validator.kind != :block
48
- end
49
- end
50
- end
51
-
52
- ActiveModel::Validator.send(:include, ClientSideValidations::ActiveModel::Validator)
53
- ActiveModel::Validations.send(:include, ClientSideValidations::ActiveModel::Validations)
54
-
55
- %w{acceptance exclusion inclusion length format numericality presence}.each do |validator|
56
- require "client_side_validations/active_model/#{validator}"
57
- validator.capitalize!
58
- eval "ActiveModel::Validations::#{validator}Validator.send(:include, ClientSideValidations::ActiveModel::#{validator})"
59
- end
60
-
@@ -1,10 +0,0 @@
1
- module ClientSideValidations::ActiveModel
2
- module Acceptance
3
- private
4
-
5
- def message_type
6
- :accepted
7
- end
8
- end
9
- end
10
-
@@ -1,15 +0,0 @@
1
- module ClientSideValidations::ActiveModel
2
- module Exclusion
3
-
4
- def client_side_hash(model, attribute)
5
- hash = super
6
- if hash[:in].is_a?(Range)
7
- hash[:range] = hash[:in]
8
- hash.delete(:in)
9
- end
10
- hash
11
- end
12
-
13
- end
14
- end
15
-
@@ -1,10 +0,0 @@
1
- module ClientSideValidations::ActiveModel
2
- module Format
3
- private
4
-
5
- def message_type
6
- :invalid
7
- end
8
- end
9
- end
10
-
@@ -1,15 +0,0 @@
1
- module ClientSideValidations::ActiveModel
2
- module Inclusion
3
-
4
- def client_side_hash(model, attribute)
5
- hash = super
6
- if hash[:in].is_a?(Range)
7
- hash[:range] = hash[:in]
8
- hash.delete(:in)
9
- end
10
- hash
11
- end
12
-
13
- end
14
- end
15
-
@@ -1,24 +0,0 @@
1
- module ClientSideValidations::ActiveModel
2
- module Length
3
-
4
- def client_side_hash(model, attribute)
5
- options = self.options.dup
6
- hash = { :messages => {} }
7
- hash[:js_tokenizer] = options[:js_tokenizer] if options[:js_tokenizer]
8
- hash[:allow_blank] = true if options[:allow_blank]
9
-
10
- self.class::MESSAGES.each do |option, message_type|
11
- if count = options[option]
12
- options[:message] = options[message_type]
13
- options.delete(:message) if options[:message].nil?
14
- hash[:messages][option] = model.errors.generate_message(attribute, message_type, options.merge(:count => count))
15
- hash[option] = count
16
- end
17
- end
18
-
19
- hash
20
- end
21
-
22
- end
23
- end
24
-
@@ -1,31 +0,0 @@
1
- module ClientSideValidations::ActiveModel
2
- module Numericality
3
-
4
- OPTION_MAP = {}
5
-
6
- def self.included(base)
7
- OPTION_MAP.merge!(base::CHECKS.keys.inject({}) { |hash, key| hash.merge!(key => key) })
8
- end
9
-
10
- def client_side_hash(model, attribute)
11
- options = self.options.dup
12
- hash = { :messages => { :numericality => model.errors.generate_message(attribute, :not_a_number, options) } }
13
-
14
- if options[:only_integer]
15
- hash[:messages][:only_integer] = model.errors.generate_message(attribute, :not_an_integer, options)
16
- hash[:only_integer] = true
17
- end
18
-
19
- OPTION_MAP.each do |option, message_type|
20
- if count = options[option]
21
- hash[:messages][option] = model.errors.generate_message(attribute, message_type, options.merge(:count => count))
22
- hash[option] = count
23
- end
24
- end
25
-
26
- hash
27
- end
28
-
29
- end
30
- end
31
-
@@ -1,10 +0,0 @@
1
- module ClientSideValidations::ActiveModel
2
- module Presence
3
- private
4
-
5
- def message_type
6
- :blank
7
- end
8
- end
9
- end
10
-
@@ -1,11 +0,0 @@
1
- require 'client_side_validations/active_model'
2
- require 'client_side_validations/active_record/middleware'
3
-
4
- %w{uniqueness}.each do |validator|
5
- require "client_side_validations/active_record/#{validator}"
6
- validator.capitalize!
7
- eval "ActiveRecord::Validations::#{validator}Validator.send(:include, ClientSideValidations::ActiveRecord::#{validator})"
8
- end
9
-
10
- ActiveRecord::Base.send(:include, ClientSideValidations::ActiveModel::Validations)
11
-
@@ -1,33 +0,0 @@
1
- module ClientSideValidations::ActiveRecord
2
- class Middleware
3
-
4
- def self.is_unique?(klass, attribute, value, params)
5
- column = klass.columns_hash[attribute.to_s]
6
- value = column.limit ? value.to_s.mb_chars[0, column.limit] : value.to_s if column.text?
7
-
8
- t = klass.arel_table
9
-
10
- if params[:case_sensitive] == 'true'
11
- if t.engine.connection.instance_variable_get("@config")[:adapter] == 'mysql'
12
- relation = Arel::Nodes::SqlLiteral.new("BINARY #{t[attribute].eq(value).to_sql}")
13
- else
14
- relation = t[attribute].eq(value)
15
- end
16
- else
17
- relation = t[attribute].matches(value)
18
- end
19
-
20
- if relation.is_a?(Arel::Nodes::SqlLiteral)
21
- relation = Arel::Nodes::SqlLiteral.new("BINARY #{t[attribute].eq(value).to_sql} AND #{t.primary_key.not_eq(params[:id]).to_sql}")
22
- else
23
- relation = relation.and(t.primary_key.not_eq(params[:id])) if params[:id]
24
- end
25
-
26
- (params[:scope] || {}).each do |key, value|
27
- relation = relation.and(t[key].eq(value))
28
- end
29
-
30
- !klass.where(relation).exists?
31
- end
32
- end
33
- end
@@ -1,28 +0,0 @@
1
- module ClientSideValidations::ActiveRecord
2
- module Uniqueness
3
- def client_side_hash(model, attribute)
4
- hash = {}
5
- hash[:message] = model.errors.generate_message(attribute, message_type, options.except(:scope))
6
- hash[:case_sensitive] = options[:case_sensitive]
7
- hash[:id] = model.id unless model.new_record?
8
- if options.key?(:scope) && options[:scope].present?
9
- hash[:scope] = Array.wrap(options[:scope]).inject({}) do |scope_hash, scope_item|
10
- scope_hash.merge!(scope_item => model.send(scope_item))
11
- end
12
- end
13
-
14
- unless model.class.name.demodulize == model.class.name
15
- hash[:class] = model.class.name.underscore
16
- end
17
-
18
- hash
19
- end
20
-
21
- private
22
-
23
- def message_type
24
- :taken
25
- end
26
- end
27
- end
28
-
@@ -1,3 +0,0 @@
1
- require 'active_support/json'
2
- require 'client_side_validations/core_ext/range'
3
- require 'client_side_validations/core_ext/regexp'
@@ -1,10 +0,0 @@
1
- class Range
2
- def as_json(options = nil)
3
- [first, last]
4
- end
5
-
6
- def to_json(options = nil)
7
- as_json(options).inspect
8
- end
9
- end
10
-
@@ -1,14 +0,0 @@
1
- class Regexp
2
- def as_json(options = nil)
3
- Regexp.new inspect.sub("\\A","^").sub("\\Z","$").sub("\\z","$").sub(/^\//,"").sub(/\/[a-z]*$/,""), self.options
4
- end
5
-
6
- def to_json(options = nil)
7
- as_json(options).inspect
8
- end
9
-
10
- def encode_json(encoder)
11
- inspect
12
- end
13
- end
14
-
@@ -1,6 +0,0 @@
1
- module ClientSideValidations
2
- class Engine < ::Rails::Engine
3
- config.app_middleware.use ClientSideValidations::Middleware::Validators
4
- end
5
- end
6
-
@@ -1,8 +0,0 @@
1
- # This is only used by dependant libraries that need to find the files
2
-
3
- module ClientSideValidations
4
- module Files
5
- Initializer = File.expand_path(File.dirname(__FILE__) + '/../generators/templates/client_side_validations/initializer.rb')
6
- Javascript = File.expand_path(File.dirname(__FILE__) + '/../../vendor/assets/javascripts/rails.validations.js')
7
- end
8
- end
@@ -1,21 +0,0 @@
1
- module ClientSideValidations
2
- module Formtastic
3
- module FormBuilder
4
-
5
- def self.included(base)
6
- base.class_eval do
7
- def self.client_side_form_settings(options, form_helper)
8
- {
9
- :type => self.to_s,
10
- :inline_error_class => ::Formtastic::FormBuilder.default_inline_error_class
11
- }
12
- end
13
- end
14
- end
15
-
16
- end
17
- end
18
- end
19
-
20
- Formtastic::FormBuilder.send(:include, ClientSideValidations::Formtastic::FormBuilder)
21
-
@@ -1,81 +0,0 @@
1
- # encoding: utf-8
2
-
3
- require 'client_side_validations/core_ext'
4
-
5
- module ClientSideValidations
6
-
7
- module Middleware
8
- class Validators
9
- def initialize(app)
10
- @app = app
11
- end
12
-
13
- def call(env)
14
- case env['PATH_INFO']
15
- when %r{\/validators\/(\w+)}
16
- "::ClientSideValidations::Middleware::#{$1.camelize}".constantize.new(env).response
17
- else
18
- @app.call(env)
19
- end
20
- end
21
- end
22
-
23
- class Base
24
- attr_accessor :request, :body, :status
25
-
26
- def initialize(env)
27
- self.body = ''
28
- self.status = 200
29
- self.request = ActionDispatch::Request.new(env)
30
- end
31
-
32
- def response
33
- [status, {'Content-Type' => content_type, 'Content-Length' => body.length.to_s}, [body]]
34
- end
35
-
36
- def content_type
37
- 'application/json'
38
- end
39
- end
40
-
41
- class Uniqueness < Base
42
- IGNORE_PARAMS = %w{case_sensitive id scope}
43
-
44
- def response
45
- if is_unique?
46
- self.status = 404
47
- self.body = 'true'
48
- else
49
- self.status = 200
50
- self.body = 'false'
51
- end
52
- super
53
- end
54
-
55
- private
56
-
57
- def is_unique?
58
- resource = extract_resource
59
- klass = resource.classify.constantize
60
- attribute = request.params[resource].keys.first
61
- value = request.params[resource][attribute]
62
-
63
- if (defined?(::ActiveRecord::Base) && klass < ::ActiveRecord::Base)
64
- middleware_klass = ClientSideValidations::ActiveRecord::Middleware
65
- elsif (defined?(::Mongoid::Document) && klass.included_modules.include?(::Mongoid::Document))
66
- middleware_klass = ClientSideValidations::Mongoid::Middleware
67
- elsif (defined?(::MongoMapper::Document) && klass.included_modules.include?(::MongoMapper::Document))
68
- middleware_klass = ClientSideValidations::MongoMapper::Middleware
69
- end
70
-
71
- middleware_klass.is_unique?(klass, attribute, value, request.params)
72
- end
73
-
74
- def extract_resource
75
- parent_key = (request.params.keys - IGNORE_PARAMS).first
76
- end
77
- end
78
- end
79
-
80
- end
81
-