client_side_validations 2.9.9 → 3.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/client_side_validations.gemspec +45 -0
- data/javascript/rails.validations.js +380 -0
- data/lib/client_side_validations.rb +5 -57
- data/lib/client_side_validations/action_view.rb +13 -0
- data/lib/client_side_validations/action_view/form_builder.rb +57 -0
- data/lib/client_side_validations/action_view/form_helper.rb +58 -0
- data/lib/client_side_validations/action_view/form_tag_helper.rb +12 -0
- data/lib/client_side_validations/active_model.rb +46 -0
- data/lib/client_side_validations/active_model/acceptance.rb +10 -0
- data/lib/client_side_validations/active_model/exclusion.rb +15 -0
- data/lib/client_side_validations/active_model/format.rb +10 -0
- data/lib/client_side_validations/active_model/inclusion.rb +15 -0
- data/lib/client_side_validations/active_model/length.rb +22 -0
- data/lib/client_side_validations/active_model/numericality.rb +26 -0
- data/lib/client_side_validations/active_model/presence.rb +10 -0
- data/lib/client_side_validations/active_record.rb +11 -0
- data/lib/client_side_validations/active_record/middleware.rb +25 -0
- data/lib/client_side_validations/active_record/uniqueness.rb +24 -0
- data/lib/client_side_validations/core_ext.rb +3 -0
- data/lib/client_side_validations/core_ext/range.rb +10 -0
- data/lib/client_side_validations/core_ext/regexp.rb +14 -0
- data/lib/client_side_validations/formtastic.rb +21 -0
- data/lib/client_side_validations/middleware.rb +83 -0
- data/lib/client_side_validations/mongoid.rb +9 -0
- data/lib/client_side_validations/mongoid/middleware.rb +20 -0
- data/lib/client_side_validations/mongoid/uniqueness.rb +24 -0
- data/lib/client_side_validations/simple_form.rb +24 -0
- data/lib/client_side_validations/version.rb +3 -0
- data/lib/generators/client_side_validations/install_generator.rb +22 -0
- data/lib/generators/templates/README +7 -0
- data/lib/generators/templates/client_side_validations.rb +14 -0
- data/test/action_view/cases/helper.rb +152 -0
- data/test/action_view/cases/test_helpers.rb +222 -0
- data/test/action_view/cases/test_legacy_helpers.rb +150 -0
- data/test/action_view/models.rb +3 -0
- data/test/action_view/models/comment.rb +35 -0
- data/test/action_view/models/post.rb +35 -0
- data/test/active_model/cases/helper.rb +4 -0
- data/test/active_model/cases/test_acceptance_validator.rb +16 -0
- data/test/active_model/cases/test_base.rb +11 -0
- data/test/active_model/cases/test_confirmation_validator.rb +16 -0
- data/test/active_model/cases/test_exclusion_validator.rb +20 -0
- data/test/active_model/cases/test_format_validator.rb +21 -0
- data/test/active_model/cases/test_inclusion_validator.rb +21 -0
- data/test/active_model/cases/test_length_validator.rb +61 -0
- data/test/active_model/cases/test_numericality_validator.rb +46 -0
- data/test/active_model/cases/test_presence_validator.rb +16 -0
- data/test/active_model/cases/test_validations.rb +132 -0
- data/test/active_model/models/person.rb +12 -0
- data/test/active_record/cases/helper.rb +12 -0
- data/test/active_record/cases/test_base.rb +11 -0
- data/test/active_record/cases/test_middleware.rb +150 -0
- data/test/active_record/cases/test_uniqueness_validator.rb +31 -0
- data/test/active_record/models/guid.rb +7 -0
- data/test/active_record/models/user.rb +10 -0
- data/test/base_helper.rb +6 -0
- data/test/core_ext/cases/test_core_ext.rb +45 -0
- data/test/formtastic/cases/helper.rb +2 -0
- data/test/formtastic/cases/test_form_builder.rb +11 -0
- data/test/formtastic/cases/test_form_helper.rb +22 -0
- data/test/generators/cases/test_install_generator.rb +15 -0
- data/test/javascript/config.ru +3 -0
- data/test/javascript/public/test/callbacks/elementAfter.js +53 -0
- data/test/javascript/public/test/callbacks/elementBefore.js +53 -0
- data/test/javascript/public/test/callbacks/elementFail.js +69 -0
- data/test/javascript/public/test/callbacks/elementPass.js +69 -0
- data/test/javascript/public/test/callbacks/formAfter.js +44 -0
- data/test/javascript/public/test/callbacks/formBefore.js +44 -0
- data/test/javascript/public/test/callbacks/formFail.js +50 -0
- data/test/javascript/public/test/callbacks/formPass.js +49 -0
- data/test/javascript/public/test/form_builders/validateForm.js +65 -0
- data/test/javascript/public/test/form_builders/validateFormtastic.js +51 -0
- data/test/javascript/public/test/form_builders/validateSimpleForm.js +54 -0
- data/test/javascript/public/test/settings.js +15 -0
- data/test/javascript/public/test/validateElement.js +138 -0
- data/test/javascript/public/test/validators/acceptance.js +42 -0
- data/test/javascript/public/test/validators/confirmation.js +25 -0
- data/test/javascript/public/test/validators/exclusion.js +41 -0
- data/test/javascript/public/test/validators/format.js +27 -0
- data/test/javascript/public/test/validators/inclusion.js +42 -0
- data/test/javascript/public/test/validators/length.js +70 -0
- data/test/javascript/public/test/validators/numericality.js +135 -0
- data/test/javascript/public/test/validators/presence.js +15 -0
- data/test/javascript/public/test/validators/uniqueness.js +88 -0
- data/test/javascript/public/vendor/jquery.metadata.js +122 -0
- data/test/javascript/public/vendor/qunit.css +196 -0
- data/test/javascript/public/vendor/qunit.js +1374 -0
- data/test/javascript/server.rb +78 -0
- data/test/javascript/views/index.erb +20 -0
- data/test/javascript/views/layout.erb +21 -0
- data/test/middleware/cases/helper.rb +15 -0
- data/test/middleware/cases/test_middleware.rb +8 -0
- data/test/mongoid/cases/helper.rb +16 -0
- data/test/mongoid/cases/test_base.rb +15 -0
- data/test/mongoid/cases/test_middleware.rb +68 -0
- data/test/mongoid/cases/test_uniqueness_validator.rb +31 -0
- data/test/mongoid/models/book.rb +8 -0
- data/test/simple_form/cases/helper.rb +2 -0
- data/test/simple_form/cases/test_form_builder.rb +14 -0
- data/test/simple_form/cases/test_form_helper.rb +22 -0
- metadata +230 -111
- data/LICENSE +0 -24
- data/README.markdown +0 -132
- data/generators/client_side_validations/client_side_validations_generator.rb +0 -17
- data/javascript/lib/client_side_validations.js +0 -93
- data/javascript/lib/jquery-validation.js +0 -1146
- data/lib/client_side_validations/adapters/action_view.rb +0 -153
- data/lib/client_side_validations/adapters/active_model.rb +0 -137
- data/lib/client_side_validations/adapters/orm_base.rb +0 -89
- data/lib/client_side_validations/orm.rb +0 -226
- data/lib/client_side_validations/rails.rb +0 -11
- data/lib/client_side_validations/template.rb +0 -3
- data/lib/generators/client_side_validations_generator.rb +0 -15
@@ -0,0 +1,13 @@
|
|
1
|
+
module ClientSideValidations::ActionView
|
2
|
+
module Helpers
|
3
|
+
end
|
4
|
+
end
|
5
|
+
|
6
|
+
require "client_side_validations/action_view/form_helper"
|
7
|
+
require "client_side_validations/action_view/form_tag_helper"
|
8
|
+
require "client_side_validations/action_view/form_builder"
|
9
|
+
|
10
|
+
ActionView::Base.send(:include, ClientSideValidations::ActionView::Helpers::FormHelper)
|
11
|
+
ActionView::Base.send(:include, ClientSideValidations::ActionView::Helpers::FormTagHelper)
|
12
|
+
ActionView::Helpers::FormBuilder.send(:include, ClientSideValidations::ActionView::Helpers::FormBuilder)
|
13
|
+
|
@@ -0,0 +1,57 @@
|
|
1
|
+
module ClientSideValidations::ActionView::Helpers
|
2
|
+
module FormBuilder
|
3
|
+
|
4
|
+
def self.included(base)
|
5
|
+
(base.field_helpers - %w(apply_form_for_options! label check_box radio_button fields_for hidden_field)).each do |selector|
|
6
|
+
base.class_eval <<-RUBY_EVAL
|
7
|
+
def #{selector}_with_client_side_validations(method, options = {})
|
8
|
+
apply_client_side_validators(method, options)
|
9
|
+
options.delete(:validate)
|
10
|
+
#{selector}_without_client_side_validations(method, options)
|
11
|
+
end
|
12
|
+
RUBY_EVAL
|
13
|
+
|
14
|
+
base.class_eval { alias_method_chain selector, :client_side_validations }
|
15
|
+
end
|
16
|
+
|
17
|
+
base.class_eval do
|
18
|
+
alias_method_chain :fields_for, :client_side_validations
|
19
|
+
alias_method_chain :check_box, :client_side_validations
|
20
|
+
alias_method_chain :radio_button, :client_side_validations
|
21
|
+
|
22
|
+
def self.client_side_form_js_hash(options, form_helper)
|
23
|
+
{
|
24
|
+
:type => self.to_s,
|
25
|
+
:input_tag => form_helper.class.field_error_proc.call(%{<span id="input_tag" />}, Struct.new(:error_message, :tag_id).new([], "")),
|
26
|
+
:label_tag => form_helper.class.field_error_proc.call(%{<label id="label_tag" />})
|
27
|
+
}
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def fields_for_with_client_side_validations(record_or_name_or_array, *args, &block)
|
33
|
+
options = args.extract_options!
|
34
|
+
options[:validate] ||= @options[:validate] if @options[:validate] && !options.key?(:validate)
|
35
|
+
fields_for_without_client_side_validations(record_or_name_or_array, *(args << options), &block)
|
36
|
+
end
|
37
|
+
|
38
|
+
def check_box_with_client_side_validations(method, options = {}, checked_value = "1", unchecked_value = "0")
|
39
|
+
apply_client_side_validators(method, options)
|
40
|
+
check_box_without_client_side_validations(method, options, checked_value, unchecked_value)
|
41
|
+
end
|
42
|
+
|
43
|
+
def radio_button_with_client_side_validations(method, tag_value, options = {})
|
44
|
+
apply_client_side_validators(method, options)
|
45
|
+
radio_button_without_client_side_validations(method, tag_value, options)
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
def apply_client_side_validators(method, options = {})
|
51
|
+
if @options[:validate] && options[:validate] != false && validators = @object.client_side_validation_hash[method]
|
52
|
+
options.merge!("data-validators" => validators.to_json)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,58 @@
|
|
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
|
+
# Turn off SimpleForm's HTML5 Form Validations
|
10
|
+
if options[:builder].to_s == 'SimpleForm::FormBuilder'
|
11
|
+
options[:html][:novalidate] = true
|
12
|
+
end
|
13
|
+
|
14
|
+
case record_or_name_or_array
|
15
|
+
when String, Symbol
|
16
|
+
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.'
|
17
|
+
when Array
|
18
|
+
object = record_or_name_or_array.last
|
19
|
+
else
|
20
|
+
object = record_or_name_or_array
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
script_tag = client_side_form_js_variable(object, options)
|
25
|
+
"#{super(record_or_name_or_array, *(args << options), &proc)}#{script_tag}".html_safe
|
26
|
+
end
|
27
|
+
|
28
|
+
def apply_form_for_options!(object_or_array, options)
|
29
|
+
super
|
30
|
+
options[:html][:validate] = true if options[:validate]
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
def client_side_form_js_variable(object, options)
|
36
|
+
if options[:validate]
|
37
|
+
builder = options[:builder] || ActionView::Base.default_form_builder
|
38
|
+
|
39
|
+
if options[:html] && options[:html][:id]
|
40
|
+
var_name = options[:html][:id]
|
41
|
+
else
|
42
|
+
var_name = if object.respond_to?(:persisted?) && object.persisted?
|
43
|
+
options[:as] ? "#{options[:as]}_edit" : dom_id(object, :edit)
|
44
|
+
else
|
45
|
+
options[:as] ? "#{options[:as]}_new" : dom_id(object)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
content_tag(:script) do
|
50
|
+
"var #{var_name} = #{builder.client_side_form_js_hash(options, self).to_json};".html_safe
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
@@ -0,0 +1,12 @@
|
|
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
|
+
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'client_side_validations/core_ext'
|
2
|
+
|
3
|
+
module ClientSideValidations::ActiveModel
|
4
|
+
module Validator
|
5
|
+
|
6
|
+
def client_side_hash(model, attribute)
|
7
|
+
extra_options = options.except(*::ActiveModel::Errors::CALLBACKS_OPTIONS - [:on, :allow_blank])
|
8
|
+
{ :message => model.errors.generate_message(attribute, message_type, extra_options) }.merge(extra_options)
|
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
|
+
_validators.inject({}) do |attr_hash, attr|
|
21
|
+
|
22
|
+
validator_hash = attr[1].inject({}) do |kind_hash, validator|
|
23
|
+
client_side_hash = validator.client_side_hash(self, attr[0])
|
24
|
+
# Yeah yeah, #new_record? is not part of ActiveModel :p
|
25
|
+
if ((self.respond_to?(:new_record?) && client_side_hash[:on] == (self.new_record? ? :create : :update)) || client_side_hash[:on].nil?)
|
26
|
+
kind_hash.merge!(validator.kind => client_side_hash.except(:on))
|
27
|
+
else
|
28
|
+
kind_hash.merge!({})
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
attr_hash.merge!(attr[0] => validator_hash)
|
33
|
+
end.delete_if { |key, value| value.blank? }
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
ActiveModel::Validator.send(:include, ClientSideValidations::ActiveModel::Validator)
|
39
|
+
ActiveModel::Validations.send(:include, ClientSideValidations::ActiveModel::Validations)
|
40
|
+
|
41
|
+
%w{acceptance exclusion inclusion length format numericality presence}.each do |validator|
|
42
|
+
require "client_side_validations/active_model/#{validator}"
|
43
|
+
validator.capitalize!
|
44
|
+
eval "ActiveModel::Validations::#{validator}Validator.send(:include, ClientSideValidations::ActiveModel::#{validator})"
|
45
|
+
end
|
46
|
+
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module ClientSideValidations::ActiveModel
|
2
|
+
module Length
|
3
|
+
|
4
|
+
def client_side_hash(model, attribute)
|
5
|
+
extra_options = options.except(*::ActiveModel::Errors::CALLBACKS_OPTIONS - [:allow_blank, :on]).except(:tokenizer, :too_long, :too_short, :wrong_length)
|
6
|
+
|
7
|
+
errors_options = options.except(*self.class::RESERVED_OPTIONS)
|
8
|
+
messages = extra_options.except(:js_tokenizer, :allow_blank, :on).keys.inject({}) do |hash, key|
|
9
|
+
errors_options[:count] = extra_options[key]
|
10
|
+
count = extra_options[key]
|
11
|
+
default_message = options[self.class::MESSAGES[key]]
|
12
|
+
errors_options[:message] ||= default_message if default_message
|
13
|
+
|
14
|
+
hash.merge!(key => model.errors.generate_message(attribute, self.class::MESSAGES[key], errors_options))
|
15
|
+
end
|
16
|
+
|
17
|
+
{ :messages => messages }.merge(extra_options)
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
@@ -0,0 +1,26 @@
|
|
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
|
+
OPTION_MAP.merge!(:numericality => :not_a_number, :only_integer => :not_an_integer)
|
9
|
+
end
|
10
|
+
|
11
|
+
def client_side_hash(model, attribute)
|
12
|
+
except_options = ::ActiveModel::Errors::CALLBACKS_OPTIONS - [:on] + [:message]
|
13
|
+
extra_options = options.except(*except_options).reject { |key, value| key == :only_integer && !value }
|
14
|
+
keys = [:numericality] | (extra_options.keys - [:message, :on])
|
15
|
+
filtered_options = options.except(*self.class::RESERVED_OPTIONS)
|
16
|
+
messages = keys.inject({}) do |hash, key|
|
17
|
+
count = extra_options[key]
|
18
|
+
hash.merge!(key => model.errors.generate_message(attribute, OPTION_MAP[key], filtered_options.merge(:count => count)))
|
19
|
+
end
|
20
|
+
|
21
|
+
{ :messages => messages }.merge(extra_options)
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
@@ -0,0 +1,11 @@
|
|
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
|
+
|
@@ -0,0 +1,25 @@
|
|
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
|
+
relation = t[attribute].eq(value)
|
12
|
+
else
|
13
|
+
relation = t[attribute].matches(value)
|
14
|
+
end
|
15
|
+
|
16
|
+
relation = relation.and(t[:id].not_eq(params[:id])) if params[:id]
|
17
|
+
|
18
|
+
(params[:scope] || {}).each do |key, value|
|
19
|
+
relation = relation.and(t[key].eq(value))
|
20
|
+
end
|
21
|
+
|
22
|
+
!klass.where(relation).exists?
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module ClientSideValidations::ActiveRecord
|
2
|
+
module Uniqueness
|
3
|
+
def client_side_hash(model, attribute)
|
4
|
+
extra_options = options.except(*::ActiveModel::Errors::CALLBACKS_OPTIONS - [:on, :allow_blank])
|
5
|
+
hash = { :message => model.errors.generate_message(attribute, message_type, extra_options.except(:case_sensitive, :scope)) }
|
6
|
+
hash = hash.merge(extra_options).merge(model.new_record? ? {} : { :id => model.id })
|
7
|
+
|
8
|
+
if hash[:scope]
|
9
|
+
hash[:scope] = hash[:scope].inject({}) do |scope_hash, scope_item|
|
10
|
+
scope_hash.merge!(scope_item => model.send(scope_item))
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
hash
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def message_type
|
20
|
+
:taken
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
@@ -0,0 +1,14 @@
|
|
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
|
+
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module ClientSideValidations
|
2
|
+
module Formtastic
|
3
|
+
module SemanticFormBuilder
|
4
|
+
|
5
|
+
def self.included(base)
|
6
|
+
base.class_eval do
|
7
|
+
def self.client_side_form_js_hash(options, form_helper)
|
8
|
+
{
|
9
|
+
:type => self.to_s,
|
10
|
+
:inline_error_class => ::Formtastic::SemanticFormBuilder.default_inline_error_class
|
11
|
+
}
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
Formtastic::SemanticFormBuilder.send(:include, ClientSideValidations::Formtastic::SemanticFormBuilder)
|
21
|
+
|
@@ -0,0 +1,83 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module ClientSideValidations
|
4
|
+
|
5
|
+
module Middleware
|
6
|
+
|
7
|
+
class Validators
|
8
|
+
def initialize(app)
|
9
|
+
@app = app
|
10
|
+
end
|
11
|
+
|
12
|
+
def call(env)
|
13
|
+
case env['PATH_INFO']
|
14
|
+
when %r{\/validators\/(\w+)\.json}
|
15
|
+
"::ClientSideValidations::Middleware::#{$1.camelize}".constantize.new(env).response
|
16
|
+
else
|
17
|
+
@app.call(env)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
class Base
|
23
|
+
attr_accessor :request, :body, :status
|
24
|
+
|
25
|
+
def initialize(env)
|
26
|
+
self.body = ''
|
27
|
+
self.status = 200
|
28
|
+
self.request = ActionDispatch::Request.new(env)
|
29
|
+
end
|
30
|
+
|
31
|
+
def response
|
32
|
+
[status, {'Content-Type' => content_type, 'Content-Length' => body.length.to_s}, [body]]
|
33
|
+
end
|
34
|
+
|
35
|
+
def content_type
|
36
|
+
'application/json'
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
class Uniqueness < Base
|
41
|
+
IGNORE_PARAMS = %w{case_sensitive id scope}
|
42
|
+
|
43
|
+
def response
|
44
|
+
if is_unique?
|
45
|
+
self.status = 404
|
46
|
+
self.body = 'true'
|
47
|
+
else
|
48
|
+
self.status = 200
|
49
|
+
self.body = 'false'
|
50
|
+
end
|
51
|
+
super
|
52
|
+
end
|
53
|
+
|
54
|
+
private
|
55
|
+
|
56
|
+
def is_unique?
|
57
|
+
resource = extract_resource
|
58
|
+
klass = resource.classify.constantize
|
59
|
+
attribute = request.params[resource].keys.first
|
60
|
+
value = request.params[resource][attribute]
|
61
|
+
|
62
|
+
if (defined?(::ActiveRecord::Base) && klass < ::ActiveRecord::Base)
|
63
|
+
middleware_klass = ClientSideValidations::ActiveRecord::Middleware
|
64
|
+
elsif (defined?(::Mongoid::Document) && klass.included_modules.include?(::Mongoid::Document))
|
65
|
+
middleware_klass = ClientSideValidations::Mongoid::Middleware
|
66
|
+
end
|
67
|
+
|
68
|
+
middleware_klass.is_unique?(klass, attribute, value, request.params)
|
69
|
+
end
|
70
|
+
|
71
|
+
def extract_resource
|
72
|
+
parent_key = (request.params.keys - IGNORE_PARAMS).first
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
77
|
+
|
78
|
+
class Engine < ::Rails::Engine
|
79
|
+
config.app_middleware.use ClientSideValidations::Middleware::Validators
|
80
|
+
end
|
81
|
+
|
82
|
+
end
|
83
|
+
|