comply 1.2.0 → 1.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/app/assets/javascripts/comply/base_validatable_form.js.coffee +53 -0
- data/app/assets/javascripts/comply/validatable_form.js.coffee +1 -40
- data/app/assets/javascripts/comply/validatable_input.js.coffee +6 -2
- data/app/controllers/comply/validations_controller.rb +12 -2
- data/lib/comply/version.rb +1 -1
- data/lib/comply/whitelist_constantize.rb +62 -0
- metadata +35 -48
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ee73736d6989d2b6f909ef8e39385d7cd8bf4d72
|
4
|
+
data.tar.gz: b22aacb5881c0bb6116e7d7b7122f392f8b821e1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 277312618b13c1b4b648a6f10f2ae14a13a12c4c8ee73f6fb73771e9f5d1e4525bdebbd53e43f3cde0196f2bc4e51c2f2510362778af9b89e02dc462ca6dacff
|
7
|
+
data.tar.gz: 8bb27b8ea2a7cd92b88209980ea9222806c3f1704c1e5b90aee2d098c86836f285cd4281060feb23e3120de022fbe64e218847cdcd4d07b35f7480767019e0fe
|
@@ -0,0 +1,53 @@
|
|
1
|
+
class Comply.BaseValidatableForm
|
2
|
+
constructor: (@$form) ->
|
3
|
+
return if @$form.data('complied')
|
4
|
+
@$form.data('complied', true)
|
5
|
+
@inputs = @_inputs()
|
6
|
+
@model = @$form.data('validate-model')
|
7
|
+
@$button = @$form.find('[type=submit]')
|
8
|
+
@validationRoute = @$form.data('validate-route') or "/#{Comply.enginePath}/validations"
|
9
|
+
@$button.click @validateInputs
|
10
|
+
@$button.attr('disabled', false).removeClass('disabled')
|
11
|
+
|
12
|
+
validateInputs: (event) =>
|
13
|
+
event.preventDefault()
|
14
|
+
@validate inputs: @inputs, submit: true
|
15
|
+
|
16
|
+
validate: (options) ->
|
17
|
+
$.get @validationRoute, @_params(), (response) =>
|
18
|
+
@_onValidate(response, options)
|
19
|
+
|
20
|
+
if @_allSuccess(response)
|
21
|
+
if @_onValidationSuccess(response, options)
|
22
|
+
return @$form.submit() if options.submit
|
23
|
+
else
|
24
|
+
@_onValidationFailure(response, options)
|
25
|
+
|
26
|
+
if @_onValidationComplete(response, options)
|
27
|
+
@_setMessages(options.inputs, response)
|
28
|
+
|
29
|
+
# private
|
30
|
+
|
31
|
+
_onValidate: (response, options) ->
|
32
|
+
_onValidationSuccess: (response, options) -> true
|
33
|
+
_onValidationFailure: (response, options) -> true
|
34
|
+
_onValidationComplete: (response, options) -> true
|
35
|
+
|
36
|
+
_setMessages: (inputs, response) =>
|
37
|
+
for input in inputs
|
38
|
+
status = @_isSuccess(input, response)
|
39
|
+
input.setMessage status, @_responseValue(input, response, status)
|
40
|
+
|
41
|
+
_allSuccess: (response) -> not (err for field, err of response.error when err.length).length
|
42
|
+
|
43
|
+
_inputs: ->
|
44
|
+
for input in @$form.find('[data-validate][name]')
|
45
|
+
new Comply.ValidatableInput $(input), this
|
46
|
+
|
47
|
+
_isSuccess: (input, response) ->
|
48
|
+
if !!@_responseValue(input, response) then 'error' else 'success'
|
49
|
+
|
50
|
+
_params: -> "#{$.param(model: @model)}&#{@$form.serialize()}"
|
51
|
+
|
52
|
+
_responseValue: (input, response, status = 'error') ->
|
53
|
+
if response[status]? then response[status][input.attrName]
|
@@ -1,40 +1 @@
|
|
1
|
-
class Comply.ValidatableForm
|
2
|
-
constructor: (@$form) ->
|
3
|
-
return if @$form.data('complied')
|
4
|
-
@$form.data('complied', true)
|
5
|
-
@inputs = @_inputs()
|
6
|
-
@model = @$form.data('validate-model')
|
7
|
-
@$button = @$form.find('[type=submit]')
|
8
|
-
@validationRoute = @$form.data('validate-route') or "/#{Comply.enginePath}/validations"
|
9
|
-
@$button.click @validateInputs
|
10
|
-
@$button.attr('disabled', false).removeClass('disabled')
|
11
|
-
|
12
|
-
validateInputs: (event) =>
|
13
|
-
event.preventDefault()
|
14
|
-
@validate inputs: @inputs, submit: true
|
15
|
-
|
16
|
-
validate: (options) ->
|
17
|
-
$.get @validationRoute, @_params(), (response) =>
|
18
|
-
return @$form.submit() if options.submit and @_allSuccess(response)
|
19
|
-
@_setMessages(options.inputs, response)
|
20
|
-
|
21
|
-
# private
|
22
|
-
|
23
|
-
_setMessages: (inputs, response) =>
|
24
|
-
for input in inputs
|
25
|
-
status = @_isSuccess(input, response)
|
26
|
-
input.setMessage status, @_responseValue(input, response, status)
|
27
|
-
|
28
|
-
_allSuccess: (response) -> not (err for field, err of response.error when err.length).length
|
29
|
-
|
30
|
-
_inputs: ->
|
31
|
-
for input in @$form.find('[data-validate][name]')
|
32
|
-
new Comply.ValidatableInput $(input), this
|
33
|
-
|
34
|
-
_isSuccess: (input, response) ->
|
35
|
-
if !!@_responseValue(input, response) then 'error' else 'success'
|
36
|
-
|
37
|
-
_params: -> "#{$.param(model: @model)}&#{@$form.serialize()}"
|
38
|
-
|
39
|
-
_responseValue: (input, response, status = 'error') ->
|
40
|
-
if response[status]? then response[status][input.attrName]
|
1
|
+
class Comply.ValidatableForm extends Comply.BaseValidatableForm
|
@@ -22,7 +22,10 @@ class Comply.ValidatableInput
|
|
22
22
|
#private
|
23
23
|
|
24
24
|
_dependency: ->
|
25
|
-
|
25
|
+
if @$el.data('validate-with')?
|
26
|
+
new Comply.ValidatableInput(
|
27
|
+
$("#{@$el.data('validate-with')}[name]"), @form
|
28
|
+
)
|
26
29
|
|
27
30
|
_forceValidate: -> @forceValidate or= @$el.data('validate-force')
|
28
31
|
|
@@ -34,7 +37,8 @@ class Comply.ValidatableInput
|
|
34
37
|
_$multiparamFields: ->
|
35
38
|
@$multiparamFields or= $(field) for field in @_multiparamFields()
|
36
39
|
|
37
|
-
_submitValidations: =>
|
40
|
+
_submitValidations: =>
|
41
|
+
@form.validate inputs: (el for el in [this, @dependency] when el)
|
38
42
|
|
39
43
|
_validatable: ->
|
40
44
|
return true if @_forceValidate() or not @_multiparam()
|
@@ -1,9 +1,19 @@
|
|
1
|
+
require 'comply/whitelist_constantize'
|
2
|
+
begin
|
3
|
+
require 'strong_parameters'
|
4
|
+
rescue LoadError
|
5
|
+
end
|
6
|
+
|
1
7
|
module Comply
|
2
8
|
class ValidationsController < Comply::ApplicationController
|
3
9
|
ssl_allowed :show
|
4
10
|
|
5
11
|
before_filter :require_model, :require_fields
|
6
12
|
|
13
|
+
if ActiveModel.const_defined?(:ForbiddenAttributesProtection)
|
14
|
+
include ActiveModel::ForbiddenAttributesProtection
|
15
|
+
end
|
16
|
+
|
7
17
|
def show
|
8
18
|
@instance = validation_instance
|
9
19
|
@instance.valid?
|
@@ -26,8 +36,8 @@ module Comply
|
|
26
36
|
end
|
27
37
|
|
28
38
|
def require_model
|
29
|
-
@model = params[:model]
|
30
|
-
rescue NameError
|
39
|
+
@model = Comply::WhitelistConstantize.constantize(params[:model]) if params[:model].present?
|
40
|
+
rescue NameError, Comply::WhitelistConstantize::NotWhitelistedError
|
31
41
|
@model = nil
|
32
42
|
ensure
|
33
43
|
render json: { error: 'Model not found' }, status: 500 unless @model
|
data/lib/comply/version.rb
CHANGED
@@ -0,0 +1,62 @@
|
|
1
|
+
require 'active_model/validations'
|
2
|
+
|
3
|
+
module Comply
|
4
|
+
module WhitelistConstantize
|
5
|
+
|
6
|
+
class MatcherFactory
|
7
|
+
attr_reader :obj, :options
|
8
|
+
def initialize(obj, options = {})
|
9
|
+
@obj = obj
|
10
|
+
@options = options
|
11
|
+
end
|
12
|
+
|
13
|
+
def to_matcher
|
14
|
+
if obj.is_a?(Module) && options[:include_inheritance]
|
15
|
+
procify_module
|
16
|
+
else
|
17
|
+
obj
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def procify_module
|
24
|
+
-> (klass) { klass <= obj }
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
class NotWhitelistedError < ArgumentError
|
29
|
+
end
|
30
|
+
|
31
|
+
class << self
|
32
|
+
DEFAULT_PROC = MatcherFactory.new(ActiveModel::Validations, include_inheritance: true).to_matcher
|
33
|
+
|
34
|
+
def constantize(str)
|
35
|
+
whitelist!(str.classify.constantize)
|
36
|
+
end
|
37
|
+
|
38
|
+
def whitelist
|
39
|
+
@whitelist ||= [DEFAULT_PROC]
|
40
|
+
end
|
41
|
+
|
42
|
+
# obj can be a Class, Module or Proc.
|
43
|
+
# For classes and modules, optionally add `include_inheritance: true` to
|
44
|
+
# also match subclasses.
|
45
|
+
# Procs should accept one argument.
|
46
|
+
def allow(obj, opts = {})
|
47
|
+
whitelist << MatcherFactory.new(obj, opts).to_matcher
|
48
|
+
end
|
49
|
+
|
50
|
+
private
|
51
|
+
|
52
|
+
def whitelist!(obj)
|
53
|
+
obj.tap do |klass|
|
54
|
+
whitelist.detect(-> { raise NotWhitelistedError }) do |matcher|
|
55
|
+
matcher === klass || matcher == klass
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
end
|
metadata
CHANGED
@@ -1,129 +1,115 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: comply
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
|
-
-
|
8
|
-
-
|
9
|
-
-
|
10
|
-
-
|
11
|
-
-
|
7
|
+
- "@jacobaweiss"
|
8
|
+
- "@andyjbas"
|
9
|
+
- "@azach"
|
10
|
+
- "@linedotstar"
|
11
|
+
- "@cfurrow"
|
12
12
|
autorequire:
|
13
13
|
bindir: bin
|
14
14
|
cert_chain: []
|
15
|
-
date:
|
15
|
+
date: 2015-04-29 00:00:00.000000000 Z
|
16
16
|
dependencies:
|
17
17
|
- !ruby/object:Gem::Dependency
|
18
18
|
name: coffee-rails
|
19
19
|
requirement: !ruby/object:Gem::Requirement
|
20
20
|
requirements:
|
21
|
-
- -
|
21
|
+
- - ">="
|
22
22
|
- !ruby/object:Gem::Version
|
23
23
|
version: '0'
|
24
24
|
type: :runtime
|
25
25
|
prerelease: false
|
26
26
|
version_requirements: !ruby/object:Gem::Requirement
|
27
27
|
requirements:
|
28
|
-
- -
|
28
|
+
- - ">="
|
29
29
|
- !ruby/object:Gem::Version
|
30
30
|
version: '0'
|
31
31
|
- !ruby/object:Gem::Dependency
|
32
32
|
name: bartt-ssl_requirement
|
33
33
|
requirement: !ruby/object:Gem::Requirement
|
34
34
|
requirements:
|
35
|
-
- - ~>
|
35
|
+
- - "~>"
|
36
36
|
- !ruby/object:Gem::Version
|
37
37
|
version: 1.4.2
|
38
38
|
type: :runtime
|
39
39
|
prerelease: false
|
40
40
|
version_requirements: !ruby/object:Gem::Requirement
|
41
41
|
requirements:
|
42
|
-
- - ~>
|
42
|
+
- - "~>"
|
43
43
|
- !ruby/object:Gem::Version
|
44
44
|
version: 1.4.2
|
45
45
|
- !ruby/object:Gem::Dependency
|
46
46
|
name: rails
|
47
47
|
requirement: !ruby/object:Gem::Requirement
|
48
48
|
requirements:
|
49
|
-
- -
|
49
|
+
- - "~>"
|
50
50
|
- !ruby/object:Gem::Version
|
51
|
-
version:
|
51
|
+
version: 3.2.0
|
52
52
|
type: :runtime
|
53
53
|
prerelease: false
|
54
54
|
version_requirements: !ruby/object:Gem::Requirement
|
55
55
|
requirements:
|
56
|
-
- -
|
56
|
+
- - "~>"
|
57
57
|
- !ruby/object:Gem::Version
|
58
|
-
version:
|
58
|
+
version: 3.2.0
|
59
59
|
- !ruby/object:Gem::Dependency
|
60
|
-
name:
|
60
|
+
name: strong_parameters
|
61
61
|
requirement: !ruby/object:Gem::Requirement
|
62
62
|
requirements:
|
63
|
-
- -
|
63
|
+
- - "~>"
|
64
64
|
- !ruby/object:Gem::Version
|
65
|
-
version:
|
66
|
-
type: :
|
67
|
-
prerelease: false
|
68
|
-
version_requirements: !ruby/object:Gem::Requirement
|
69
|
-
requirements:
|
70
|
-
- - '>='
|
71
|
-
- !ruby/object:Gem::Version
|
72
|
-
version: '0'
|
73
|
-
- !ruby/object:Gem::Dependency
|
74
|
-
name: sinon-rails
|
75
|
-
requirement: !ruby/object:Gem::Requirement
|
76
|
-
requirements:
|
77
|
-
- - '>='
|
78
|
-
- !ruby/object:Gem::Version
|
79
|
-
version: '0'
|
80
|
-
type: :development
|
65
|
+
version: 0.2.0
|
66
|
+
type: :runtime
|
81
67
|
prerelease: false
|
82
68
|
version_requirements: !ruby/object:Gem::Requirement
|
83
69
|
requirements:
|
84
|
-
- -
|
70
|
+
- - "~>"
|
85
71
|
- !ruby/object:Gem::Version
|
86
|
-
version:
|
72
|
+
version: 0.2.0
|
87
73
|
- !ruby/object:Gem::Dependency
|
88
|
-
name: pry
|
74
|
+
name: pry-nav
|
89
75
|
requirement: !ruby/object:Gem::Requirement
|
90
76
|
requirements:
|
91
|
-
- -
|
77
|
+
- - ">="
|
92
78
|
- !ruby/object:Gem::Version
|
93
79
|
version: '0'
|
94
80
|
type: :development
|
95
81
|
prerelease: false
|
96
82
|
version_requirements: !ruby/object:Gem::Requirement
|
97
83
|
requirements:
|
98
|
-
- -
|
84
|
+
- - ">="
|
99
85
|
- !ruby/object:Gem::Version
|
100
86
|
version: '0'
|
101
87
|
- !ruby/object:Gem::Dependency
|
102
88
|
name: rspec-rails
|
103
89
|
requirement: !ruby/object:Gem::Requirement
|
104
90
|
requirements:
|
105
|
-
- - ~>
|
91
|
+
- - "~>"
|
106
92
|
- !ruby/object:Gem::Version
|
107
|
-
version:
|
93
|
+
version: 3.2.0
|
108
94
|
type: :development
|
109
95
|
prerelease: false
|
110
96
|
version_requirements: !ruby/object:Gem::Requirement
|
111
97
|
requirements:
|
112
|
-
- - ~>
|
98
|
+
- - "~>"
|
113
99
|
- !ruby/object:Gem::Version
|
114
|
-
version:
|
100
|
+
version: 3.2.0
|
115
101
|
- !ruby/object:Gem::Dependency
|
116
102
|
name: sqlite3
|
117
103
|
requirement: !ruby/object:Gem::Requirement
|
118
104
|
requirements:
|
119
|
-
- -
|
105
|
+
- - ">="
|
120
106
|
- !ruby/object:Gem::Version
|
121
107
|
version: '0'
|
122
108
|
type: :development
|
123
109
|
prerelease: false
|
124
110
|
version_requirements: !ruby/object:Gem::Requirement
|
125
111
|
requirements:
|
126
|
-
- -
|
112
|
+
- - ">="
|
127
113
|
- !ruby/object:Gem::Version
|
128
114
|
version: '0'
|
129
115
|
description: Validate your ActiveRecord models on the client, showing their error
|
@@ -137,6 +123,7 @@ files:
|
|
137
123
|
- MIT-LICENSE
|
138
124
|
- README.md
|
139
125
|
- Rakefile
|
126
|
+
- app/assets/javascripts/comply/base_validatable_form.js.coffee
|
140
127
|
- app/assets/javascripts/comply/base_validation_message.js.coffee
|
141
128
|
- app/assets/javascripts/comply/config.js.coffee
|
142
129
|
- app/assets/javascripts/comply/index.js.coffee
|
@@ -149,6 +136,7 @@ files:
|
|
149
136
|
- lib/comply.rb
|
150
137
|
- lib/comply/engine.rb
|
151
138
|
- lib/comply/version.rb
|
139
|
+
- lib/comply/whitelist_constantize.rb
|
152
140
|
homepage: http://www.github.com/lumoslabs/comply
|
153
141
|
licenses: []
|
154
142
|
metadata: {}
|
@@ -158,19 +146,18 @@ require_paths:
|
|
158
146
|
- lib
|
159
147
|
required_ruby_version: !ruby/object:Gem::Requirement
|
160
148
|
requirements:
|
161
|
-
- -
|
149
|
+
- - ">="
|
162
150
|
- !ruby/object:Gem::Version
|
163
151
|
version: '0'
|
164
152
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
165
153
|
requirements:
|
166
|
-
- -
|
154
|
+
- - ">="
|
167
155
|
- !ruby/object:Gem::Version
|
168
156
|
version: '0'
|
169
157
|
requirements: []
|
170
158
|
rubyforge_project:
|
171
|
-
rubygems_version: 2.
|
159
|
+
rubygems_version: 2.2.3
|
172
160
|
signing_key:
|
173
161
|
specification_version: 4
|
174
162
|
summary: Inline validation of your ActiveRecord models via the AJAX internets
|
175
163
|
test_files: []
|
176
|
-
has_rdoc:
|