validates_by_schema 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,11 +1,37 @@
1
1
  # Validates By Schema (validates_by_schema)
2
2
  [![Build Status](https://secure.travis-ci.org/joshwlewis/validates_by_schema.png)](http://travis-ci.org/joshwlewis/validates_by_schema)
3
+ [![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/joshwlewis/validates_by_schema)
4
+ [![Dependency Status](https://gemnasium.com/joshwlewis/validates_by_schema.png)](https://gemnasium.com/joshwlewis/validates_by_schema)
5
+ [![Gem Version](https://badge.fury.io/rb/validates_by_schema.png)](http://badge.fury.io/rb/validates_by_schema)
3
6
 
4
7
  Automatic validation based on your database schema column types and limits. Keep your code DRY by inferring column validations from table properties!
5
8
 
6
- ## Install
9
+ ## Example
10
+
11
+ Say you had a table setup like this:
12
+
13
+ ```ruby
14
+ create_table "widgets", :force => true do |t|
15
+ t.integer "quantity", :limit => 2
16
+ t.decimal "thickness", :precision => 4, :scale => 4
17
+ t.string "color", :null => false
18
+ end
19
+ ```
20
+
21
+ Then these validations are inferred when you add `validates_by_schema` to your model:
22
+
23
+ ```ruby
24
+ validates :quantity, numericality: { allow_nil: true,
25
+ greater_than: -32768, less_than: 32768}
26
+ validates :thickness, numericality: {allow_nil: true,
27
+ less_than_or_equal_to: 0.999, greater_than_or_equal_to: -0.999}
28
+ validates :color, presence: true, length: {allow_nil: false, maximum: 255}
29
+ ```
30
+
31
+ ## Installation
7
32
 
8
33
  1. Add it to your Gemfile:
34
+
9
35
  ```ruby
10
36
  gem "validates_by_schema"
11
37
  ```
@@ -13,15 +39,16 @@ gem "validates_by_schema"
13
39
  2. Then `bundle`
14
40
 
15
41
  3. Call it from your ActiveRecord model:
42
+
16
43
  ```ruby
17
44
  class Widget < ActiveRecord::Base
18
- validates_by_schema
45
+ validates_by_schema
19
46
  end
20
47
  ```
21
48
 
22
49
  ## Usage
23
50
 
24
- You can also whitelist/blacklist columns with :only and :except options
51
+ You can also whitelist or blacklist columns with :only or :except options, respectively:
25
52
 
26
53
  ```ruby
27
54
  validates_by_schema only: [:body, :description]
@@ -31,22 +58,8 @@ validates_by_schema only: [:body, :description]
31
58
  validates_by_schema except: [:name, :title]
32
59
  ```
33
60
 
34
- ## Example
35
-
36
- Say you had a table setup like this:
37
- ```ruby
38
- create_table "widgets", :force => true do |t|
39
- t.integer "wheel_count", :null => false
40
- t.decimal "thickness", :precision => 4, :scale => 4
41
- t.string "color"
42
- end
43
- ```
61
+ ## Notes
44
62
 
45
- Then these validations run when your model `validates_by_schema`:
46
- ```ruby
47
- validates :wheel_count, presence: true, numericality {allow_nil: false, greater_than: -2147483647, less_than: 2147483647}
48
- validates :thickness, numericality: {allow_nil: true, less_than_or_equal_to: 0.999, greater_than_or_equal_to: -0.999}
49
- validates :color, length: {allow_nil: true, maximum: 255}
50
- ```
63
+ Column properties are inferred by your database adapter (like pg, mysql2, sqlite3), and does not depend on migration files or schema.rb. As such, you could use this on projects where the database where Rails is not in control of the database configuration.
51
64
 
52
- This is tested with postgres, mysql, and sqlite3. It will most likely work with any other database Rails supports.
65
+ This has been tested with mysql, postgresql, and sqlite3. It should work with any other database that has reliable adapter.
@@ -6,17 +6,11 @@ module ValidatesBySchema
6
6
  module ClassMethods
7
7
  def validates_by_schema options={}
8
8
  return unless table_exists?
9
- # Don't auto validate primary keys or timestamps
10
- columns = self.columns.reject(&:primary)
11
- columns.reject!{|c| ['updated_at', 'created_at'].include? c.name}
9
+ columns = schema_validateable_columns
12
10
 
13
11
  # Allow user to specify :only or :except options
14
- except = options.delete(:except).try(:map, &:to_s)
15
- only = options.delete(:only).try(:map, &:to_s)
16
- if only.present?
17
- columns.select!{|c| only.include? c.name}
18
- elsif except.present?
19
- columns.reject!{|c| except.include? c.name}
12
+ [:only => :select!, :except => :reject!].each do |k,v|
13
+ columns.send(v){|c| options[k].include? c.name} if options[k]
20
14
  end
21
15
 
22
16
  columns.each do |c|
@@ -24,6 +18,13 @@ module ValidatesBySchema
24
18
  validates c.name, vo if vo.present?
25
19
  end
26
20
  end
21
+
22
+ def schema_validateable_columns
23
+ # Don't auto validate primary keys or timestamps
24
+ columns.reject do |c|
25
+ c.primary || %w(updated_at created_at).include?(c.name)
26
+ end
27
+ end
27
28
  end
28
29
  end
29
30
 
@@ -1,3 +1,3 @@
1
1
  module ValidatesBySchema
2
- VERSION = "0.2.1"
2
+ VERSION = "0.2.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: validates_by_schema
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-12-15 00:00:00.000000000 Z
12
+ date: 2013-01-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails