client_side_validations 2.9.6 → 2.9.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -29,7 +29,7 @@ module ClientSideValidations
|
|
29
29
|
# the first element of the array for id, even if it is nil
|
30
30
|
id = [params["#{resource}[id]"]].flatten.first
|
31
31
|
body = is_unique?(resource, attribute, value, id).to_s
|
32
|
-
[200, {'Content-Type' => 'application/json', 'Content-Length' => "#{body.length}"}, body]
|
32
|
+
[200, {'Content-Type' => 'application/json', 'Content-Length' => "#{body.length}"}, [body]]
|
33
33
|
else
|
34
34
|
@app.call(env)
|
35
35
|
end
|
@@ -9,17 +9,18 @@ module ClientSideValidations
|
|
9
9
|
attr_accessor :validate_options
|
10
10
|
|
11
11
|
define_method(:form_for) do |record_or_name_or_array, *args, &proc|
|
12
|
-
options =
|
13
|
-
options.merge!(args.extract_options!.symbolize_keys!)
|
12
|
+
options = args.extract_options!.symbolize_keys!
|
14
13
|
script = ""
|
15
14
|
if validations = options.delete(:validations)
|
16
|
-
set_validate_options(validations, record_or_name_or_array)
|
15
|
+
set_validate_options(validations, record_or_name_or_array, options)
|
16
|
+
unless options.has_key?(:html)
|
17
|
+
options[:html] = {}
|
18
|
+
end
|
17
19
|
options[:html]['data-csv'] = validate_options.delete('data-csv')
|
18
20
|
script = %{<script type='text/javascript'>var #{options[:html]['data-csv']}_validate_options=#{validate_options.to_json};</script>}
|
19
21
|
end
|
20
|
-
args <<
|
22
|
+
args << options
|
21
23
|
result = form_method.bind(self).call(record_or_name_or_array, *args, &proc)
|
22
|
-
|
23
24
|
if rails3?
|
24
25
|
result += script.html_safe
|
25
26
|
else
|
@@ -40,34 +41,34 @@ module ClientSideValidations
|
|
40
41
|
!version.blank? && version >= 3
|
41
42
|
end
|
42
43
|
|
43
|
-
def set_validate_options(validations, record_or_name_or_array)
|
44
|
+
def set_validate_options(validations, record_or_name_or_array, form_for_options)
|
44
45
|
options = ::ClientSideValidations.default_options || { }
|
45
46
|
|
46
47
|
case validations
|
47
48
|
when true
|
48
|
-
object_name = get_object_name(record_or_name_or_array)
|
49
|
-
data_csv = get_data_csv(record_or_name_or_array)
|
49
|
+
object_name = get_object_name(record_or_name_or_array, form_for_options)
|
50
|
+
data_csv = get_data_csv(record_or_name_or_array, form_for_options)
|
50
51
|
validate_options = rules_and_messages(record_or_name_or_array)
|
51
52
|
when Hash
|
52
53
|
validations.symbolize_keys!
|
53
54
|
if validations.has_key?(:instance)
|
54
|
-
object_name = get_object_name(validations[:instance])
|
55
|
-
data_csv = get_data_csv(validations[:instance])
|
55
|
+
object_name = get_object_name(validations[:instance], form_for_options)
|
56
|
+
data_csv = get_data_csv(validations[:instance], form_for_options)
|
56
57
|
validate_options = validations[:instance].validate_options
|
57
58
|
elsif validations.has_key?(:class)
|
58
|
-
object_name = get_object_name(validations[:class]
|
59
|
-
data_csv = get_data_csv(validations[:
|
59
|
+
object_name = get_object_name(validations[:class], form_for_options)
|
60
|
+
data_csv = get_data_csv(validations[:class], form_for_options)
|
60
61
|
validate_options = validations[:class].new.validate_options
|
61
62
|
else
|
62
|
-
object_name = get_object_name(record_or_name_or_array)
|
63
|
-
data_csv = get_data_csv(record_or_name_or_array)
|
63
|
+
object_name = get_object_name(record_or_name_or_array, form_for_options)
|
64
|
+
data_csv = get_data_csv(record_or_name_or_array, form_for_options)
|
64
65
|
validate_options = rules_and_messages(record_or_name_or_array)
|
65
66
|
end
|
66
67
|
|
67
68
|
options.merge!(validations[:options] || {})
|
68
69
|
else
|
69
|
-
object_name = get_object_name(validations)
|
70
|
-
data_csv = get_data_csv(validations)
|
70
|
+
object_name = get_object_name(validations, form_for_options)
|
71
|
+
data_csv = get_data_csv(validations, form_for_options)
|
71
72
|
validate_options = validations.new.validate_options
|
72
73
|
end
|
73
74
|
|
@@ -86,14 +87,6 @@ module ClientSideValidations
|
|
86
87
|
validate_options
|
87
88
|
end
|
88
89
|
|
89
|
-
def convert_options_to_json(options)
|
90
|
-
json = []
|
91
|
-
options.each do |k, v|
|
92
|
-
json << %{#{k}:#{v}}
|
93
|
-
end
|
94
|
-
%{{#{json.join(',')}}}
|
95
|
-
end
|
96
|
-
|
97
90
|
def rules_and_messages(record_or_name_or_array)
|
98
91
|
case record_or_name_or_array
|
99
92
|
when String, Symbol
|
@@ -105,32 +98,36 @@ module ClientSideValidations
|
|
105
98
|
end
|
106
99
|
end
|
107
100
|
|
108
|
-
def get_data_csv(object)
|
101
|
+
def get_data_csv(object, form_for_options)
|
109
102
|
case object
|
110
103
|
when String, Symbol
|
111
|
-
object.to_s
|
104
|
+
form_for_options[:as] || object.to_s
|
112
105
|
when Array
|
113
|
-
get_data_csv(object.last)
|
106
|
+
get_data_csv(object.last, form_for_options)
|
114
107
|
when Class
|
115
|
-
object.to_s.underscore
|
108
|
+
form_for_options[:as] || object.to_s.underscore
|
116
109
|
else
|
117
|
-
|
110
|
+
if object.respond_to?(:persisted?) && object.persisted?
|
111
|
+
form_for_options[:as] ? "#{form_for_options[:as]}_edit" : dom_id(object)
|
112
|
+
else
|
113
|
+
form_for_options[:as] ? "#{form_for_options[:as]}_new" : dom_id(object)
|
114
|
+
end
|
118
115
|
end
|
119
116
|
end
|
120
117
|
|
121
|
-
def get_object_name(object)
|
118
|
+
def get_object_name(object, form_for_options)
|
122
119
|
case object
|
123
120
|
when String, Symbol
|
124
|
-
object.to_s
|
121
|
+
form_for_options[:as] || object.to_s
|
125
122
|
when Array
|
126
|
-
get_object_name(object.last)
|
123
|
+
get_object_name(object.last, form_for_options)
|
127
124
|
when Class
|
128
|
-
get_object_name(object.new)
|
125
|
+
get_object_name(object.new, form_for_options)
|
129
126
|
else
|
130
127
|
if rails3?
|
131
|
-
::ActiveModel::Naming.singular(object)
|
128
|
+
form_for_options[:as] || ::ActiveModel::Naming.singular(object)
|
132
129
|
else
|
133
|
-
::ActionController::RecordIdentifier.singular_class_name(object)
|
130
|
+
form_for_options[:as] || ::ActionController::RecordIdentifier.singular_class_name(object)
|
134
131
|
end
|
135
132
|
end
|
136
133
|
end
|
@@ -73,7 +73,11 @@ module ClientSideValidations
|
|
73
73
|
if defined?(ActiveRecord) && base.kind_of?(ActiveRecord::Base)
|
74
74
|
I18n.translate('activerecord.errors.messages.taken')
|
75
75
|
elsif defined?(Mongoid) && base.class.included_modules.include?(Mongoid::Document)
|
76
|
-
|
76
|
+
if ruby18?
|
77
|
+
I18n.translate('errors.messages.taken')
|
78
|
+
else
|
79
|
+
I18n.translate('activemodel.errors.messages.taken')
|
80
|
+
end
|
77
81
|
end
|
78
82
|
when :confirmation
|
79
83
|
I18n.translate(i18n_prefix + 'errors.messages.confirmation')
|
@@ -99,6 +103,18 @@ module ClientSideValidations
|
|
99
103
|
validation.kind.to_s
|
100
104
|
end
|
101
105
|
|
106
|
+
def ruby18?
|
107
|
+
ruby_split[0] == '1' && ruby_split[1] == '8'
|
108
|
+
end
|
109
|
+
|
110
|
+
def ruby19?
|
111
|
+
ruby_split[0] == '1' && ruby_split[1] == '9'
|
112
|
+
end
|
113
|
+
|
114
|
+
def ruby_split
|
115
|
+
RUBY_VERSION.split('.')
|
116
|
+
end
|
117
|
+
|
102
118
|
def i18n_prefix
|
103
119
|
if defined?(::ActiveModel)
|
104
120
|
''
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: client_side_validations
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 59
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 2
|
8
8
|
- 9
|
9
|
-
-
|
10
|
-
version: 2.9.
|
9
|
+
- 8
|
10
|
+
version: 2.9.8
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Brian Cardarella
|