Skip to content

Commit

Permalink
Merge pull request #4 from twinge/fixes
Browse files Browse the repository at this point in the history
Fixes
  • Loading branch information
eric1234 committed Mar 21, 2016
2 parents 639886c + 9d5dbfe commit 250e596
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 94 deletions.
19 changes: 11 additions & 8 deletions lib/enforce_schema_rules.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def enforce_schema_rules(options = {})
enforce_not_null(options.dup)
enforce_unique_indexes(options.dup)
end

# Enforce string column limits
def enforce_column_limits(options = {})
args = build_validation_args(options, :string, :too_long)
Expand All @@ -27,12 +27,15 @@ def enforce_column_limits(options = {})
validates_each(*args) do |record, attr, value|
limit = record.class.columns_hash[attr.to_s].limit
if limit
message = options[:message] % {:count => limit}
message = case
when options[:message].is_a?(String) then options[:message] % {count: limit}
when limit == 1 then options[:message][:one]
else options[:message][:other] % {:count => limit} end
record.errors.add(attr, message) unless value.nil? || value.size <= limit
end
end
end

# Enforce numericality of integer columns
def enforce_integer_columns(options = {})
# first get the non-integers
Expand All @@ -44,14 +47,14 @@ def enforce_integer_columns(options = {})
args = build_validation_args(options, :integer, :not_a_number)
validates_numericality_of(*args) unless args.first.is_a? Hash
end

# Enfore "not null" columns settings
def enforce_not_null(options = {})
args = build_validation_args(options, :not_null, :blank)
return if args.first.is_a?(Hash)
validates_presence_of(*args)
end

# Enfore unique indexes
def enforce_unique_indexes(options = {})
attrs = build_validation_args(options, false, :taken)
Expand All @@ -61,7 +64,7 @@ def enforce_unique_indexes(options = {})
validates_uniqueness_of(index.columns.first, options)
end
end

def build_validation_args(options, col_type, validation_option = :invalid)
options[validation_option] = I18n.translate('errors.messages')[validation_option]
options[:message] ||= options[validation_option]
Expand All @@ -71,10 +74,10 @@ def build_validation_args(options, col_type, validation_option = :invalid)
when :numeric
lambda { |col| col.name !~ exclusion_regexp && col.number? && col.type != :integer }
when :not_null
# I have to exclude boolean types because of a "feature" of the way validates_presence_of
# I have to exclude boolean types because of a "feature" of the way validates_presence_of
# handles boolean fields
# See http://dev.rubyonrails.org/ticket/5090 and http://dev.rubyonrails.org/ticket/3334
lambda { |col| (col.name !~ exclusion_regexp || col.name =~ /_id$/) && !col.null && col.type != :boolean }
lambda { |col| col.name !~ exclusion_regexp && !col.null && col.type != :boolean }
else
lambda { |col| col.name !~ exclusion_regexp && col_type == col.type }
end
Expand Down
161 changes: 78 additions & 83 deletions test/enforce_test.rb
Original file line number Diff line number Diff line change
@@ -1,90 +1,85 @@
require File.join(File.dirname(__FILE__), 'test_setup')

class TestTableTest < Test::Unit::TestCase

def setup
EnforceSchema::UniqueRule.delete_all
end

def test_column_limit
model = EnforceSchema::AllRules.new
model.string_column = "long string"
assert !model.save
assert_equal 3, model.errors.count
assert_equal "is too long (maximum is 5 characters)", model.errors[:string_column].first
assert_equal "can't be blank", model.errors[:not_null_column].first
assert_equal "can't be blank", model.errors[:not_null_foreign_key_id].first
end

def test_not_null_attribute
model = EnforceSchema::NotNullRule.new
model.not_null_column = nil
assert !model.save
assert_equal 2, model.errors.count
assert_equal "can't be blank", model.errors[:not_null_column].first
assert_equal "can't be blank", model.errors[:not_null_foreign_key_id].first
end

def test_integer_with_string
require File.join(File.dirname(__FILE__), 'test_setup')

class TestTableTest < Test::Unit::TestCase

def setup
EnforceSchema::UniqueRule.delete_all
end

def test_column_limit
model = EnforceSchema::AllRules.new
model.string_column = "long string"
assert !model.save
assert_equal 2, model.errors.count
assert_equal "is too long (maximum is 5 characters)", model.errors[:string_column].first
assert_equal "can't be blank", model.errors[:not_null_column].first
end

def test_not_null_attribute
model = EnforceSchema::NotNullRule.new
model.not_null_column = nil
assert !model.save
assert_equal 1, model.errors.count
assert_equal "can't be blank", model.errors[:not_null_column].first
end

def test_integer_with_string
model = EnforceSchema::AllRules.new(:string_column => 'foo',
:not_null_column => 1, :not_null_foreign_key_id => 2, :boolean_column => false)
model.integer_column = "i'm obviously not an int"
assert !model.save
assert_equal 1, model.errors.count
assert_equal "is not a number", model.errors[:integer_column].first
end
def test_integer_with_float
:not_null_column => 1, :boolean_column => false)
model.integer_column = "i'm obviously not an int"
assert !model.save
assert_equal 1, model.errors.count
assert_equal "is not a number", model.errors[:integer_column].first
end

def test_integer_with_float
model = EnforceSchema::AllRules.new(:string_column => 'foo',
:not_null_column => 1, :not_null_foreign_key_id => 2, :boolean_column => false)
model.integer_column = 5.645
assert !model.save
assert_equal 1, model.errors.count
assert_equal "is not a number", model.errors[:integer_column].first
end
def test_float_with_string
:not_null_column => 1, :boolean_column => false)
model.integer_column = 5.645
assert !model.save
assert_equal 1, model.errors.count
assert_equal "is not a number", model.errors[:integer_column].first
end

def test_float_with_string
model = EnforceSchema::AllRules.new(:string_column => 'foo',
:not_null_column => 1, :not_null_foreign_key_id => 2, :boolean_column => false)
model.float_column = "blah"
assert !model.save
assert_equal 1, model.errors.count
assert_equal "is not a number", model.errors[:float_column].first
end
def test_float_with_int
:not_null_column => 1, :boolean_column => false)
model.float_column = "blah"
assert !model.save
assert_equal 1, model.errors.count
assert_equal "is not a number", model.errors[:float_column].first
end

def test_float_with_int
model = EnforceSchema::AllRules.new(:string_column => 'foo',
:not_null_column => 1, :not_null_foreign_key_id => 2, :boolean_column => false)
model.float_column = 5
assert model.save
end
def test_custom_message
model = EnforceSchema::StringRule.new
model.string_column = "another long string"
assert !model.save
assert_equal 1, model.errors.count
assert_equal "custom message", model.errors[:string_column].first
end
def test_unique
:not_null_column => 1, :boolean_column => false)
model.float_column = 5
assert model.save
end

def test_custom_message
model = EnforceSchema::StringRule.new
model.string_column = "another long string"
assert !model.save
assert_equal 1, model.errors.count
assert_equal "custom message", model.errors[:string_column].first
end

def test_unique
EnforceSchema::UniqueRule.create(:string_column => 'blah',
:not_null_column => 1, :not_null_foreign_key_id => 2,
:boolean_column => false)
:not_null_column => 1, :boolean_column => false)
model = EnforceSchema::UniqueRule.new(:string_column => 'blah',
:not_null_column => 1, :not_null_foreign_key_id => 2,
:boolean_column => false)
assert !model.save
assert_equal 1, model.errors.count
assert_equal "has already been taken", model.errors[:string_column].first
end

def test_created_at
:not_null_column => 1, :boolean_column => false)
assert !model.save
assert_equal 1, model.errors.count
assert_equal "has already been taken", model.errors[:string_column].first
end

def test_created_at
model = EnforceSchema::AllRules.new(:string_column => 'foo',
:not_null_column => 1, :not_null_foreign_key_id => 2,
:boolean_column => false)
assert model.save
assert_not_nil model.created_at
end

end
:not_null_column => 1, :boolean_column => false)
assert model.save
assert_not_nil model.created_at
end

end
3 changes: 0 additions & 3 deletions test/test_setup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
t.column :integer_column, :integer
t.column :float_column, :float
t.column :boolean_column, :boolean, :null => false
t.column :not_null_foreign_key_id, :integer, :null => false
t.column :created_at, :datetime
end
add_index(:enforce_schema_rules_test_table, :string_column, :unique => true)
Expand All @@ -39,5 +38,3 @@ class UniqueRule < Model
enforce_unique_indexes
end
end


0 comments on commit 250e596

Please sign in to comment.