simplificator-arext 0.2.0

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.
Files changed (5) hide show
  1. data/README +27 -0
  2. data/init.rb +4 -0
  3. data/lib/arext/validations.rb +42 -0
  4. data/lib/arext.rb +2 -0
  5. metadata +56 -0
data/README ADDED
@@ -0,0 +1,27 @@
1
+ Arext
2
+ =====
3
+
4
+ Active Record Extensions.
5
+
6
+ == Validations (validates_constraints() )
7
+ This uses the information one can extract from ActiveRecord::ConnectionAdapter::Column
8
+ to add default validations. The text?, limit and type methods are used to guess what validations are to be added
9
+ validates_constraints make some assumptions (see below) and none of its behaviour is configurable.
10
+ (you can always use the standard validations in addition to make some adjustments)
11
+
12
+ * Adds validates_presence_of for columns with not null constraints (unless type is :boolean)
13
+ * options are added so that empty strings will pass, unlike if you add the validate_presence_of yourself.
14
+ If you want to have different behaviour then just add a validation yourself
15
+ * Adds validates_inclusion_of(:in => [true, false]) for columns with not null constraints and :boolean type
16
+ * Adds validates_length_of for text columns with not null limit
17
+
18
+ Example
19
+ =======
20
+
21
+ in your ActiveRecord class:
22
+ validate_constraints(:attribute_name, :other_attribute_name)
23
+ or
24
+ validates_constraints() for all attributes
25
+
26
+
27
+ Copyright (c) 2008 [name of plugin creator], released under the MIT license
data/init.rb ADDED
@@ -0,0 +1,4 @@
1
+ require File.join(File.dirname(__FILE__), 'lib', 'arext')
2
+
3
+
4
+
@@ -0,0 +1,42 @@
1
+ module Arext
2
+ module Validations
3
+ module ClassMethods
4
+ def validates_constraints(*attrs)
5
+ attrs = column_names if attrs.blank?
6
+ attrs.each do |attr|
7
+ attribute_name = attr.to_s
8
+ col = columns_hash[attribute_name]
9
+ raise ArgumentError.new("Attribute #{attribute_name} is unknown for #{name}") unless col
10
+
11
+ if (col.name != primary_key) # don't touch id
12
+ # length checks
13
+ if col.text? && (not col.limit.blank?)
14
+ validates_length_of(attribute_name, :maximum => col.limit, :allow_nil => true, :allow_blank => true)
15
+ logger.debug("Added Validation for '#{attribute_name}' length to be maximum of #{col.limit}")
16
+ elsif col.text?
17
+ logger.debug("Did not find a limit for '#{attribute_name}' even though it's text")
18
+ end
19
+
20
+ # nil checks with validates_presence_of. skip boolean values
21
+ if (not col.null)
22
+ case col.type
23
+ when :boolean
24
+ # booleans can not be checked with presence_of
25
+ validates_inclusion_of(attribute_name, :in => [true, false])
26
+ logger.debug("Added Validation for inclusion of #{attribute_name} in [true, false] since null is not allowed")
27
+ else
28
+ validates_presence_of(attribute_name, :if => lambda {|item| item.send(col.name) != ''})
29
+ logger.debug("Added Validation for presence of #{attribute_name} since null is not allowed")
30
+ end
31
+
32
+ end
33
+ else
34
+ logger.debug("Skip validates_constraints for ID column #{col.name}")
35
+ end
36
+ end
37
+
38
+ end
39
+
40
+ end
41
+ end
42
+ end
data/lib/arext.rb ADDED
@@ -0,0 +1,2 @@
1
+ require File.join(File.dirname(__FILE__), 'arext', 'validations')
2
+ ActiveRecord::Base.send(:extend, Arext::Validations::ClassMethods)
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simplificator-arext
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Simplificator GmbH
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-08-22 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Adds some new validations to AR.
17
+ email: info@simplificator.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - init.rb
26
+ - lib/arext.rb
27
+ - lib/arext/validations.rb
28
+ - README
29
+ has_rdoc: false
30
+ homepage: http://labs.simplificator.com/
31
+ post_install_message:
32
+ rdoc_options: []
33
+
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: "0"
41
+ version:
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: "0"
47
+ version:
48
+ requirements: []
49
+
50
+ rubyforge_project:
51
+ rubygems_version: 1.2.0
52
+ signing_key:
53
+ specification_version: 2
54
+ summary: provieds some extensions to ActiveRecord
55
+ test_files: []
56
+