database-validation 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ccbffb8c493a14309b3b913fce6bfb81150cb1d1
4
+ data.tar.gz: 1f45b7bbf708818b5dd99886d346d471eb3cf42f
5
+ SHA512:
6
+ metadata.gz: 0a24af486380efd0e4a6c019a18595b3d81c395cc8792d03e137f4d05e130e59f6803b84e05864cd57f59495cb95ba7fd230af009ab65aaaf573e2b6f17099d4
7
+ data.tar.gz: 9b7a019fb0887d9f7dc85d2888149c831e0b923ba24d7e04995bc5014aab5092011da3573f59511b0e59e971670b5dfde14d93cb06033670125efa95da05fe15
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .DS_store
@@ -0,0 +1,3 @@
1
+ ## v0.1.1
2
+
3
+ * Just strings are going to be validated
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in database_validations.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Agustin Anfuso
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,64 @@
1
+ # Database Validation
2
+ [![Code Climate](https://codeclimate.com/github/eventioz/database-validation.png)](https://codeclimate.com/github/eventioz/database-validation)
3
+
4
+ Is a simple way to add Active Record Validations to models and views through the limits established in your database.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'database-validation'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install database-validation
19
+
20
+ ## Usage
21
+
22
+ ### Models Validation
23
+
24
+ Add this method to all the models that you want to add validations to
25
+ all its attributes.
26
+
27
+ class SomeModel < ActiveRecord::Base
28
+ validate_limits
29
+
30
+ # your stuff
31
+ end
32
+
33
+ Or you can pass, as an option, an array with the attributes you want to
34
+ be skipped for validations OR the few you want to validate.
35
+
36
+ validate_limits only: [:email]
37
+
38
+ or
39
+
40
+ validate_limits except: [:email, :credit_card]
41
+
42
+ ### Views Validation
43
+
44
+ form_for **must** have an associated object.
45
+
46
+ <%= form_for @object do |f| %>
47
+
48
+ form_for automatically adds *maxlength* option with the value
49
+ corresponding to the limit of the column in the database to all its text_fields.
50
+
51
+ However, you can overwrite *maxlength* option with any value you want
52
+ or...
53
+
54
+ Add *skip_validations* option to your form and set it to true to prevent gem from adding validations.
55
+
56
+ <%= form_for @object, skip_validations: true do |f| %>
57
+
58
+ ## Contributing
59
+
60
+ 1. Fork it
61
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
62
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
63
+ 4. Push to the branch (`git push origin my-new-feature`)
64
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'database_validation/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "database-validation"
8
+ spec.version = DatabaseValidation::VERSION
9
+ spec.authors = ["Agustin Anfuso"]
10
+ spec.email = ["anfusoagustin89@gmail.com"]
11
+ spec.summary = %q{Length limit validations through database for Rails.}
12
+ spec.description = %q{Simple way to add Active Record Validations to models and views through the limits established in your database.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ end
@@ -0,0 +1,4 @@
1
+ require "database_validation/version"
2
+ require "database_validation/model_additions"
3
+ require "database_validation/view_helpers"
4
+ require "database_validation/railtie" if defined? Rails
@@ -0,0 +1,45 @@
1
+ module DatabaseValidation
2
+ module ModelAdditions
3
+ # Avoids adding LengthValidator to boolean, date or datetime attributes.
4
+ AVAILABLE_TYPES = [ :string ]
5
+
6
+ def limit_for(attr)
7
+ columns_hash.with_indifferent_access[attr].limit if column_belongs?(attr)
8
+ end
9
+
10
+ def type_for(attr)
11
+ columns_hash.with_indifferent_access[attr].type if column_belongs?(attr)
12
+ end
13
+
14
+ def valid_limit?(attr)
15
+ limit_for(attr) != nil
16
+ end
17
+
18
+ def valid_type?(attr)
19
+ AVAILABLE_TYPES.include?(type_for(attr))
20
+ end
21
+
22
+ def column_belongs?(attr)
23
+ column_names.map(&:to_sym).include?(attr)
24
+ end
25
+
26
+ def validate_limits(options = {})
27
+ columns = content_columns.map(&:name).map(&:to_sym)
28
+
29
+ if options[:only].present?
30
+ columns &= [*options[:only]]
31
+ elsif options[:except].present?
32
+ columns -= [*options[:except]]
33
+ end
34
+
35
+ columns.each do |column|
36
+ validate_attr(column) if valid_limit?(column) && valid_type?(column)
37
+ end
38
+ end
39
+
40
+ private
41
+ def validate_attr(attr)
42
+ validates attr, length: { maximum: limit_for(attr) }
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,13 @@
1
+ module DatabaseValidation
2
+ class Railtie < Rails::Railtie
3
+ initializer 'database-validations' do
4
+ ActiveSupport.on_load :active_record do
5
+ extend ModelAdditions
6
+ end
7
+
8
+ ActiveSupport.on_load :action_view do
9
+ include ViewHelpers
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,3 @@
1
+ module DatabaseValidation
2
+ VERSION = "0.1.1"
3
+ end
@@ -0,0 +1,11 @@
1
+ module DatabaseValidation
2
+ module ViewHelpers
3
+ class ActionView::Helpers::FormBuilder
4
+ def text_field_with_maxlength(method, options = {})
5
+ options[:maxlength] ||= @object.class.limit_for(method) unless @options[:skip_validations]
6
+ text_field_without_maxlength(method, options.merge(maxlength: options[:maxlength]))
7
+ end
8
+ alias_method_chain :text_field, :maxlength
9
+ end
10
+ end
11
+ end
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: database-validation
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Agustin Anfuso
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-11-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Simple way to add Active Record Validations to models and views through
42
+ the limits established in your database.
43
+ email:
44
+ - anfusoagustin89@gmail.com
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - .gitignore
50
+ - CHANGELOG.md
51
+ - Gemfile
52
+ - LICENSE.txt
53
+ - README.md
54
+ - Rakefile
55
+ - database-validation.gemspec
56
+ - lib/database-validation.rb
57
+ - lib/database_validation/model_additions.rb
58
+ - lib/database_validation/railtie.rb
59
+ - lib/database_validation/version.rb
60
+ - lib/database_validation/view_helpers.rb
61
+ homepage: ''
62
+ licenses:
63
+ - MIT
64
+ metadata: {}
65
+ post_install_message:
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - '>='
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ requirements: []
80
+ rubyforge_project:
81
+ rubygems_version: 2.0.3
82
+ signing_key:
83
+ specification_version: 4
84
+ summary: Length limit validations through database for Rails.
85
+ test_files: []