automagical_validations 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem "activerecord", ">= 3.0.0"
4
+
5
+ group :development do
6
+ gem "rspec", "~> 2.8.0"
7
+ gem "bundler", "~> 1.0.0"
8
+ gem "jeweler", "~> 1.8.3"
9
+ gem "sqlite3-ruby", "~> 1.3.3"
10
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Toms Mikoss
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,43 @@
1
+ #automagical_validations
2
+
3
+ automagical_validations provides way to automatically apply validations to ActiveRecord models based on the information that can be inferred from database schema.
4
+
5
+ ##Use
6
+
7
+ automagical_validations defines an `ActiveRecord::Base` class method that takes column type (like ones you would use in a migration) as argument:
8
+
9
+ class Post < ActiveRecord::Base
10
+ automagically_validate :string, :text
11
+ end
12
+
13
+ ##What it does
14
+ ###Column maximum length validations
15
+
16
+ automagical_validations will define maximum length validations on all columns matching the passed types.
17
+
18
+ If `automagically_validate` is invoked for columns that do not support `limit` attribute (depends chiefly on the adapter used), no validations will be created.
19
+
20
+ If a maximum length validation already exists for column (for example, `automagically_validate` is invoked after `validates_length_of`), additional validation will not be created.
21
+
22
+ ##Installation
23
+
24
+ Add the following line to your `Gemfile`
25
+
26
+ gem "automagical_validations"
27
+
28
+ and run the `bundle install` command.
29
+
30
+ ##Contributing to automagical_validations
31
+
32
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
33
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
34
+ * Fork the project.
35
+ * Start a feature/bugfix branch.
36
+ * Commit and push until you are happy with your contribution.
37
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
38
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
39
+
40
+ ## Copyright
41
+
42
+ Copyright &copy; 2012 Toms Mikoss. See LICENSE.txt for
43
+ further details.
data/Rakefile ADDED
@@ -0,0 +1,34 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "automagical_validations"
18
+ gem.homepage = "http://github.com/tmikoss/automagical_validations"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{ActiveRecord extension that allows to infer validation rules from database}
21
+ gem.description = %Q{ActiveRecord extension that allows to infer validation rules from database}
22
+ gem.email = "toms.mikoss@gmail.com"
23
+ gem.authors = ["Toms Mikoss"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+ task :default => :spec
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,22 @@
1
+ module AutomagicalValidations
2
+ def automagically_validate(*types_to_validate)
3
+ types_to_validate.map!{ |type| type.to_sym }
4
+
5
+ columns.each do |column|
6
+ # Do not touch the columns not specified
7
+ next unless types_to_validate.include? column.type
8
+
9
+ # Do not define validator on columns that have no limit information, even if asked to
10
+ next unless column.limit
11
+
12
+ # Do not define additional length validators if any are already in place
13
+ next if validators_on(column.name).any? do |validator|
14
+ validator.is_a?(ActiveModel::Validations::LengthValidator) && validator.options[:maximum]
15
+ end
16
+
17
+ validates_length_of column.name, :maximum => column.limit
18
+ end
19
+ end
20
+ end
21
+
22
+ ActiveRecord::Base.extend AutomagicalValidations
@@ -0,0 +1,171 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "AutomagicalValidations" do
4
+ subject { Post.new }
5
+
6
+ context "maximum length validator" do
7
+ context "presence of validators" do
8
+ before(:all) do
9
+ Post.rebuild_table do |t|
10
+ t.string :title
11
+ t.text :content
12
+ end
13
+
14
+ Post.automagically_validate :string
15
+ end
16
+
17
+ it "should define length validator on specified type of column" do
18
+ Post.should have(1).validators_on(:title)
19
+ Post.validators_on(:title).first.should be_a ActiveModel::Validations::LengthValidator
20
+ end
21
+
22
+ it "should not define any validators on other types of columns" do
23
+ Post.validators_on(:content).should be_empty
24
+ end
25
+ end
26
+
27
+ context "when asked to define validation on column that provides no limit information" do
28
+ before(:all) do
29
+ Post.rebuild_table do |t|
30
+ t.date :published_at
31
+ end
32
+
33
+ Post.automagically_validate :date
34
+ end
35
+
36
+ it "should not define any validators" do
37
+ Post.validators_on(:published_at).should be_empty
38
+ end
39
+ end
40
+
41
+ context "when column type is provided as string" do
42
+ before(:all) do
43
+ Post.rebuild_table do |t|
44
+ t.string :title
45
+ end
46
+
47
+ Post.automagically_validate 'string'
48
+ end
49
+
50
+ it "should define validators" do
51
+ Post.should have(1).validators_on(:title)
52
+ end
53
+ end
54
+
55
+ context "when a table is defined with implicit limits (defaults)" do
56
+ before(:all) do
57
+ Post.rebuild_table do |t|
58
+ t.string :title
59
+ end
60
+
61
+ Post.automagically_validate :string
62
+ end
63
+
64
+ it "should be valid when content is not specified" do
65
+ subject.should be_valid
66
+ end
67
+
68
+ it "should be valid when content is empty" do
69
+ subject.title = ''
70
+ subject.should be_valid
71
+ end
72
+
73
+ it "should be valid when under or equal to default Rails string column length (255)" do
74
+ subject.title = "a"*255
75
+ subject.should be_valid
76
+ end
77
+
78
+ it "should not be valid when over default Rails string column length (255)" do
79
+ subject.title = "a"*256
80
+ subject.should_not be_valid
81
+ end
82
+ end
83
+
84
+ context "when a table is defined with explicit limits" do
85
+ before(:all) do
86
+ Post.rebuild_table do |t|
87
+ t.string :title, :limit => 20
88
+ end
89
+
90
+ Post.automagically_validate :string
91
+ end
92
+
93
+ it "should be valid when content is not specified" do
94
+ subject.should be_valid
95
+ end
96
+
97
+ it "should be valid when content is empty" do
98
+ subject.title = ''
99
+ subject.should be_valid
100
+ end
101
+
102
+ it "should be valid when under or equal to specified limit" do
103
+ subject.title = "a"*20
104
+ subject.should be_valid
105
+ end
106
+
107
+ it "should not be valid when over specified limit" do
108
+ subject.title = "a"*21
109
+ subject.should_not be_valid
110
+ end
111
+ end
112
+
113
+ context "when explicit validation exists" do
114
+ context "when explicit validation is defined before automagical validation" do
115
+ before(:all) do
116
+ Post.rebuild_table do |t|
117
+ t.string :title, :limit => 20
118
+ end
119
+
120
+ Post.validates_length_of :title, :maximum => 10
121
+ Post.automagically_validate :string
122
+ end
123
+
124
+ it "should not overwrite explicit validation" do
125
+ subject.title = "a"*15
126
+ subject.should_not be_valid
127
+ end
128
+
129
+ it "should not define additional validation" do
130
+ Post.should have(1).validators_on(:title)
131
+ end
132
+ end
133
+
134
+ context "when explicit validation without maximum parameter is defined before automagical validation" do
135
+ before(:all) do
136
+ Post.rebuild_table do |t|
137
+ t.string :title, :limit => 20
138
+ end
139
+
140
+ Post.validates_length_of :title, :minimum => 10
141
+ Post.automagically_validate :string
142
+ end
143
+
144
+ it "should provide validation for max value" do
145
+ subject.title = "a"*21
146
+ subject.should_not be_valid
147
+ end
148
+
149
+ it "should define additional validation to take care of maximum length" do
150
+ Post.should have(2).validators_on(:title)
151
+ end
152
+ end
153
+
154
+ context "when explicit validation is defined after automagical validation" do
155
+ before(:all) do
156
+ Post.rebuild_table do |t|
157
+ t.string :title, :limit => 20
158
+ end
159
+
160
+ Post.automagically_validate :string
161
+ Post.validates_length_of :title, :maximum => 10
162
+ end
163
+
164
+ it "explicit validation should take precedence" do
165
+ subject.title = "a"*15
166
+ subject.should_not be_valid
167
+ end
168
+ end
169
+ end
170
+ end
171
+ end
@@ -0,0 +1,34 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'active_record'
5
+ require 'automagical_validations'
6
+
7
+ # Requires supporting files with custom matchers and macros, etc,
8
+ # in ./support/ and its subdirectories.
9
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
10
+
11
+ ActiveRecord::Base.establish_connection(
12
+ :adapter => "sqlite3",
13
+ :database => "#{File.dirname(__FILE__)}/automagical_validations.db"
14
+ )
15
+
16
+ class Post < ActiveRecord::Base
17
+ def self.rebuild_table
18
+ ActiveRecord::Schema.define do
19
+ self.verbose = false
20
+
21
+ create_table :posts, :force => true do |t|
22
+ yield t
23
+ end
24
+ end
25
+
26
+ reset_column_information
27
+
28
+ # Reset all validators to prevent test cases from messing with each other
29
+ self._validators = columns.inject({}) do |memo, column|
30
+ memo[column.name.to_sym] = []
31
+ memo
32
+ end
33
+ end
34
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: automagical_validations
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Toms Mikoss
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-04-04 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activerecord
16
+ requirement: &891825110 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 3.0.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *891825110
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec
27
+ requirement: &891824830 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 2.8.0
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *891824830
36
+ - !ruby/object:Gem::Dependency
37
+ name: bundler
38
+ requirement: &891824540 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: 1.0.0
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *891824540
47
+ - !ruby/object:Gem::Dependency
48
+ name: jeweler
49
+ requirement: &891824100 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 1.8.3
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *891824100
58
+ - !ruby/object:Gem::Dependency
59
+ name: sqlite3-ruby
60
+ requirement: &891823040 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ~>
64
+ - !ruby/object:Gem::Version
65
+ version: 1.3.3
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *891823040
69
+ description: ActiveRecord extension that allows to infer validation rules from database
70
+ email: toms.mikoss@gmail.com
71
+ executables: []
72
+ extensions: []
73
+ extra_rdoc_files:
74
+ - LICENSE.txt
75
+ - README.md
76
+ files:
77
+ - .document
78
+ - .rspec
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - VERSION
84
+ - lib/automagical_validations.rb
85
+ - spec/automagical_validations_spec.rb
86
+ - spec/spec_helper.rb
87
+ homepage: http://github.com/tmikoss/automagical_validations
88
+ licenses:
89
+ - MIT
90
+ post_install_message:
91
+ rdoc_options: []
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ none: false
96
+ requirements:
97
+ - - ! '>='
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ segments:
101
+ - 0
102
+ hash: 802746131
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ none: false
105
+ requirements:
106
+ - - ! '>='
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 1.8.17
112
+ signing_key:
113
+ specification_version: 3
114
+ summary: ActiveRecord extension that allows to infer validation rules from database
115
+ test_files: []