Skip to content
Browse files

add lowercase constraint

  • Loading branch information...
1 parent 3047fd3 commit 137232893bc5c540776a477c49e92a69c7f6ab2c @look look committed Jan 28, 2010
Showing with 56 additions and 27 deletions.
  1. +37 −27 lib/constraints.rb
  2. +19 −0 test/sexy_pg_constraints_test.rb
View
64 lib/constraints.rb
@@ -1,28 +1,28 @@
module SexyPgConstraints
module Constraints
module_function
-
+
##
# Only allow listed values.
#
- # Example:
+ # Example:
# constrain :books, :variation, :whitelist => %w(hardcover softcover)
#
def whitelist(table, column, options)
"check (#{table}.#{column} in (#{ options.collect{|v| "'#{v}'"}.join(',') }))"
end
-
- ##
+
+ ##
# Prohibit listed values.
#
- # Example:
+ # Example:
# constrain :books, :isbn, :blacklist => %w(invalid_isbn1 invalid_isbn2)
#
def blacklist(table, column, options)
"check (#{table}.#{column} not in (#{ options.collect{|v| "'#{v}'"}.join(',') }))"
end
-
- ##
+
+ ##
# The value must have at least 1 non-space character.
#
# Example:
@@ -32,7 +32,7 @@ def not_blank(table, column, options)
"check ( length(trim(both from #{table}.#{column})) > 0 )"
end
- ##
+ ##
# The numeric value must be within given range.
#
# Example:
@@ -45,7 +45,7 @@ def within(table, column, options)
"check (#{column_ref} >= #{options.begin} and #{column_ref} #{options.exclude_end? ? ' < ' : ' <= '} #{options.end})"
end
- ##
+ ##
# Check the length of strings/text to be within the range.
#
# Example:
@@ -54,8 +54,8 @@ def within(table, column, options)
def length_within(table, column, options)
within(table, "length(#{table}.#{column})", options)
end
-
- ##
+
+ ##
# Allow only valid email format.
#
# Example:
@@ -65,7 +65,7 @@ def email(table, column, options)
"check (((#{table}.#{column})::text ~ E'^([-a-z0-9]+)@([-a-z0-9]+[.]+[a-z]{2,4})$'::text))"
end
- ##
+ ##
# Allow only alphanumeric values.
#
# Example:
@@ -74,8 +74,18 @@ def email(table, column, options)
def alphanumeric(table, column, options)
"check (((#{table}.#{column})::text ~* '^[a-z0-9]+$'::text))"
end
-
- ##
+
+ ##
+ # Allow only lower case values.
+ #
+ # Example:
+ # constrain :books, :author, :lowercase => true
+ #
+ def lowercase(table, column, options)
+ "check (#{table}.#{column} = lower(#{table}.#{column}))"
+ end
+
+ ##
# Allow only positive values.
#
# Example:
@@ -84,8 +94,8 @@ def alphanumeric(table, column, options)
def positive(table, column, options)
"check (#{table}.#{column} >= 0)"
end
-
- ##
+
+ ##
# Allow only odd values.
#
# Example:
@@ -95,7 +105,7 @@ def odd(table, column, options)
"check (mod(#{table}.#{column}, 2) != 0)"
end
- ##
+ ##
# Allow only even values.
#
# Example:
@@ -105,7 +115,7 @@ def even(table, column, options)
"check (mod(#{table}.#{column}, 2) = 0)"
end
- ##
+ ##
# Make sure every entry in the column is unique.
#
# Example:
@@ -115,8 +125,8 @@ def unique(table, column, options)
column = Array(column).map {|c| %{"#{c}"} }.join(', ')
"unique (#{column})"
end
-
- ##
+
+ ##
# Allow only text/strings of the exact length specified, no more, no less.
#
# Example:
@@ -125,8 +135,8 @@ def unique(table, column, options)
def exact_length(table, column, options)
"check ( length(trim(both from #{table}.#{column})) = #{options} )"
end
-
- ##
+
+ ##
# Allow only values that match the regular expression.
#
# Example:
@@ -135,8 +145,8 @@ def exact_length(table, column, options)
def format(table, column, options)
"check (((#{table}.#{column})::text #{options.casefold? ? '~*' : '~'} E'#{options.source}'::text ))"
end
-
- ##
+
+ ##
# Add foreign key constraint.
#
# Example:
@@ -146,10 +156,10 @@ def reference(table, column, options)
on_delete = options.delete(:on_delete)
fk_table = options.keys.first
fk_column = options[fk_table]
-
+
on_delete = "on delete #{on_delete}" if on_delete
-
+
%{foreign key ("#{column}") references #{fk_table} (#{fk_column}) #{on_delete}}
end
end
-end
+end
View
19 test/sexy_pg_constraints_test.rb
@@ -853,4 +853,23 @@ def test_multicolumn_constraint_block_syntax
book.isbn = 'bar'
end
end
+
+ def test_lowercase
+ ActiveRecord::Migration.constrain :books, :author, :lowercase => true
+
+ assert_prohibits Book, :author, :lowercase do |book|
+ book.author = 'UPPER'
+ end
+
+ assert_allows Book do |book|
+ book.author = 'lower with 1337'
+ end
+
+ ActiveRecord::Migration.deconstrain :books, :author, :lowercase
+
+ assert_allows Book do |book|
+ book.author = 'UPPER'
+ end
+
+ end
end

0 comments on commit 1372328

Please sign in to comment.
Something went wrong with that request. Please try again.