default_validations 1.0.1
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.
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
require File.expand_path('../lib/default_validations/version', __FILE__)
|
|
3
|
+
|
|
4
|
+
Gem::Specification.new do |s|
|
|
5
|
+
s.name = 'default_validations'
|
|
6
|
+
s.version = DefaultValidations::VERSION
|
|
7
|
+
s.platform = Gem::Platform::RUBY
|
|
8
|
+
s.license = 'MIT'
|
|
9
|
+
s.author = 'Pavel Astraukh'
|
|
10
|
+
s.email = 'paladin111333@gmail.com'
|
|
11
|
+
s.description = 'Default validations for string and integer attributes'
|
|
12
|
+
s.homepage = 'http://github.com/enotpoloskun/iam'
|
|
13
|
+
s.summary = 'Simple default validations creator for Rails'
|
|
14
|
+
|
|
15
|
+
s.files = `git ls-files`.split("\n")
|
|
16
|
+
s.require_path = 'lib'
|
|
17
|
+
|
|
18
|
+
s.add_dependency 'activesupport', '~> 3.1'
|
|
19
|
+
|
|
20
|
+
s.rubyforge_project = s.name
|
|
21
|
+
end
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
module DefaultValidations
|
|
2
|
+
module Configuration
|
|
3
|
+
extend ActiveSupport::Concern
|
|
4
|
+
include ActiveSupport::Configurable
|
|
5
|
+
|
|
6
|
+
included do
|
|
7
|
+
config_accessor :enabled, :models, :excluded_models, :ignore_carrierwave_uploaders, :string_length, :integer_max_value, :validate_strings, :validate_integers
|
|
8
|
+
|
|
9
|
+
self.configure do |config|
|
|
10
|
+
config.validate_strings = true
|
|
11
|
+
config.validate_integers = true
|
|
12
|
+
config.enabled = true
|
|
13
|
+
config.models = []
|
|
14
|
+
config.excluded_models = []
|
|
15
|
+
config.ignore_carrierwave_uploaders = false
|
|
16
|
+
config.string_length = 255
|
|
17
|
+
config.integer_max_value = 2147483647
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
module DefaultValidations
|
|
2
|
+
class ValidationsCreator
|
|
3
|
+
class << self
|
|
4
|
+
|
|
5
|
+
def create_validations
|
|
6
|
+
models_to_validate.each do |klass|
|
|
7
|
+
create_string_validations_for_class(klass) if DefaultValidations.validate_strings
|
|
8
|
+
create_integer_validations_for_class(klass) if DefaultValidations.validate_integers
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def create_string_validations_for_class(klass)
|
|
14
|
+
not_validated_attributes = not_validated_string_attributes_for_class(klass)
|
|
15
|
+
if not_validated_attributes.present?
|
|
16
|
+
klass.class_eval do
|
|
17
|
+
validates *not_validated_attributes, length: { maximum: DefaultValidations.string_length }
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def create_integer_validations_for_class(klass)
|
|
23
|
+
not_validated_attributes = not_validated_integer_attributes_for_class(klass)
|
|
24
|
+
if not_validated_attributes.present?
|
|
25
|
+
klass.class_eval do
|
|
26
|
+
validates *not_validated_attributes, numericality: { greater_than: -DefaultValidations.integer_max_value, less_than: DefaultValidations.integer_max_value }
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
private
|
|
32
|
+
def all_models_with_table
|
|
33
|
+
ActiveRecord::Base.descendants.select(&:table_exists?)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def models_to_validate
|
|
37
|
+
@@models ||= DefaultValidations.models.empty? ? (all_models_with_table - DefaultValidations.excluded_models)
|
|
38
|
+
: DefaultValidations.models
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def carrier_wave_uploader_attributes(klass)
|
|
42
|
+
DefaultValidations.ignore_carrierwave_uploaders ? klass.uploaders.keys.map(&:to_s) : []
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def not_validated_string_attributes_for_class(klass)
|
|
46
|
+
not_validated_attributes = klass.column_names.select do |name|
|
|
47
|
+
klass.columns_hash[name].type == :string && !klass.validators_on(name).map(&:class).include?(ActiveModel::Validations::LengthValidator)
|
|
48
|
+
end - carrier_wave_uploader_attributes(klass)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def not_validated_integer_attributes_for_class(klass)
|
|
52
|
+
not_validated_attributes = klass.column_names.select do |name|
|
|
53
|
+
klass.columns_hash[name].type == :integer && !klass.validators_on(name).map(&:class).include?(ActiveModel::Validations::NumericalityValidator)
|
|
54
|
+
end - excluded_primary_and_foreign_keys_for_class(klass)
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def excluded_primary_and_foreign_keys_for_class(klass)
|
|
58
|
+
klass.reflect_on_all_associations(:belongs_to).map(&:foreign_key) << klass.primary_key
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: default_validations
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
prerelease: false
|
|
5
|
+
segments:
|
|
6
|
+
- 1
|
|
7
|
+
- 0
|
|
8
|
+
- 1
|
|
9
|
+
version: 1.0.1
|
|
10
|
+
platform: ruby
|
|
11
|
+
authors:
|
|
12
|
+
- Pavel Astraukh
|
|
13
|
+
autorequire:
|
|
14
|
+
bindir: bin
|
|
15
|
+
cert_chain: []
|
|
16
|
+
|
|
17
|
+
date: 2013-02-24 00:00:00 +03:00
|
|
18
|
+
default_executable:
|
|
19
|
+
dependencies:
|
|
20
|
+
- !ruby/object:Gem::Dependency
|
|
21
|
+
name: activesupport
|
|
22
|
+
prerelease: false
|
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
|
24
|
+
requirements:
|
|
25
|
+
- - ~>
|
|
26
|
+
- !ruby/object:Gem::Version
|
|
27
|
+
segments:
|
|
28
|
+
- 3
|
|
29
|
+
- 1
|
|
30
|
+
version: "3.1"
|
|
31
|
+
type: :runtime
|
|
32
|
+
version_requirements: *id001
|
|
33
|
+
description: Default validations for string and integer attributes
|
|
34
|
+
email: paladin111333@gmail.com
|
|
35
|
+
executables: []
|
|
36
|
+
|
|
37
|
+
extensions: []
|
|
38
|
+
|
|
39
|
+
extra_rdoc_files: []
|
|
40
|
+
|
|
41
|
+
files:
|
|
42
|
+
- default_validations.gemspec
|
|
43
|
+
- lib/default_validations.rb
|
|
44
|
+
- lib/default_validations/configuration.rb
|
|
45
|
+
- lib/default_validations/railtie.rb
|
|
46
|
+
- lib/default_validations/validations_creator.rb
|
|
47
|
+
- lib/default_validations/version.rb
|
|
48
|
+
has_rdoc: true
|
|
49
|
+
homepage: http://github.com/enotpoloskun/iam
|
|
50
|
+
licenses:
|
|
51
|
+
- MIT
|
|
52
|
+
post_install_message:
|
|
53
|
+
rdoc_options: []
|
|
54
|
+
|
|
55
|
+
require_paths:
|
|
56
|
+
- lib
|
|
57
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
58
|
+
requirements:
|
|
59
|
+
- - ">="
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
segments:
|
|
62
|
+
- 0
|
|
63
|
+
version: "0"
|
|
64
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
65
|
+
requirements:
|
|
66
|
+
- - ">="
|
|
67
|
+
- !ruby/object:Gem::Version
|
|
68
|
+
segments:
|
|
69
|
+
- 0
|
|
70
|
+
version: "0"
|
|
71
|
+
requirements: []
|
|
72
|
+
|
|
73
|
+
rubyforge_project: default_validations
|
|
74
|
+
rubygems_version: 1.3.6
|
|
75
|
+
signing_key:
|
|
76
|
+
specification_version: 3
|
|
77
|
+
summary: Simple default validations creator for Rails
|
|
78
|
+
test_files: []
|
|
79
|
+
|